From 9039b3e378f7d138b26c1255483fcc11719a7728 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Fri, 18 Nov 2022 19:15:44 -0800 Subject: [PATCH 01/28] Rewrite basic plotting, plot attributes --- docs/src/tutorial.md | 78 ++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 9e70cabae6..7d4e0e9066 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -12,58 +12,86 @@ the manual. ## Basic Plotting: Line Plots -The most basic plots are line plots. Assuming you have installed Plots.jl via -`Pkg.add("Plots")`, you can plot a line by calling `plot` on two vectors of -numbers. For example: +After you have installed Plots.jl, the first step is to initialize the package. +Depending on your computer, this will take a few seconds: ```@example tutorial using Plots -x = 1:10; y = rand(10); # These are the plotting data +``` + +To start, let's plot some trig functions. For the `x` coordinates, we can +create a range from 0 to 10 of, say, 100 elements. For the `y` coordinates, we +can create a vector by evaluating `sin(x)` in an element-wise fashion. To do this +in Julia, we insert a dot right after the function call. Finally, we use `plot()` +to plot the line. + +```@example tutorial +x = range(0, 10, length=100) +y = sin.(x) plot(x, y) ``` The plot is displayed in a plot pane, a stand-alone window or the browser, depending on the environment and backend (see [below](@ref plotting-backends)). -In Plots.jl, every column is a **series**, i.e. a set of related points which -form lines, surfaces, or other plotting primitives. Thus we can plot multiple -lines by plotting a matrix of values and each column is interpreted as a -separate line: +If this is your first plot of the session and it takes a while to show up, +this is normal; this latency is often called the "time to first plot" (TTFP) +problem, and subsequent plots will be fast. Because of the way Julia works under +the hood, this is a difficult problem to solve, but much progress has been made +in the past few years to reduce this compilation time. + +In Plots.jl, every column is a **series**, a set of related points which +form lines, surfaces, or other plotting primitives. We can plot multiple +lines by plotting a matrix of values where each column is interpreted as a +separate line. Below, `[y1 y2]` forms a `100x2` matrix (100 elements, 2 columns). ```@example tutorial -x = 1:10; y = rand(10, 2) # 2 columns means two lines -plot(x, y) +x = range(0, 10, length=100) +y1 = sin.(x) +y2 = cos.(x) +plot(x, [y1 y2]) ``` Additionally, we can add more lines by mutating the plot object. This is done -by the `plot!` command. Let's add another line to our current plot: +by the `plot!` command, where the `!` denotes that the command is modifying +the current plot. +You'll notice that we use an `@.` macro. This is a convenience macro +that inserts dots for every function call to the right of the macro, ensuring +that the entire expression is to be evaluated in an element-wise manner. +If we inputted the dots manually, we would need three of them for the sine, +exponent, and subtraction, and the resulting code would be less readable. ```@example tutorial -z = rand(10) -plot!(x, z) +y3 = @. sin(x)^2 - 1/2 # equivalent to y3 = sin.(x).^2 .- 1/2 +plot!(x, y3) ``` Note that we could have done the same as above using an explicit plot variable: ```@example tutorial -x = 1:10; y = rand(10, 2) # 2 columns means two lines -p = plot(x, y) -z = rand(10) -plot!(p, x, z) +x = range(0, 10, length=100) +y1 = sin.(x) +y2 = cos.(x) +p = plot(x, [y1 y2]) + +y3 = @. sin(x)^2 - 1/2 +plot!(p, x, y3) ``` -Note that in the case where `p` is omitted, Plots.jl uses the global -`Plots.CURRENT_PLOT` automatically in the same manner. +In cases where the plot variable is omitted, Plots.jl uses the global +`Plots.CURRENT_PLOT` automatically. ## Plot Attributes -In the previous section we made plots... we're done right? No! We need to style -our plots. In Plots.jl, the modifiers to plots are called **attributes**. These +In the previous section we made plots... we're done, right? No! We need to style +our plots. In Plots.jl, the modifiers to plots are called **attributes**, which are documented at the [attributes page](@ref attributes). Plots.jl follows a simple -rule with data vs attributes: positional arguments are input data, and keyword -arguments are attributes. Thus something like `plot(x,y,z)` is 3-dimensional -data for 3D plots, while `plot(x,y,attribute=value)` is 2-dimensional with -an attribute. +rule with data vs attributes: +* Positional arguments correspond to input data +* Keyword arguments correspond to attributes +So something like `plot(x, y, z)` is 3-dimensional data for 3D plots, +while `plot(x, y, attribute=value)` is 2-dimensional data with an attribute assigned +to some value. As an example, we see that from the attributes page that we can increase the line width using `linewidth` (or its alias `lw`), change the legend's labels From 1e2169cdc76b1fcb05c1b87db87b8b269c156476 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Fri, 18 Nov 2022 20:20:32 -0800 Subject: [PATCH 02/28] Overhaul attributes section --- docs/src/tutorial.md | 81 +++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 7d4e0e9066..1a92616fe1 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -35,8 +35,8 @@ The plot is displayed in a plot pane, a stand-alone window or the browser, depending on the environment and backend (see [below](@ref plotting-backends)). If this is your first plot of the session and it takes a while to show up, -this is normal; this latency is often called the "time to first plot" (TTFP) -problem, and subsequent plots will be fast. Because of the way Julia works under +this is normal; this latency is called the "time to first plot" problem, +and subsequent plots will be fast. Because of the way Julia works under the hood, this is a difficult problem to solve, but much progress has been made in the past few years to reduce this compilation time. @@ -55,7 +55,7 @@ plot(x, [y1 y2]) Additionally, we can add more lines by mutating the plot object. This is done by the `plot!` command, where the `!` denotes that the command is modifying the current plot. -You'll notice that we use an `@.` macro. This is a convenience macro +You'll notice that we also use an `@.` macro. This is a convenience macro that inserts dots for every function call to the right of the macro, ensuring that the entire expression is to be evaluated in an element-wise manner. If we inputted the dots manually, we would need three of them for the sine, @@ -66,7 +66,8 @@ y3 = @. sin(x)^2 - 1/2 # equivalent to y3 = sin.(x).^2 .- 1/2 plot!(x, y3) ``` -Note that we could have done the same as above using an explicit plot variable: +Note that we could have done the same as above using an explicit plot variable, +which we call `p`: ```@example tutorial x = range(0, 10, length=100) @@ -87,36 +88,70 @@ In the previous section we made plots... we're done, right? No! We need to style our plots. In Plots.jl, the modifiers to plots are called **attributes**, which are documented at the [attributes page](@ref attributes). Plots.jl follows a simple rule with data vs attributes: + * Positional arguments correspond to input data * Keyword arguments correspond to attributes -So something like `plot(x, y, z)` is 3-dimensional data for 3D plots, -while `plot(x, y, attribute=value)` is 2-dimensional data with an attribute assigned -to some value. -As an example, we see that from the attributes page that we can increase the -line width using `linewidth` (or its alias `lw`), change the legend's labels -using the `label` command, and add a title with `title`. Let's apply that to our -previous plot: +So something like `plot(x, y, z)` is three-dimensional data for 3D plots with no +attributes, while `plot(x, y, attribute=value)` is two-dimensional data with +one attribute assigned to some value. + +As an example, we can change the line width using `linewidth` (or its alias `lw`), +change the legend's labels using `label`, and add a title with `title`. Notice how +`["sin(x)" "cos(x)"]` has the same number of columns as the data. +Additionally, since the linewidth is being attributed to `[y1 y2]`, both lines +will be affected by the assigned value. Let's apply these to our previous plot: ```@example tutorial -x = 1:10; y = rand(10, 2) # 2 columns means two lines -plot(x, y, title = "Two Lines", label = ["Line 1" "Line 2"], lw = 3) +x = range(0, 10, length=100) +y1 = sin.(x) +y2 = cos.(x) +plot(x, [y1 y2], title="Trig functions", label=["sin(x)" "cos(x)"], linewidth=3) ``` -Note that every attribute can also be applied by mutating the plot with a -modifier function. For example, the `xlabel` attribute adds a label for the -x-axis. We can in the plot command specify it via `xlabel=...` like we did above. -Or we can use the modifier function to add it after the plot has already been -generated: +Every attribute can also be applied by mutating the plot with a +modifier function. Some attributes have their own dedicated modifier functions, +while others can be accessed through `plot!(attribute=value)`. +For example, the `xlabel` attribute adds a label for the +x-axis. We can specify it in the plot command with `xlabel=...` like we did +above, or we can use the modifier function to add it after the plot has already +been generated. It's up to you to decide which is better for code readability. + +Every modifier function is the name of the attribute followed by `!`. This will +implicitly use the global `Plots.CURRENT_PLOT`. We can apply it to +other plot objects via `attribute!(p, value)`. + +Let's use keywords and modifier functions interchangeably +to perform the some common modifications. You'll notice that for the attributes +`ls` and `legend`, a colon `:` is inserted before the name. The colon denotes +a symbol in Julia, which are commonly used for values of attributes, along with +strings and numbers. + +* Labels for the individual lines, seen in the legend +* Line widths (we'll use the alias `lw` instead of `linewidth`) +* Line styles (we'll use the alias `ls` instead of `linestyle`) +* Legend position (outside the plot, as the default would clutter the plot) +* Legend columns (3, to better use the horizontal space) +* X-limits to go from `0` to `2pi` +* Plot title and axis labels ```@example tutorial -xlabel!("My x label") +x = range(0, 10, length=100) +y1 = sin.(x) +y2 = cos.(x) +y3 = @. sin(x)^2 - 1/2 + +plot(x, [y1 y2], label=["sin(x)" "cos(x)"], lw=[2 1]) +plot!(x, y3, label="sin(x)^2 - 1/2", lw=3, ls=:dot) +plot!(legend=:outerbottom, legendcolumns=3) +xlims!(0, 2pi) +title!("Trig functions") +xlabel!("x") +ylabel!("y") ``` -Every modifier function is the name of the attribute followed by `!`. Note that -this implicitly uses the global `Plots.CURRENT_PLOT` and we can apply it to -other plot objects via `attribute!(p,value)`. For more examples of attributes -in action, see the examples pages. +More information about attributes can be found in the Attributes section +of the Manual. ## [Plotting Backends](@id plotting-backends) From c16174fb76272f0fee074138918accdc8b5ce843 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Fri, 18 Nov 2022 21:32:14 -0800 Subject: [PATCH 03/28] Overhaul plotting scatter, in scripts --- docs/src/tutorial.md | 203 ++++++++++++++++++++++++++----------------- 1 file changed, 124 insertions(+), 79 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 1a92616fe1..87ad8fdf08 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -8,7 +8,8 @@ Plots.reset_defaults() This is a guide for getting you up and running with Plots.jl. Its main goal is to introduce you to the terminology used in the package, how to use Plots.jl in common use cases, and put you in a position to easily understand the rest of -the manual. +the manual. We recommend you follow along the code examples inside the REPL +or an interactive notebook. ## Basic Plotting: Line Plots @@ -86,8 +87,8 @@ In cases where the plot variable is omitted, Plots.jl uses the global In the previous section we made plots... we're done, right? No! We need to style our plots. In Plots.jl, the modifiers to plots are called **attributes**, which -are documented at the [attributes page](@ref attributes). Plots.jl follows a simple -rule with data vs attributes: +are documented at the [attributes page](@ref attributes). Plots.jl follows two +simple rules with data and attributes: * Positional arguments correspond to input data * Keyword arguments correspond to attributes @@ -99,8 +100,9 @@ one attribute assigned to some value. As an example, we can change the line width using `linewidth` (or its alias `lw`), change the legend's labels using `label`, and add a title with `title`. Notice how `["sin(x)" "cos(x)"]` has the same number of columns as the data. -Additionally, since the linewidth is being attributed to `[y1 y2]`, both lines -will be affected by the assigned value. Let's apply these to our previous plot: +Additionally, since the line width is being attributed to `[y1 y2]`, both lines +will be affected by the assigned value. Let's apply all of this to our previous +plot: ```@example tutorial x = range(0, 10, length=100) @@ -115,17 +117,24 @@ while others can be accessed through `plot!(attribute=value)`. For example, the `xlabel` attribute adds a label for the x-axis. We can specify it in the plot command with `xlabel=...` like we did above, or we can use the modifier function to add it after the plot has already -been generated. It's up to you to decide which is better for code readability. +been generated. It's up to you to decide which is better for code readability. + +```julia +xlabel!("x") +``` Every modifier function is the name of the attribute followed by `!`. This will implicitly use the global `Plots.CURRENT_PLOT`. We can apply it to -other plot objects via `attribute!(p, value)`. +other plot objects via `attribute!(p, value)`, where `p` is the name +of the plot object that wants to be modified. -Let's use keywords and modifier functions interchangeably -to perform the some common modifications. You'll notice that for the attributes -`ls` and `legend`, a colon `:` is inserted before the name. The colon denotes -a symbol in Julia, which are commonly used for values of attributes, along with -strings and numbers. +Let's use keywords and modifier functions interchangeably to perform some +common modifications to our example, listed below. You'll notice that for the +attributes `ls` and `legend`, a colon `:` is inserted before the name. +The colon denotes a symbol in Julia. They are commonly used for values of +attributes in Plots.jl, along with strings and numbers. Another thing to note +is that `y3` is being plotted as a dotted line. This is distinct from a +scatter plot of the data. * Labels for the individual lines, seen in the legend * Line widths (we'll use the alias `lw` instead of `linewidth`) @@ -153,101 +162,85 @@ ylabel!("y") More information about attributes can be found in the Attributes section of the Manual. -## [Plotting Backends](@id plotting-backends) +## Changing the Plotting Series -Here's a secret: Plots.jl isn't actually a plotting package! Plots.jl is a -plotting metapackage: it's an interface over many different plotting libraries. -Thus what Plots.jl is actually doing is interpreting your commands and then -generating the plots using another plotting library. This plotting library in -the background is referred to as the **backend**. The nice thing about this -is that this means you can use many different plotting libraries all with the -Plots.jl syntax, and we'll see in a little bit that Plots.jl adds new features -to each of these libraries! +At this point you know about line plots, but don't you want to plot your data +in other ways? In Plots.jl, these other ways of plotting a series is called a +**series type**. A line is one series type. However, a scatter plot is another +series type which is commonly used. -When we started plotting above, our plot used the default backend GR. However, let's say we want a -different plotting backend which will plot into a nice GUI or into the plot pane -of VS Code. To do this, we'll need a backend which is compatible with these -features. Some common backends for this are PyPlot and Plotly. To install these -backends, simply use the standard Julia installation -(`Pkg.add("BackendPackage")`). We can specifically choose the backend we are -plotting into by using the name of the backend in all lower case as a function. -Let's plot the example from above using Plotly and then GR: +Let's start with the sine function again, but this type, we'll define a vector +called `y_noisy` that adds some randomness. +We can change the series type by the `seriestype` attribute. ```@example tutorial -x = 1:10; y = rand(10, 2) # 2 columns means two lines -plotlyjs() # Set the backend to Plotly -# This plots into the web browser via Plotly -plot(x, y, title = "This is Plotted using Plotly") -png("tutorial_1") # hide -``` -![](tutorial_1.png) +x = range(0, 10, length=100) +y = sin.(x) +y_noisy = @. sin(x) + 0.1*randn() -```@example tutorial -gr() # Set the backend to GR -# This plots using GR -plot(x, y, title = "This is Plotted using GR") +plot(x, y, label="sin(x)") +plot!(x, y_noisy, seriestype=:scatter, label="data") ``` -If you're in VS Code or Juno, the first plot command will cause the plot to open in the -plot pane. If you're in the REPL, the plot command will open in a browser window. You can always -open a GUI anyways by using the `gui()` command. - -Each plotting backend has a very different feel. Some have interactivity, some -are faster and can deal with huge numbers of datapoints, and some can do -3D plots. Saving plots is done by the `savefig` command. As an example: +For each built-in series type, there is a shorthand function for directly +calling that series type which matches the name of the series type. It handles +attributes just the same as the `plot` command, and have a mutating form which +ends in `!`. For example, we can instead write the last line as: ```julia -savefig("myplot.png") # Saves the CURRENT_PLOT as a .png -savefig(p, "myplot.pdf") # Saves the plot from p as a .pdf vector graphic +scatter!(x, y_noisy, label="data") ``` -Some backends like GR can save to vector graphics and PDFs, while others like Plotly only save to `.png`s. For more information on backends, see the -[backends page](@ref backends). For examples of plots from the various backends, see -the Examples section. +The series types which are available are dependent on the backend, and are +documented on the [Supported Attributes page](@ref supported). As we will describe +later, other libraries can add new series types using **recipes**. -## Changing the Plotting Series +Scatter plots will have some common attributes related to the markers. Here +is an example of the same plot, but with some attributes fleshed out to make +the plot more presentable. Many aliases are used for brevity: -At this point you're a master of lines, but don't you want to plot your data -in other ways? In Plots.jl, these other ways of plotting a series is called a -**series type**. A line is one series type. However, a scatter plot is another -series type which is commonly used. We can change the series type by the -`seriestype` attribute: +* `lc` for `linecolor` +* `lw` for `linewidth` +* `mc` for `markercolor` +* `ms` for `markersize` +* `ma` for `markeralpha` ```@example tutorial -gr() # We will continue onward using the GR backend -plot(x, y, seriestype = :scatter, title = "My Scatter Plot") -``` +using Random +Random.seed!(1234) # set the seed to make the plot reproducible -For each built-in series type, there is a shorthand function for directly -calling that series type which matches the name of the series type. It handles -attributes just the same as the `plot` command, and have a mutating form which -ends in `!`. For example, we can instead do that scatter plot with: +x = range(0, 10, length=100) +y = sin.(x) +y_noisy = @. sin(x) + 0.1*randn() -```@example tutorial -scatter(x, y, title = "My Scatter Plot") +plot(x, y, label="sin(x)", lc=:black, lw=2) +scatter!(x, y_noisy, label="data", mc=:red, ms=2, ma=0.5) +plot!(legend=:bottomleft) +title!("Sine with noise") +xlabel!("x") +ylabel!("y") ``` -The series types which are available are dependent on the backend, and are -documented on the [Supported Attributes page](@ref supported). As we will describe -later, other libraries can add new series types via **recipes** as well. - ## Plotting in Scripts -Now that you're making useful plots, go ahead and add these plotting commands -to a script. Now call the script... and the plot doesn't show up? This is -because Julia in interactive use calls `display` on every variable that is -returned by a command without a `;`. Thus in each case above, the interactive -usage was automatically calling `display` on the returned plot objects. +At the start of the tutorial, we recommended following along the code examples +in an interactive session for the following reason: try adding those same +plotting commands to a script. Now call the script... and the plot doesn't +show up? This is because Julia in interactive use calls `display` on every +variable that is returned by a command without a semicolon `;`. In each case +above, the interactive usage was automatically calling `display` on the returned +plot objects. -In a script, Julia does not do automatic displays (which is why `;` is not -necessary). However, if we would like to display our plots in a script, this +In a script, Julia does not do automatic displays, which is why `;` is not +necessary. However, if we would like to display our plots in a script, this means we just need to add the `display` call. For example: ```julia display(plot(x, y)) ``` -If we have a plot object `p`, we can do `display(p)` at any time. +Alternatively, we could call `gui()` at the end to do the same thing. +If we have a plot object `p`, we can type `display(p)` to display the plot. ## Combining Multiple Plots as Subplots @@ -283,6 +276,58 @@ Notice that the attributes in the individual plots are applied to the individual plots, while the attributes on the final `plot` call are applied to all of the subplots. +## [Plotting Backends](@id plotting-backends) + +Here's a secret: Plots.jl isn't actually a plotting package! Plots.jl is a +plotting metapackage: it's an interface over many different plotting libraries. +Thus what Plots.jl is actually doing is interpreting your commands and then +generating the plots using another plotting library. This plotting library in +the background is referred to as the **backend**. The nice thing about this +is that this means you can use many different plotting libraries all with the +Plots.jl syntax, and we'll see in a little bit that Plots.jl adds new features +to each of these libraries! + +When we started plotting above, our plot used the default backend GR. However, let's say we want a +different plotting backend which will plot into a nice GUI or into the plot pane +of VS Code. To do this, we'll need a backend which is compatible with these +features. Some common backends for this are PyPlot and Plotly. To install these +backends, simply use the standard Julia installation +(`Pkg.add("BackendPackage")`). We can specifically choose the backend we are +plotting into by using the name of the backend in all lower case as a function. +Let's plot the example from above using Plotly and then GR: + +```@example tutorial +x = 1:10; y = rand(10, 2) # 2 columns means two lines +plotlyjs() # Set the backend to Plotly +# This plots into the web browser via Plotly +plot(x, y, title = "This is Plotted using Plotly") +png("tutorial_1") # hide +``` +![](tutorial_1.png) + +```@example tutorial +gr() # Set the backend to GR +# This plots using GR +plot(x, y, title = "This is Plotted using GR") +``` + +If you're in VS Code or Juno, the first plot command will cause the plot to open in the +plot pane. If you're in the REPL, the plot command will open in a browser window. You can always +open a GUI anyways by using the `gui()` command. + +Each plotting backend has a very different feel. Some have interactivity, some +are faster and can deal with huge numbers of datapoints, and some can do +3D plots. Saving plots is done by the `savefig` command. As an example: + +```julia +savefig("myplot.png") # Saves the CURRENT_PLOT as a .png +savefig(p, "myplot.pdf") # Saves the plot from p as a .pdf vector graphic +``` + +Some backends like GR can save to vector graphics and PDFs, while others like Plotly only save to `.png`s. For more information on backends, see the +[backends page](@ref backends). For examples of plots from the various backends, see +the Examples section. + ## Plot Recipes and Recipe Libraries You now know all of the basic terminology of Plots.jl and can roam the From ee5dc79793c651b4aa756065b4381b1de3642959 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:55:06 -0800 Subject: [PATCH 04/28] Update tutorial.md --- docs/src/tutorial.md | 197 +++++++++++++++++++++++++++---------------- 1 file changed, 122 insertions(+), 75 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 87ad8fdf08..d18d246bc9 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -44,7 +44,7 @@ in the past few years to reduce this compilation time. In Plots.jl, every column is a **series**, a set of related points which form lines, surfaces, or other plotting primitives. We can plot multiple lines by plotting a matrix of values where each column is interpreted as a -separate line. Below, `[y1 y2]` forms a `100x2` matrix (100 elements, 2 columns). +separate line. Below, `[y1 y2]` forms a 100x2 matrix (100 elements, 2 columns). ```@example tutorial x = range(0, 10, length=100) @@ -83,6 +83,15 @@ plot!(p, x, y3) In cases where the plot variable is omitted, Plots.jl uses the global `Plots.CURRENT_PLOT` automatically. +### Saving Figures + +Saving plots is done by the `savefig` command. For example: + +```julia +savefig("myplot.png") # Saves the CURRENT_PLOT as a .png +savefig(p, "myplot.pdf") # Saves the plot from p as a .pdf vector graphic +``` + ## Plot Attributes In the previous section we made plots... we're done, right? No! We need to style @@ -159,6 +168,26 @@ xlabel!("x") ylabel!("y") ``` +### Logarithmic Scale Plots + +Sometimes data needs to be plotted across orders of magnitude. The attributes +`xscale` and `yscale` can be set to `:log10`. Care should be taken to ensure +that the data and limits are positive. In our case, `x` has to start from +a positive number, and the lower y-limit cannot be 0. + +```@example tutorial +x = 10 .^ range(0, 4, length=100) +y = @. 1/(1+x) + +plot(x, y, label="1/(1+x)") +plot!(xscale=:log10, yscale=:log10, minorgrid=true) +xlims!(1e0, 1e4) +ylims!(1e-5, 1e0) +title!("Log-log plot") +xlabel!("x") +ylabel!("y") +``` + More information about attributes can be found in the Attributes section of the Manual. @@ -206,9 +235,6 @@ the plot more presentable. Many aliases are used for brevity: * `ma` for `markeralpha` ```@example tutorial -using Random -Random.seed!(1234) # set the seed to make the plot reproducible - x = range(0, 10, length=100) y = sin.(x) y_noisy = @. sin(x) + 0.1*randn() @@ -221,6 +247,63 @@ xlabel!("x") ylabel!("y") ``` +## [Plotting Backends](@id plotting-backends) + +Here's a secret: Plots.jl isn't actually a plotting package! Plots.jl is a +plotting metapackage: it's an interface over many different plotting libraries. +What Plots.jl is actually doing is interpreting your commands and then +generating the plots using another plotting library, called the **backend**. +The nice thing about this is that you can use many different plotting libraries +all with the Plots.jl syntax, and we'll see in a little bit that Plots.jl +adds new features to each of these libraries! + +When we started plotting above, our plot used the default backend GR. +However, let's say we want a different plotting backend which will plot into +a nice GUI or into the plot pane of VS Code. To do this, we'll need a backend +which is compatible with these features. Some common backends for this are +PyPlot and Plotly. For example, to install PyPlot, simply type the command +`Pkg.add("PyPlot")` into the REPL; to install Plotly, replace with `PlotlyJS`. + +We can specifically choose the backend we are plotting into by using the name +of the backend in all lowercase as a function. Let's plot the example from +above using Plotly and then GR: + +```@example tutorial +plotlyjs() # set the backend to Plotly + +x = range(0, 10, length=100) +y = sin.(x) +y_noisy = @. sin(x) + 0.1*randn() + +# this plots into the web browser via Plotly +plot(x, y, label="sin(x)", lc=:black, lw=2) +scatter!(x, y_noisy, label="data", mc=:red, ms=2, ma=0.5) +plot!(legend=:bottomleft) +title!("Sine with noise, plotted with Plotly") +xlabel!("x") +ylabel!("y") +``` + +```@example tutorial +gr() # set the backend to GR + +# this plots using GR +plot(x, y, label="sin(x)", lc=:black, lw=2) +scatter!(x, y_noisy, label="data", mc=:red, ms=2, ma=0.5) +plot!(legend=:bottomleft) +title!("Sine with noise, plotted with GR") +xlabel!("x") +ylabel!("y") +``` + +Each plotting backend has a very different feel. Some have interactivity, some +are faster and can deal with huge numbers of datapoints, and some can do +3D plots. Some backends like GR can save to vector graphics and PDFs, while +others like Plotly only save to PNGs. + +For more information on backends, see the [backends page](@ref backends). +For examples of plots from the various backends, see the Examples section. + ## Plotting in Scripts At the start of the tutorial, we recommended following along the code examples @@ -240,7 +323,8 @@ display(plot(x, y)) ``` Alternatively, we could call `gui()` at the end to do the same thing. -If we have a plot object `p`, we can type `display(p)` to display the plot. +Finally, if we have a plot object `p`, we can type `display(p)` to +display the plot. ## Combining Multiple Plots as Subplots @@ -250,83 +334,45 @@ for generating simple layouts. More advanced layouts are shown in the [Layouts page](@ref layouts). The first method is to define a layout which will split a series. The `layout` -command takes in a 2-tuple `layout=(N, M)` which builds an NxM grid of plots. -It will automatically split a series to be in each plot. For example, if we do -`layout=(4,1)` on a plot with four series, then we will get four rows of plots, -each with one series in it: +command takes in a 2-tuple `layout=(N, M)` which builds an NxM grid of plots, +and it will automatically split a series to be in each plot. For example, if we +type `layout=(3, 1)` on a plot with three series, then we will get three rows of +plots, each with one series in it. + +Let's define some functions and plot them in separate plots. Since there's only +one series in each plot, we'll also remove the legend in each of the plots +using `legend=false`. ```@example tutorial -y = rand(10, 4) -plot(x, y, layout = (4, 1)) +x = range(0, 10, length=100) +y1 = @. exp(-0.1x) * cos(4x) +y2 = @. exp(-0.3x) * cos(4x) +y3 = @. exp(-0.5x) * cos(4x) +y = [y1 y2 y3] +plot(x, [y1 y2 y3], layout=(3, 1), legend=false) ``` We can also use layouts on plots of plot objects. For example, we can generate -for separate plots and make a single plot that combines them in a 2x2 grid -via the following: +four separate plots and make a single plot that combines them in a 2x2 grid. ```@example tutorial -p1 = plot(x, y) # Make a line plot -p2 = scatter(x, y) # Make a scatter plot -p3 = plot(x, y, xlabel = "This one is labelled", lw = 3, title = "Subtitle") -p4 = histogram(x, y) # Four histograms each with 10 points? Why not! -plot(p1, p2, p3, p4, layout = (2, 2), legend = false) +x = range(0, 10, length=100) +y1 = @. exp(-0.1x) * cos(4x) +y2 = @. exp(-0.3x) * cos(4x) +y3 = @. exp(-0.1x) +y4 = @. exp(-0.3x) +y = [y1 y2 y3 y4] + +p1 = plot(x, y) +p2 = plot(x, y, title="Title 2", lw=3) +p3 = scatter(x, y, ms=2, ma=0.5, xlabel="xlabel 3") +p4 = scatter(x, y, title="Title 4", ms=2, ma=0.2) +plot(p1, p2, p3, p4, layout=(2,2), legend=false) ``` Notice that the attributes in the individual plots are applied to the -individual plots, while the attributes on the final `plot` call are applied -to all of the subplots. - -## [Plotting Backends](@id plotting-backends) - -Here's a secret: Plots.jl isn't actually a plotting package! Plots.jl is a -plotting metapackage: it's an interface over many different plotting libraries. -Thus what Plots.jl is actually doing is interpreting your commands and then -generating the plots using another plotting library. This plotting library in -the background is referred to as the **backend**. The nice thing about this -is that this means you can use many different plotting libraries all with the -Plots.jl syntax, and we'll see in a little bit that Plots.jl adds new features -to each of these libraries! - -When we started plotting above, our plot used the default backend GR. However, let's say we want a -different plotting backend which will plot into a nice GUI or into the plot pane -of VS Code. To do this, we'll need a backend which is compatible with these -features. Some common backends for this are PyPlot and Plotly. To install these -backends, simply use the standard Julia installation -(`Pkg.add("BackendPackage")`). We can specifically choose the backend we are -plotting into by using the name of the backend in all lower case as a function. -Let's plot the example from above using Plotly and then GR: - -```@example tutorial -x = 1:10; y = rand(10, 2) # 2 columns means two lines -plotlyjs() # Set the backend to Plotly -# This plots into the web browser via Plotly -plot(x, y, title = "This is Plotted using Plotly") -png("tutorial_1") # hide -``` -![](tutorial_1.png) - -```@example tutorial -gr() # Set the backend to GR -# This plots using GR -plot(x, y, title = "This is Plotted using GR") -``` - -If you're in VS Code or Juno, the first plot command will cause the plot to open in the -plot pane. If you're in the REPL, the plot command will open in a browser window. You can always -open a GUI anyways by using the `gui()` command. - -Each plotting backend has a very different feel. Some have interactivity, some -are faster and can deal with huge numbers of datapoints, and some can do -3D plots. Saving plots is done by the `savefig` command. As an example: - -```julia -savefig("myplot.png") # Saves the CURRENT_PLOT as a .png -savefig(p, "myplot.pdf") # Saves the plot from p as a .pdf vector graphic -``` - -Some backends like GR can save to vector graphics and PDFs, while others like Plotly only save to `.png`s. For more information on backends, see the -[backends page](@ref backends). For examples of plots from the various backends, see -the Examples section. +individual plots, while the attribute `legend=false` in the final `plot` +call is applied to all of the subplots. ## Plot Recipes and Recipe Libraries @@ -354,8 +400,8 @@ on are: 2. It adds a plot recipe for marginal histograms. 3. It adds a bunch of new statistical plot series. -Besides recipes, StatsPlots.jl also provides a specialized macro from plotting directly -from data tables. +Besides recipes, StatsPlots.jl also provides a specialized macro from plotting +directly from data tables. ### Using User Recipes @@ -443,6 +489,7 @@ try. Here's a short list of very usable addons to check out: - [PlotThemes.jl](https://github.com/JuliaPlots/PlotThemes.jl) allows you to change the color scheme of your plots. For example, `theme(:dark)` adds a dark theme. -- [StatsPlots.jl](https://github.com/JuliaPlots/StatsPlots.jl) adds functionality for visualizations of statistical analysis +- [StatsPlots.jl](https://github.com/JuliaPlots/StatsPlots.jl) adds functionality + for visualizations of statistical analysis - The [ecosystem page](@ref ecosystem) shows many other packages which have recipes and extend Plots.jl's functionality. From e874237e449c6dd9176ce8baf8664dbb73ea3b7f Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Fri, 18 Nov 2022 23:13:46 -0800 Subject: [PATCH 05/28] Expand log section, copyediting --- docs/src/tutorial.md | 71 ++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index d18d246bc9..dc85e23c1e 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -171,9 +171,9 @@ ylabel!("y") ### Logarithmic Scale Plots Sometimes data needs to be plotted across orders of magnitude. The attributes -`xscale` and `yscale` can be set to `:log10`. Care should be taken to ensure -that the data and limits are positive. In our case, `x` has to start from -a positive number, and the lower y-limit cannot be 0. +`xscale` and `yscale` can be set to `:log10` in this case. They can also be +set to `:identity` to keep them linear-scale. +Care should be taken to ensure that the data and limits are positive. ```@example tutorial x = 10 .^ range(0, 4, length=100) @@ -181,8 +181,8 @@ y = @. 1/(1+x) plot(x, y, label="1/(1+x)") plot!(xscale=:log10, yscale=:log10, minorgrid=true) -xlims!(1e0, 1e4) -ylims!(1e-5, 1e0) +xlims!(1e+0, 1e+4) +ylims!(1e-5, 1e+0) title!("Log-log plot") xlabel!("x") ylabel!("y") @@ -226,7 +226,8 @@ later, other libraries can add new series types using **recipes**. Scatter plots will have some common attributes related to the markers. Here is an example of the same plot, but with some attributes fleshed out to make -the plot more presentable. Many aliases are used for brevity: +the plot more presentable. Many aliases are used for brevity, and the list +below is by no means exhaustive. * `lc` for `linecolor` * `lw` for `linewidth` @@ -262,7 +263,8 @@ However, let's say we want a different plotting backend which will plot into a nice GUI or into the plot pane of VS Code. To do this, we'll need a backend which is compatible with these features. Some common backends for this are PyPlot and Plotly. For example, to install PyPlot, simply type the command -`Pkg.add("PyPlot")` into the REPL; to install Plotly, replace with `PlotlyJS`. +`Pkg.add("PyPlot")` into the REPL; to install Plotly, type +`Pkg.add("PlotlyJS")`. We can specifically choose the backend we are plotting into by using the name of the backend in all lowercase as a function. Let's plot the example from @@ -299,7 +301,7 @@ ylabel!("y") Each plotting backend has a very different feel. Some have interactivity, some are faster and can deal with huge numbers of datapoints, and some can do 3D plots. Some backends like GR can save to vector graphics and PDFs, while -others like Plotly only save to PNGs. +others like Plotly can only save to PNGs. For more information on backends, see the [backends page](@ref backends). For examples of plots from the various backends, see the Examples section. @@ -353,7 +355,7 @@ plot(x, [y1 y2 y3], layout=(3, 1), legend=false) ``` We can also use layouts on plots of plot objects. For example, we can generate -four separate plots and make a single plot that combines them in a 2x2 grid. +four separate plots and make a single plot that combines them into a 2x2 grid. ```@example tutorial x = range(0, 10, length=100) @@ -406,26 +408,31 @@ directly from data tables. ### Using User Recipes A user recipe says how to interpret plotting commands on a new data type. -In this case, StatsPlots.jl thus has a macro `@df` which allows you to plot +In this case, StatsPlots.jl has a macro `@df` which allows you to plot a `DataFrame` directly by using the column names. Let's build a `DataFrame` with columns `a`, `b`, and `c`, and tell Plots.jl to use `a` as the `x` axis and plot the series defined by columns `b` and `c`: ```@example tutorial # Pkg.add("StatsPlots") -using StatsPlots # Required for the DataFrame user recipe -# Now let's create the DataFrame +# required for the dataframe user recipe +using StatsPlots + +# now let's create the dataframe using DataFrames -df = DataFrame(a = 1:10, b = 10 * rand(10), c = 10 * rand(10)) -# Plot the DataFrame by declaring the points by the column names -@df df plot(:a, [:b :c]) # x = :a, y = [:b :c]. Notice this is two columns! +df = DataFrame(a=1:10, b=10*rand(10), c=10*rand(10)) + +# plot the dataframe by declaring the points by the column names +# x = :a, y = [:b :c] (notice that y has two columns!) +@df df plot(:a, [:b :c]) ``` -Notice there's not much you have to do here: all of the commands from before +There's not much you have to do here: all of the commands from before (attributes, series types, etc.) will still work on this data: ```@example tutorial -@df df scatter(:a, :b, title = "My DataFrame Scatter Plot!") # x = :a, y = :b +# x = :a, y = :b +@df df scatter(:a, :b, title="My DataFrame Scatter Plot!") ``` ### Using a Type Recipe @@ -436,49 +443,49 @@ data: ```@example tutorial using Distributions -plot(Normal(3, 5), lw = 3) +plot(Normal(3, 5), lw=3) ``` -Thus type recipes are a very convenient way to plot a specialized type which +Type recipes are a very convenient way to plot a specialized type which requires no more intervention! ### Using Plot Recipes -StatsPlots.jl adds the `marginhist` multiplot via a plot recipe. For our data -we will pull in the famous `iris` dataset from RDatasets: +StatsPlots.jl adds the `marginhist` multiplot via a plot recipe. For our data, +we'll pull in the famous `iris` dataset from RDatasets: ```@example tutorial -#Pkg.add("RDatasets") +# Pkg.add("RDatasets") using RDatasets, StatsPlots iris = dataset("datasets", "iris") @df iris marginalhist(:PetalLength, :PetalWidth) ``` -Here `iris` is a Dataframe, using the `@df` macro on `Dataframe`s described above, +Here, `iris` is a DataFrame; using the `@df` macro on `DataFrame`s described above, we give `marginalhist(x, y)` the data from the `PetalLength` and the `PetalWidth` columns. -This demonstrates two important facts. Notice that this is more than a series -since it generates multiple series (i.e. there are multiple plots due to the -hists on the top and right). Thus a plot recipe is not just a series but instead -something like a new `plot` command. +Notice that this is more than a series since it generates multiple series +(i.e. there are multiple plots due to the hists on the top and right). +Thus a plot recipe is not just a series, but also something like a new +`plot` command. ### Using Series Recipes StatsPlots.jl also introduces new series recipes. The key is that you don't have -to do anything differently: after `using StatsPlots` you can simply use those +to do anything differently. After `using StatsPlots`, you can simply use those new series recipes as though they were built into the plotting libraries. Let's use the Violin plot on some random data: ```@example tutorial -y = rand(100, 4) # Four series of 100 points each -violin(["Series 1" "Series 2" "Series 3" "Series 4"], y, leg = false) +y = rand(100, 4) +violin(["Series 1" "Series 2" "Series 3" "Series 4"], y, legend=false) ``` -and we can add a `boxplot` on top using the same mutation commands as before: +We can add a `boxplot` on top using the same mutation commands as before: ```@example tutorial -boxplot!(["Series 1" "Series 2" "Series 3" "Series 4"], y, leg = false) +boxplot!(["Series 1" "Series 2" "Series 3" "Series 4"], y, legend=false) ``` ## Additional Addons To Try From ae98dd07e8ae36b1e6aad1e226cf99642a0f49e1 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 01:11:41 -0800 Subject: [PATCH 06/28] Add LaTeXStrings subsection --- docs/src/tutorial.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index dc85e23c1e..80f4328e16 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -191,6 +191,29 @@ ylabel!("y") More information about attributes can be found in the Attributes section of the Manual. +### LaTeX Equation Strings + +Plots.jl works with LaTeXStrings.jl, a package that allows the user to type +LaTeX equations in string literals. To install this, type in +`Pkg.add("LaTeXStrings")`. The easiest way to use it is to prepend `L` to a +LaTeX-formatted string. Here's an example of it in use. Note that `*` denotes +string concatenation in Julia. + +```@example tutorial +using LaTeXStrings + +x = 10 .^ range(0, 4, length=100) +y = @. 1/(1+x) + +plot(x, y, label=L"\frac{1}{1+x}") +plot!(xscale=:log10, yscale=:log10, minorgrid=true) +xlims!(1e+0, 1e+4) +ylims!(1e-5, 1e+0) +title!("Log-log plot of " * L"\frac{1}{1+x}") +xlabel!(L"x") +ylabel!(L"y") +``` + ## Changing the Plotting Series At this point you know about line plots, but don't you want to plot your data From 09be420170383ce264c290b8aa63a1f527b623a3 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 02:51:00 -0800 Subject: [PATCH 07/28] Update docs/src/tutorial.md Co-authored-by: t-bltg --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 80f4328e16..ab5ce10a58 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -20,7 +20,7 @@ Depending on your computer, this will take a few seconds: using Plots ``` -To start, let's plot some trig functions. For the `x` coordinates, we can +To start, let's plot some trigonometric functions. For the `x` coordinates, we can create a range from 0 to 10 of, say, 100 elements. For the `y` coordinates, we can create a vector by evaluating `sin(x)` in an element-wise fashion. To do this in Julia, we insert a dot right after the function call. Finally, we use `plot()` From a6d363cc0be7e73e66f8017437cde11507f18271 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 02:51:43 -0800 Subject: [PATCH 08/28] Update docs/src/tutorial.md Co-authored-by: t-bltg --- docs/src/tutorial.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index ab5ce10a58..c790a3c106 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -273,8 +273,7 @@ ylabel!("y") ## [Plotting Backends](@id plotting-backends) -Here's a secret: Plots.jl isn't actually a plotting package! Plots.jl is a -plotting metapackage: it's an interface over many different plotting libraries. +Plots.jl is a plotting metapackage: it's an interface over many different plotting libraries. What Plots.jl is actually doing is interpreting your commands and then generating the plots using another plotting library, called the **backend**. The nice thing about this is that you can use many different plotting libraries From 03673c43928396a67a7b8ec06d1c85bd7db6245f Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 02:51:52 -0800 Subject: [PATCH 09/28] Update docs/src/tutorial.md Co-authored-by: t-bltg --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index c790a3c106..8ff32a2146 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -333,7 +333,7 @@ For examples of plots from the various backends, see the Examples section. At the start of the tutorial, we recommended following along the code examples in an interactive session for the following reason: try adding those same plotting commands to a script. Now call the script... and the plot doesn't -show up? This is because Julia in interactive use calls `display` on every +show up? This is because Julia in interactive use through the REPL calls `display` on every variable that is returned by a command without a semicolon `;`. In each case above, the interactive usage was automatically calling `display` on the returned plot objects. From 3c2794a4902c4317c408a5b6e213edc3082e8479 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 02:52:05 -0800 Subject: [PATCH 10/28] Update docs/src/tutorial.md Co-authored-by: t-bltg --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 8ff32a2146..dade4e357f 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -474,7 +474,7 @@ requires no more intervention! ### Using Plot Recipes StatsPlots.jl adds the `marginhist` multiplot via a plot recipe. For our data, -we'll pull in the famous `iris` dataset from RDatasets: +we will pull in the famous `iris` dataset from RDatasets: ```@example tutorial # Pkg.add("RDatasets") From 2f937150c915a4130defc62c2057bd83fd329e23 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 02:55:40 -0800 Subject: [PATCH 11/28] Update docs/src/tutorial.md Co-authored-by: t-bltg --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index dade4e357f..a344e98880 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -117,7 +117,7 @@ plot: x = range(0, 10, length=100) y1 = sin.(x) y2 = cos.(x) -plot(x, [y1 y2], title="Trig functions", label=["sin(x)" "cos(x)"], linewidth=3) +plot(x, [y1 y2], title="Trigonometric functions", label=["sin(x)" "cos(x)"], linewidth=3) ``` Every attribute can also be applied by mutating the plot with a From c9b1a20d08a54f36a1a1a1be811a055f6ac8a0dd Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 02:55:47 -0800 Subject: [PATCH 12/28] Update docs/src/tutorial.md Co-authored-by: t-bltg --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index a344e98880..f1e38ed93b 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -163,7 +163,7 @@ plot(x, [y1 y2], label=["sin(x)" "cos(x)"], lw=[2 1]) plot!(x, y3, label="sin(x)^2 - 1/2", lw=3, ls=:dot) plot!(legend=:outerbottom, legendcolumns=3) xlims!(0, 2pi) -title!("Trig functions") +title!("Trigonometric functions") xlabel!("x") ylabel!("y") ``` From e154865ee63c0d20b095bfa87519d57dc9782aa8 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 03:04:27 -0800 Subject: [PATCH 13/28] Add Plots.png, Plots.pdf mention --- docs/src/tutorial.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index f1e38ed93b..bd3053bf03 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -88,8 +88,16 @@ In cases where the plot variable is omitted, Plots.jl uses the global Saving plots is done by the `savefig` command. For example: ```julia -savefig("myplot.png") # Saves the CURRENT_PLOT as a .png -savefig(p, "myplot.pdf") # Saves the plot from p as a .pdf vector graphic +savefig("myplot.png") # saves the CURRENT_PLOT as a .png +savefig(p, "myplot.pdf") # saves the plot from p as a .pdf vector graphic +``` + +There also exist convenience functions `png` and `pdf`. With these, the extension +is omitted from the filename. The following is equivalent to the above code: + +```julia +png("myplot") +pdf(p, "myplot") ``` ## Plot Attributes From 848feacda6ce7c02b6ab254b0d3af2ea17ffdbe3 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 03:25:17 -0800 Subject: [PATCH 14/28] Update docs/src/tutorial.md Co-authored-by: t-bltg --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index bd3053bf03..f24b01df3b 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -92,7 +92,7 @@ savefig("myplot.png") # saves the CURRENT_PLOT as a .png savefig(p, "myplot.pdf") # saves the plot from p as a .pdf vector graphic ``` -There also exist convenience functions `png` and `pdf`. With these, the extension +There also exist convenience functions `png`, `Plots.pdf` and other unexported helpers. With these, the extension is omitted from the filename. The following is equivalent to the above code: ```julia From c658cdb668f66bbcc42a7b87f82a178a13c4dedb Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 03:26:53 -0800 Subject: [PATCH 15/28] Update tutorial.md --- docs/src/tutorial.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index f24b01df3b..f5b08ab9d2 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -92,8 +92,9 @@ savefig("myplot.png") # saves the CURRENT_PLOT as a .png savefig(p, "myplot.pdf") # saves the plot from p as a .pdf vector graphic ``` -There also exist convenience functions `png`, `Plots.pdf` and other unexported helpers. With these, the extension -is omitted from the filename. The following is equivalent to the above code: +There also exist convenience functions `Plots.png`, `Plots.pdf` and other +unexported helpers. With these, the extension is omitted from the filename. +The following is equivalent to the above code: ```julia png("myplot") From a525c09743c928d0bff3e0871ed4047d20a26a18 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 03:37:27 -0800 Subject: [PATCH 16/28] Update docs/src/tutorial.md Co-authored-by: t-bltg --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index f5b08ab9d2..c96367eb80 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -36,7 +36,7 @@ The plot is displayed in a plot pane, a stand-alone window or the browser, depending on the environment and backend (see [below](@ref plotting-backends)). If this is your first plot of the session and it takes a while to show up, -this is normal; this latency is called the "time to first plot" problem, +this is normal; this latency is called the "time to first plot" problem (or `TTFP`), and subsequent plots will be fast. Because of the way Julia works under the hood, this is a difficult problem to solve, but much progress has been made in the past few years to reduce this compilation time. From a96fd6f59ecebd15d2f11acbf2147e387851fd93 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 11:42:16 -0800 Subject: [PATCH 17/28] Update tutorial.md --- docs/src/tutorial.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index f5b08ab9d2..322a46be72 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -223,7 +223,7 @@ xlabel!(L"x") ylabel!(L"y") ``` -## Changing the Plotting Series +## Changing Series Type: Scatter Plots At this point you know about line plots, but don't you want to plot your data in other ways? In Plots.jl, these other ways of plotting a series is called a @@ -245,8 +245,8 @@ plot!(x, y_noisy, seriestype=:scatter, label="data") For each built-in series type, there is a shorthand function for directly calling that series type which matches the name of the series type. It handles -attributes just the same as the `plot` command, and have a mutating form which -ends in `!`. For example, we can instead write the last line as: +attributes just the same as the `plot` command, and it has a mutating form which +ends in `!`. For example, we can write the last line as: ```julia scatter!(x, y_noisy, label="data") From dec035831f64ad8f1dad50a739095e33add2c11a Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sat, 19 Nov 2022 12:34:23 -0800 Subject: [PATCH 18/28] Better wording --- docs/src/tutorial.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index f5de2aa553..ab7ce1d06d 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -133,8 +133,8 @@ Every attribute can also be applied by mutating the plot with a modifier function. Some attributes have their own dedicated modifier functions, while others can be accessed through `plot!(attribute=value)`. For example, the `xlabel` attribute adds a label for the -x-axis. We can specify it in the plot command with `xlabel=...` like we did -above, or we can use the modifier function to add it after the plot has already +x-axis. We can specify it in the plot command with `xlabel=...`, +or we can use the modifier function to add it after the plot has already been generated. It's up to you to decide which is better for code readability. ```julia @@ -148,11 +148,9 @@ of the plot object that wants to be modified. Let's use keywords and modifier functions interchangeably to perform some common modifications to our example, listed below. You'll notice that for the -attributes `ls` and `legend`, a colon `:` is inserted before the name. +attributes `ls` and `legend`, the values include a colon `:`. The colon denotes a symbol in Julia. They are commonly used for values of -attributes in Plots.jl, along with strings and numbers. Another thing to note -is that `y3` is being plotted as a dotted line. This is distinct from a -scatter plot of the data. +attributes in Plots.jl, along with strings and numbers. * Labels for the individual lines, seen in the legend * Line widths (we'll use the alias `lw` instead of `linewidth`) @@ -177,6 +175,9 @@ xlabel!("x") ylabel!("y") ``` +Note that `y3` is being plotted as a dotted line. This is distinct from a +scatter plot of the data. + ### Logarithmic Scale Plots Sometimes data needs to be plotted across orders of magnitude. The attributes @@ -244,7 +245,7 @@ plot!(x, y_noisy, seriestype=:scatter, label="data") ``` For each built-in series type, there is a shorthand function for directly -calling that series type which matches the name of the series type. It handles +calling that series type which matches its name. It handles attributes just the same as the `plot` command, and it has a mutating form which ends in `!`. For example, we can write the last line as: @@ -403,7 +404,7 @@ p4 = scatter(x, y, title="Title 4", ms=2, ma=0.2) plot(p1, p2, p3, p4, layout=(2,2), legend=false) ``` -Notice that the attributes in the individual plots are applied to the +Notice that the attributes in the individual plots are applied to those individual plots, while the attribute `legend=false` in the final `plot` call is applied to all of the subplots. From 1029e06ad2a5530af904f8811ca477bc229aacee Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sun, 20 Nov 2022 17:32:09 -0800 Subject: [PATCH 19/28] Unneeded line --- docs/src/tutorial.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index ab7ce1d06d..0c57331370 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -382,7 +382,6 @@ x = range(0, 10, length=100) y1 = @. exp(-0.1x) * cos(4x) y2 = @. exp(-0.3x) * cos(4x) y3 = @. exp(-0.5x) * cos(4x) -y = [y1 y2 y3] plot(x, [y1 y2 y3], layout=(3, 1), legend=false) ``` From 67d13cd1f00fcbecf0c5f470dc972347f7f48dcf Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sun, 20 Nov 2022 23:07:45 -0800 Subject: [PATCH 20/28] Better use of LaTeXStrings --- docs/src/tutorial.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 0c57331370..889964e10c 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -206,8 +206,8 @@ of the Manual. Plots.jl works with LaTeXStrings.jl, a package that allows the user to type LaTeX equations in string literals. To install this, type in `Pkg.add("LaTeXStrings")`. The easiest way to use it is to prepend `L` to a -LaTeX-formatted string. Here's an example of it in use. Note that `*` denotes -string concatenation in Julia. +LaTeX-formatted string. If the string is a mix between normal text and LaTeX +equations, insert dollar signs `$` as needed. ```@example tutorial using LaTeXStrings @@ -219,7 +219,7 @@ plot(x, y, label=L"\frac{1}{1+x}") plot!(xscale=:log10, yscale=:log10, minorgrid=true) xlims!(1e+0, 1e+4) ylims!(1e-5, 1e+0) -title!("Log-log plot of " * L"\frac{1}{1+x}") +title!(L"Log-log plot of $\frac{1}{1+x}$") xlabel!(L"x") ylabel!(L"y") ``` From 3d488706d790fb2001de70074c9fc6299d91dca3 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Sun, 20 Nov 2022 23:36:15 -0800 Subject: [PATCH 21/28] Update tutorial.md --- docs/src/tutorial.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 889964e10c..9c486c567a 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -92,13 +92,13 @@ savefig("myplot.png") # saves the CURRENT_PLOT as a .png savefig(p, "myplot.pdf") # saves the plot from p as a .pdf vector graphic ``` -There also exist convenience functions `Plots.png`, `Plots.pdf` and other +There also exist convenience functions `png`, `Plots.pdf` and other unexported helpers. With these, the extension is omitted from the filename. The following is equivalent to the above code: ```julia png("myplot") -pdf(p, "myplot") +Plots.pdf(p, "myplot") ``` ## Plot Attributes From 2c3533acf92582200f60a745c95c3992972ac319 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:17:11 -0800 Subject: [PATCH 22/28] Update docs/src/tutorial.md Co-authored-by: Simon Christ --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 9c486c567a..62bae211a1 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -13,7 +13,7 @@ or an interactive notebook. ## Basic Plotting: Line Plots -After you have installed Plots.jl, the first step is to initialize the package. +After you have installed Plots.jl via `Pkg.add("Plots")`, the first step is to initialize the package. Depending on your computer, this will take a few seconds: ```@example tutorial From 2838d23cf2792cbe031f158abb6a0f8439d622b0 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:17:21 -0800 Subject: [PATCH 23/28] Update docs/src/tutorial.md Co-authored-by: Simon Christ --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 62bae211a1..dc722d6152 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -433,7 +433,7 @@ on are: 2. It adds a plot recipe for marginal histograms. 3. It adds a bunch of new statistical plot series. -Besides recipes, StatsPlots.jl also provides a specialized macro from plotting +Besides recipes, StatsPlots.jl also provides a specialized macro `@df` from plotting directly from data tables. ### Using User Recipes From cac2db05b0a0cec73e7f8156ae695e6378722300 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:17:34 -0800 Subject: [PATCH 24/28] Update docs/src/tutorial.md Co-authored-by: Simon Christ --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index dc722d6152..c2f8151b30 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -309,7 +309,7 @@ x = range(0, 10, length=100) y = sin.(x) y_noisy = @. sin(x) + 0.1*randn() -# this plots into the web browser via Plotly +# this plots into a standalone window via Plotly plot(x, y, label="sin(x)", lc=:black, lw=2) scatter!(x, y_noisy, label="data", mc=:red, ms=2, ma=0.5) plot!(legend=:bottomleft) From dde7b70ffa41723e270344658f5704878deff2fb Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:24:23 -0800 Subject: [PATCH 25/28] Update docs/src/tutorial.md Co-authored-by: Simon Christ --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index c2f8151b30..4b07c1c0ff 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -233,7 +233,7 @@ series type which is commonly used. Let's start with the sine function again, but this type, we'll define a vector called `y_noisy` that adds some randomness. -We can change the series type by the `seriestype` attribute. +We can change the series type using the `seriestype` attribute. ```@example tutorial x = range(0, 10, length=100) From 65c24a6acc2ce7a546d255951cffa39103192da6 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:24:35 -0800 Subject: [PATCH 26/28] Update docs/src/tutorial.md Co-authored-by: Simon Christ --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 4b07c1c0ff..fe4f85d3f6 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -231,7 +231,7 @@ in other ways? In Plots.jl, these other ways of plotting a series is called a **series type**. A line is one series type. However, a scatter plot is another series type which is commonly used. -Let's start with the sine function again, but this type, we'll define a vector +Let's start with the sine function again, but this time, we'll define a vector called `y_noisy` that adds some randomness. We can change the series type using the `seriestype` attribute. From e463923b01c819c06062e0a5916b6b75c0729034 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:28:30 -0800 Subject: [PATCH 27/28] Update tutorial.md --- docs/src/tutorial.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index c2f8151b30..02a2ae3b93 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -8,13 +8,13 @@ Plots.reset_defaults() This is a guide for getting you up and running with Plots.jl. Its main goal is to introduce you to the terminology used in the package, how to use Plots.jl in common use cases, and put you in a position to easily understand the rest of -the manual. We recommend you follow along the code examples inside the REPL -or an interactive notebook. +the manual. It is recommended that the that the code examples be followed inside +the REPL or an interactive notebook. ## Basic Plotting: Line Plots -After you have installed Plots.jl via `Pkg.add("Plots")`, the first step is to initialize the package. -Depending on your computer, this will take a few seconds: +After you have installed Plots.jl via `Pkg.add("Plots")`, the first step is to +initialize the package. Depending on your computer, this will take a few seconds: ```@example tutorial using Plots @@ -101,6 +101,9 @@ png("myplot") Plots.pdf(p, "myplot") ``` +More information about outputting figures can be found in the +[Output](@ref output) section of the Manual. + ## Plot Attributes In the previous section we made plots... we're done, right? No! We need to style @@ -134,7 +137,7 @@ modifier function. Some attributes have their own dedicated modifier functions, while others can be accessed through `plot!(attribute=value)`. For example, the `xlabel` attribute adds a label for the x-axis. We can specify it in the plot command with `xlabel=...`, -or we can use the modifier function to add it after the plot has already +or we can use the modifier function below to add it after the plot has already been generated. It's up to you to decide which is better for code readability. ```julia @@ -198,8 +201,8 @@ xlabel!("x") ylabel!("y") ``` -More information about attributes can be found in the Attributes section -of the Manual. +More information about attributes can be found in the +[Attributes](@ref attributes) section of the Manual. ### LaTeX Equation Strings @@ -403,7 +406,7 @@ p4 = scatter(x, y, title="Title 4", ms=2, ma=0.2) plot(p1, p2, p3, p4, layout=(2,2), legend=false) ``` -Notice that the attributes in the individual plots are applied to those +Note that the attributes in the individual plots are applied to those individual plots, while the attribute `legend=false` in the final `plot` call is applied to all of the subplots. From cfb08d405f526cbfda76876cbc4c951b976bb345 Mon Sep 17 00:00:00 2001 From: Andrew J <42456097+Ininterrompue@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:30:19 -0800 Subject: [PATCH 28/28] Update tutorial.md --- docs/src/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorial.md b/docs/src/tutorial.md index 7aab9e983b..266c05d887 100644 --- a/docs/src/tutorial.md +++ b/docs/src/tutorial.md @@ -8,7 +8,7 @@ Plots.reset_defaults() This is a guide for getting you up and running with Plots.jl. Its main goal is to introduce you to the terminology used in the package, how to use Plots.jl in common use cases, and put you in a position to easily understand the rest of -the manual. It is recommended that the that the code examples be followed inside +the manual. It is recommended that the code examples be followed inside the REPL or an interactive notebook. ## Basic Plotting: Line Plots