{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting\n", "\n", "## Basics\n", "There are a few different ways to plot in Julia (including calling PyPlot).
\n", "\n", "Here we'll show you how to use `Plots.jl`. If it's not installed yet, you need to use the package manager to install it, and Julia will precompile it for you the first time you use it:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#using Pkg\n", "#Pkg.add(\"Plots\")\n", "using Plots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the advantages to `Plots.jl` is that it allows you to seamlessly change backends. In this notebook, we'll try out the `gr()` and `plotlyjs()` backends.
\n", "\n", "In the name of scientific inquiry, let's use this notebook to examine the relationship between the global temperature and the number of pirates between roughly 1860 and 2000." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "globaltemperatures = [14.4, 14.5, 14.8, 15.2, 15.5, 15.8]\n", "numpirates = [45000, 20000, 15000, 5000, 400, 17];" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plots supports multiple backends — that is, libraries that actually do the drawing — all with the same API. To start out, let's try the GR backend. You choose it with a call to `gr()`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Plots.GRBackend()" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gr()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and now we can use commands like `plot` and `scatter` to generate plots." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1×10\n", "\n", "\n", "4\n", "\n", "\n", "2×10\n", "\n", "\n", "4\n", "\n", "\n", "3×10\n", "\n", "\n", "4\n", "\n", "\n", "4×10\n", "\n", "\n", "4\n", "\n", "\n", "14.50\n", "\n", "\n", "14.75\n", "\n", "\n", "15.00\n", "\n", "\n", "15.25\n", "\n", "\n", "15.50\n", "\n", "\n", "15.75\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "line\n", "\n", "\n", "\n", "points\n", "\n", "\n" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(numpirates, globaltemperatures, label=\"line\")\n", "scatter!(numpirates, globaltemperatures, label=\"points\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `!` at the end of the `scatter!` function name makes `scatter!` a mutating function, indicating that the scattered points will be added onto the pre-existing plot.\n", "\n", "In contrast, see what happens when you replace `scatter!` in the above with the non-mutating function `scatter`.\n", "\n", "Next, let's update this plot with the `xlabel!`, `ylabel!`, and `title!` commands to add more information to our plot." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1×10\n", "\n", "\n", "4\n", "\n", "\n", "2×10\n", "\n", "\n", "4\n", "\n", "\n", "3×10\n", "\n", "\n", "4\n", "\n", "\n", "4×10\n", "\n", "\n", "4\n", "\n", "\n", "14.50\n", "\n", "\n", "14.75\n", "\n", "\n", "15.00\n", "\n", "\n", "15.25\n", "\n", "\n", "15.50\n", "\n", "\n", "15.75\n", "\n", "\n", "Influence of pirate population on global warming\n", "\n", "\n", "Number of Pirates [Approximate]\n", "\n", "\n", "Global Temperature (°C)\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "line\n", "\n", "\n", "\n", "points\n", "\n", "\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xlabel!(\"Number of Pirates [Approximate]\")\n", "ylabel!(\"Global Temperature (°C)\")\n", "title!(\"Influence of pirate population on global warming\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This still doesn't look quite right. The number of pirates has decreased since 1860, so reading the plot from left to right is like looking backwards in time rather than forwards. Let's flip the x axis to better see how pirate populations have caused global temperatures to change over time!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1×10\n", "\n", "\n", "4\n", "\n", "\n", "2×10\n", "\n", "\n", "4\n", "\n", "\n", "3×10\n", "\n", "\n", "4\n", "\n", "\n", "4×10\n", "\n", "\n", "4\n", "\n", "\n", "14.50\n", "\n", "\n", "14.75\n", "\n", "\n", "15.00\n", "\n", "\n", "15.25\n", "\n", "\n", "15.50\n", "\n", "\n", "15.75\n", "\n", "\n", "Influence of pirate population on global warming\n", "\n", "\n", "Number of Pirates [Approximate]\n", "\n", "\n", "Global Temperature (°C)\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "line\n", "\n", "\n", "\n", "points\n", "\n", "\n" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xflip!()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And there we have it!\n", "\n", "Note: We've had some confusion about this exercise. :) This is a joke about how people often conflate correlation and causation.\n", "\n", "**Without changing syntax, we can create this plot with the UnicodePlots backend**" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Series 1\n", "\n", "\n", "Series 2\n", "\n", "\n", "Series 3\n", "\n", "\n", "Series 4\n", "\n", "\n", "0.00\n", "\n", "\n", "0.25\n", "\n", "\n", "0.50\n", "\n", "\n", "0.75\n", "\n", "\n", "1.00\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Pkg.add(\"StatsPlots\")\n", "using StatsPlots\n", "y = rand(100, 4) # Four series of 100 points each\n", "violin([\"Series 1\" \"Series 2\" \"Series 3\" \"Series 4\"], y, leg = false)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "\n", "4\n", "\n", "\n", "6\n", "\n", "\n", "8\n", "\n", "\n", "10\n", "\n", "\n", "0.2\n", "\n", "\n", "0.4\n", "\n", "\n", "0.6\n", "\n", "\n", "0.8\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "\n", "4\n", "\n", "\n", "6\n", "\n", "\n", "8\n", "\n", "\n", "10\n", "\n", "\n", "0.2\n", "\n", "\n", "0.4\n", "\n", "\n", "0.6\n", "\n", "\n", "0.8\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "\n", "4\n", "\n", "\n", "6\n", "\n", "\n", "8\n", "\n", "\n", "10\n", "\n", "\n", "0.2\n", "\n", "\n", "0.4\n", "\n", "\n", "0.6\n", "\n", "\n", "0.8\n", "\n", "\n", "Subtitle\n", "\n", "\n", "This one is labelled\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0.00\n", "\n", "\n", "0.25\n", "\n", "\n", "0.50\n", "\n", "\n", "0.75\n", "\n", "\n", "1.00\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "3\n", "\n", "\n", "4\n", "\n", "\n", "5\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1:10; y = rand(10, 2) # 2 columns means two lines\n", "p1 = plot(x, y) # Make a line plot\n", "p2 = scatter(x, y) # Make a scatter plot\n", "p3 = plot(x, y, xlabel = \"This one is labelled\", lw = 3, title = \"Subtitle\")\n", "p4 = histogram(x, y) # Four histograms each with 10 points? Why not!\n", "plot(p1, p2, p3, p4, layout = (2, 2), legend = false)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Info: Precompiling DataFrames [a93c6f00-e57d-5684-b7b6-d8193f3e46c0]\n", "└ @ Base loading.jl:1260\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "\n", "4\n", "\n", "\n", "6\n", "\n", "\n", "8\n", "\n", "\n", "10\n", "\n", "\n", "2\n", "\n", "\n", "4\n", "\n", "\n", "6\n", "\n", "\n", "8\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "b\n", "\n", "\n", "\n", "c\n", "\n", "\n" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using StatsPlots # Required for the DataFrame user recipe\n", "# Now let's create the DataFrame\n", "using DataFrames\n", "df = DataFrame(a = 1:10, b = 10 * rand(10), c = 10 * rand(10))\n", "# Plot the DataFrame by declaring the points by the column names\n", "@df df plot(:a, [:b :c]) # x = :a, y = [:b :c]. Notice this is two columns!" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "0\n", "\n", "\n", "10\n", "\n", "\n", "20\n", "\n", "\n", "0.00\n", "\n", "\n", "0.02\n", "\n", "\n", "0.04\n", "\n", "\n", "0.06\n", "\n", "\n", "0.08\n", "\n", "\n", "\n", "\n", "\n", "\n", "y1\n", "\n", "\n" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Distributions\n", "plot(Normal(3, 5), lw = 3)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Info: Precompiling RDatasets [ce6b1742-4840-55fa-b093-852dadbb1d8b]\n", "└ @ Base loading.jl:1260\n", "┌ Warning: Attribute alias `ylabel` detected in the plot recipe defined for the signature (::Type{Val{:marginalhist}}, ::AbstractPlot). To ensure expected behavior it is recommended to use the default attribute `yguide`.\n", "└ @ Plots C:\\Users\\USti\\.julia\\packages\\Plots\\7U0ob\\src\\pipeline.jl:15\n", "┌ Warning: Attribute alias `xlabel` detected in the plot recipe defined for the signature (::Type{Val{:marginalhist}}, ::AbstractPlot). To ensure expected behavior it is recommended to use the default attribute `xguide`.\n", "└ @ Plots C:\\Users\\USti\\.julia\\packages\\Plots\\7U0ob\\src\\pipeline.jl:15\n", "┌ Warning: Attribute alias `ylabel` detected in the plot recipe defined for the signature (::Type{Val{:marginalhist}}, ::AbstractPlot). To ensure expected behavior it is recommended to use the default attribute `yguide`.\n", "└ @ Plots C:\\Users\\USti\\.julia\\packages\\Plots\\7U0ob\\src\\pipeline.jl:15\n", "┌ Warning: Attribute alias `xlabel` detected in the plot recipe defined for the signature (::Type{Val{:marginalhist}}, ::AbstractPlot). To ensure expected behavior it is recommended to use the default attribute `xguide`.\n", "└ @ Plots C:\\Users\\USti\\.julia\\packages\\Plots\\7U0ob\\src\\pipeline.jl:15\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "3\n", "\n", "\n", "4\n", "\n", "\n", "5\n", "\n", "\n", "6\n", "\n", "\n", "7\n", "\n", "\n", "0.5\n", "\n", "\n", "1.0\n", "\n", "\n", "1.5\n", "\n", "\n", "2.0\n", "\n", "\n", "2.5\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using RDatasets, StatsPlots\n", "iris = dataset(\"datasets\", \"iris\")\n", "@df iris marginalhist(:PetalLength, :PetalWidth)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Series 1\n", "\n", "\n", "Series 2\n", "\n", "\n", "Series 3\n", "\n", "\n", "Series 4\n", "\n", "\n", "0.00\n", "\n", "\n", "0.25\n", "\n", "\n", "0.50\n", "\n", "\n", "0.75\n", "\n", "\n", "1.00\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = rand(100, 4) # Four series of 100 points each\n", "violin([\"Series 1\" \"Series 2\" \"Series 3\" \"Series 4\"], y, leg = false)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Series 1\n", "\n", "\n", "Series 2\n", "\n", "\n", "Series 3\n", "\n", "\n", "Series 4\n", "\n", "\n", "0.00\n", "\n", "\n", "0.25\n", "\n", "\n", "0.50\n", "\n", "\n", "0.75\n", "\n", "\n", "1.00\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "boxplot!([\"Series 1\" \"Series 2\" \"Series 3\" \"Series 4\"], y, leg = false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises\n", "\n", "#### 8.1\n", "Given\n", "```julia\n", "x = -10:10\n", "```\n", "plot y vs. x for $y = x^2$. You may want to change backends back again." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "0\n", "\n", "\n", "25\n", "\n", "\n", "50\n", "\n", "\n", "75\n", "\n", "\n", "100\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "y1\n", "\n", "\n", "\n", "y2\n", "\n", "\n" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Plots\n", "using StatsPlots\n", "f(x) = x^2\n", "x = -10:0.1:10\n", "y = f.(x)\n", "y[2]\n", "plot(x, y);\n", "scatter!(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 8.2\n", "Execute the following code" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "0\n", "\n", "\n", "25\n", "\n", "\n", "50\n", "\n", "\n", "75\n", "\n", "\n", "100\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "-1000\n", "\n", "\n", "-500\n", "\n", "\n", "0\n", "\n", "\n", "500\n", "\n", "\n", "1000\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "0\n", "\n", "\n", "2500\n", "\n", "\n", "5000\n", "\n", "\n", "7500\n", "\n", "\n", "10000\n", "\n", "\n", "\n" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = plot(x, x)\n", "p2 = plot(x, x.^2)\n", "p3 = plot(x, x.^3)\n", "p4 = plot(x, x.^4)\n", "plot(p1, p2, p3, p4, layout = (2, 2), legend = false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and then create a $4x1$ plot that uses `p1`, `p2`, `p3`, and `p4` as subplots." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "0\n", "\n", "\n", "25\n", "\n", "\n", "50\n", "\n", "\n", "75\n", "\n", "\n", "100\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "-1000\n", "\n", "\n", "-500\n", "\n", "\n", "0\n", "\n", "\n", "500\n", "\n", "\n", "1000\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-10\n", "\n", "\n", "-5\n", "\n", "\n", "0\n", "\n", "\n", "5\n", "\n", "\n", "10\n", "\n", "\n", "0\n", "\n", "\n", "2500\n", "\n", "\n", "5000\n", "\n", "\n", "7500\n", "\n", "\n", "10000\n", "\n", "\n", "\n" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(p1, p2, p3, p4, layout = (4, 1), legend = false)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.4", "language": "julia", "name": "julia-1.3" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.0" } }, "nbformat": 4, "nbformat_minor": 4 }