english improved
This commit is contained in:
@@ -12,25 +12,25 @@ using InteractiveUtils
|
||||
|
||||
# Plots and Data Visualization in Julia: _Plots.jl_
|
||||
|
||||
There are numerous graphics packages for Julia. Two frequently used ones are [Makie.jl](https://docs.makie.org/stable/) and
|
||||
[Plots.jl](https://docs.juliaplots.org/latest/). Before presenting these in more detail, some other packages are listed.
|
||||
Julia has numerous graphics packages. Two frequently used ones are [Makie.jl](https://docs.makie.org/stable/) and
|
||||
[Plots.jl](https://docs.juliaplots.org/latest/). Before presenting `Plots.jl` in detail, we list some others.
|
||||
|
||||
## Brief Overview: Some Graphics Packages
|
||||
|
||||
| Package/Documentation | Tutorial | Examples | Remarks |
|
||||
|:----|:--|:--|:--------|
|
||||
|[Plots.jl](https://docs.juliaplots.org/latest/) | [Tutorial](https://docs.juliaplots.org/latest/tutorial/) | [Gallery](https://goropikari.github.io/PlotsGallery.jl/) | designed as a unified interface to various _backends_ (graphics libraries) |
|
||||
|[Plots.jl](https://docs.juliaplots.org/latest/) | [Tutorial](https://docs.juliaplots.org/latest/tutorial/) | [Gallery](https://docs.juliaplots.org/latest/gallery/gr/) | designed as a unified interface to various _backends_ (graphics libraries) |
|
||||
| [Makie.jl](https://docs.makie.org/stable/) | [Basic tutorial](https://docs.makie.org/v0.21/tutorials/basic-tutorial) | [Beautiful Makie](https://beautiful.makie.org/) | "data visualization ecosystem for Julia", backends: Cairo (vector graphics), OpenGL, WebGL |
|
||||
|[PlotlyJS.jl](http://juliaplots.org/PlotlyJS.jl/stable/) | [Getting started](https://plotly.com/julia/getting-started/)| [Examples](https://plotly.com/julia/plotly-fundamentals/)| Interface to the [Plotly](https://plotly.com/graphing-libraries/) JavaScript graphics library |
|
||||
| [Gadfly.jl](https://gadflyjl.org/stable/)| [Tutorial](https://gadflyjl.org/stable/tutorial/) | [Gallery](https://github.com/GiovineItalia/Gadfly.jl?tab=readme-ov-file#gallery)| "a plotting and data visualization system written in Julia, influenced by R's [ggplot2](https://ggplot2.tidyverse.org/)" |
|
||||
| [Bokeh.jl](https://cjdoris.github.io/Bokeh.jl/stable/) | | [Gallery](https://cjdoris.github.io/Bokeh.jl/stable/gallery/)| Julia frontend for [Bokeh](https://bokeh.org/) |
|
||||
|[VegaLite.jl](https://www.queryverse.org/VegaLite.jl/stable/) | [Tutorial](https://www.queryverse.org/VegaLite.jl/stable/gettingstarted/tutorial/)| [Examples](https://www.queryverse.org/VegaLite.jl/stable/examples/examples_barcharts/)| Julia frontend for [Vega-Lite](https://vega.github.io/vega-lite/)|
|
||||
| [Luxor.jl](http://juliagraphics.github.io/Luxor.jl/stable/) |[Tutorial](https://juliagraphics.github.io/Luxor.jl/stable/tutorial/helloworld/)|[Examples](https://juliagraphics.github.io/Luxor.jl/stable/example/moreexamples/)| General vector graphics/illustrations |
|
||||
| [Luxor.jl](https://juliagraphics.github.io/LuxorManual/stable/) |[Tutorial](https://juliagraphics.github.io/LuxorManual/stable/tutorial/helloworld/)|[Examples](https://juliagraphics.github.io/LuxorManual/stable/example/moreexamples/)| General vector graphics/illustrations |
|
||||
| [Javis.jl](https://juliaanimators.github.io/Javis.jl/stable/) |[Tutorials](https://juliaanimators.github.io/Javis.jl/stable/tutorials/)| [Examples](https://juliaanimators.github.io/Javis.jl/stable/examples/)| *Animated* vector graphics
|
||||
| [TidierPlots.jl](https://github.com/TidierOrg/TidierPlots.jl)| [Reference](https://tidierorg.github.io/TidierPlots.jl/latest/) || "is a 100% Julia implementation of the R package ggplot2 powered by Makie.jl"|
|
||||
|[PythonPlot.jl](https://github.com/JuliaPy/PythonPlot.jl)| |[Examples (in Python)](https://matplotlib.org/stable/gallery/index.html)| Interface to Matplotlib (Python), 1:1 transfer of the Python API, therefore see [Matplotlib documentation](https://matplotlib.org/stable/api/pyplot_summary.html)
|
||||
|
||||
{: .striped .hover}
|
||||
: {.striped .hover}
|
||||
|
||||
<!--
|
||||
| [PyPlot.jl](https://github.com/JuliaPy/PyPlot.jl) | | [Examples](https://gist.github.com/gizmaa/7214002)| Interface to Matplotlib (Python), 1:1 transfer of the Python API, therefore see [Matplotlib documentation](https://matplotlib.org/stable/) |
|
||||
@@ -44,7 +44,7 @@ The `plot()` function expects, in the simplest case:
|
||||
|
||||
- as the first argument a vector of $x$-values of length $n$ and
|
||||
- as the second argument a vector of the same length with the corresponding $y$-values.
|
||||
- The second argument can also be an $n\times m$-matrix. Then each column vector is regarded as a separate graph (called `series` in the documentation) and $m$ curves are plotted:
|
||||
- The second argument can also be an $n\times m$ matrix. Each column is treated as a separate graph (called a `series` in the documentation), plotting $m$ curves:
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -57,8 +57,8 @@ cx = @. cos(2x^(1/2))
|
||||
plot(x, [sx cx])
|
||||
```
|
||||
|
||||
- The functions of the _Plots.jl_ package such as `plot(), scatter(), contour(), heatmap(), histogram(), bar(),...` etc. all start a new plot.
|
||||
- The versions `plot!(), scatter!(), contour!(), heatmap!(), histogram!(), bar!(),...` extend an existing plot:
|
||||
- Functions like `plot()`, `scatter()`, `contour()`, `heatmap()`, `histogram()`, `bar()`, etc. from _Plots.jl_ all start a new plot.
|
||||
- The versions `plot!()`, `scatter!()`, `contour!()`, `heatmap!()`, `histogram!()`, `bar!()`, etc. extend an existing plot:
|
||||
|
||||
```{julia}
|
||||
plot(x, sx) # plot only sin(x)
|
||||
@@ -74,7 +74,7 @@ plot1a = deepcopy(plot1) # plot objects are quite deep structures
|
||||
scatter!(plot1, x, sx) # add scatter plot, i.e. unconnected data points
|
||||
```
|
||||
|
||||
The copied version `plot1a` has not been modified by the `scatter!` statement and can be used independently:
|
||||
The copied version `plot1a` remains unchanged by the `scatter!` call and can be used independently:
|
||||
|
||||
```{julia}
|
||||
plot!(plot1a, x, 2 .* sx)
|
||||
@@ -82,7 +82,7 @@ plot!(plot1a, x, 2 .* sx)
|
||||
|
||||
|
||||
|
||||
Plot objects can be saved as graphics files (PDF, SVG, PNG,...):
|
||||
Plot objects can be saved as graphics files (PDF, SVG, PNG, etc.):
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -111,7 +111,7 @@ plot(f, 0:0.01:3)
|
||||
```
|
||||
|
||||
|
||||
The parametric form $x = x(t),\ y = y(t)$ can be drawn by passing two functions and a vector of $t$-values to `plot()`.
|
||||
The parametric form $x = x(t),\ y = y(t)$ is plotted by passing two functions and a vector of $t$-values to `plot()`.
|
||||
|
||||
```{julia}
|
||||
# https://en.wikipedia.org/wiki/Butterfly_curve_(transcendental)
|
||||
@@ -125,10 +125,9 @@ plot(xt, yt, 0:0.01:12π)
|
||||
|
||||
### Plot Themes
|
||||
|
||||
> "PlotThemes is a package to spice up the plots made with Plots.jl."\
|
||||
Here is the illustrated [list of themes](https://docs.juliaplots.org/stable/generated/plotthemes/)
|
||||
> "PlotThemes is a package to spice up plots made with Plots.jl."\
|
||||
See the illustrated [list of themes](https://docs.juliaplots.org/stable/generated/plotthemes/)
|
||||
|
||||
or:
|
||||
```{julia}
|
||||
using PlotThemes
|
||||
|
||||
@@ -151,8 +150,8 @@ plot(x, [sx cx 1 ./ (1 .+ x)])
|
||||
|
||||
### Plot Attributes
|
||||
|
||||
The functions of the `Plots.jl` package have a large number of options.
|
||||
`Plots.jl` divides the attributes into 4 groups:
|
||||
The `Plots.jl` functions have numerous options.
|
||||
Attributes are divided into 4 groups:
|
||||
|
||||
::::{.cell}
|
||||
```{julia}
|
||||
@@ -182,7 +181,7 @@ plotattr(:Series) # attributes for a series, e.g., a line in the plot
|
||||
```
|
||||
::::
|
||||
|
||||
One can also ask what the individual attributes mean and which values are allowed:
|
||||
You can also query what individual attributes mean and which values are allowed:
|
||||
```{julia}
|
||||
plotattr("linestyle")
|
||||
```
|
||||
@@ -200,7 +199,7 @@ plot(x, y, seriestype = :sticks, linewidth = 4, seriescolor = "#00b300",
|
||||
)
|
||||
```
|
||||
|
||||
Many specifications can also be abbreviated significantly, see e.g. the `Aliases:` in the above output of the command `plotattr("linestyle")`.
|
||||
Many specifications can be abbreviated significantly; see, e.g., the `Aliases:` in the output of `plotattr("linestyle")`.
|
||||
|
||||
The following `plot()` command is equivalent to the previous one:
|
||||
|
||||
@@ -213,13 +212,13 @@ plot(x, y, t = :sticks, w = 4, c = "#00b300", m = (:circle, 8, :green ))
|
||||
### Additional Extras
|
||||
|
||||
```{julia}
|
||||
using Plots # repetition does not hurt
|
||||
using Plots # no harm in repeating
|
||||
using Plots.PlotMeasures # for measurements in mm, cm,...
|
||||
using LaTeXStrings # for LaTeX constructs in plot labels
|
||||
using PlotThemes # predefined themes
|
||||
```
|
||||
|
||||
The `LaTeXStrings.jl` package provides a string constructor `L"..."`. These strings can contain LaTeX constructs, especially formulas. If the string does not contain explicit dollar signs, it is automatically interpreted in LaTeX math mode.
|
||||
The `LaTeXStrings.jl` package provides the `L"..."` string constructor. These strings can contain LaTeX constructs, especially formulas. Without explicit dollar signs, they are automatically interpreted in LaTeX math mode.
|
||||
|
||||
```{julia}
|
||||
xs = range(0, 2π, length = 100)
|
||||
@@ -232,9 +231,9 @@ plot10 = plot(xs, data,
|
||||
fontfamily="Computer Modern",
|
||||
|
||||
# LaTeX string L"..."
|
||||
title = L"Winkelfunktionen $\sin(\alpha), \cos(\alpha), 2\sin(\alpha), \sin(\alpha^2)$",
|
||||
xlabel = L"Winkel $\alpha$",
|
||||
ylabel = "Funktionswert",
|
||||
title = L"Trigonometric functions $\sin(\alpha), \cos(\alpha), 2\sin(\alpha), \sin(\alpha^2)$",
|
||||
xlabel = L"angle $\alpha$",
|
||||
ylabel = "value",
|
||||
|
||||
# 1x4-matrices with colors, markers,... for the 4 'series'
|
||||
color=[:black :green RGB(0.3, 0.8, 0.2) :blue ],
|
||||
@@ -255,14 +254,14 @@ plot10 = plot(xs, data,
|
||||
top_margin = 5mm, # here Plots.PlotMeasures is needed
|
||||
)
|
||||
|
||||
# additional text: annotate!(x-pos, y-pos, text("...", font, fontsize))
|
||||
# additional text: annotate!(x-pos, y-pos, text("...", font, fontsize))
|
||||
|
||||
annotate!(plot10, 4.1, 1.8, text("nicht schön, aber viel","Computer Modern", 10) )
|
||||
annotate!(plot10, 4.1, 1.8, text("Some remark","Computer Modern", 10) )
|
||||
```
|
||||
|
||||
### Other Plot Functions
|
||||
|
||||
So far, we have plotted mainly lines. There are many other types such as _scatter plot, contour, heatmap, histogram, stick,..._
|
||||
So far, we have mainly plotted lines. Many other types exist, such as _scatter plots, contours, heatmaps, histograms, sticks_, etc.
|
||||
|
||||
This can be controlled with the `seriestype` attribute:
|
||||
|
||||
@@ -283,7 +282,7 @@ scatter(x, sin.(x))
|
||||
|
||||
### Subplots and Layout {#sec-subplot}
|
||||
|
||||
Multiple plots can be combined into one figure. The arrangement is determined by the `layout` parameter. `layout=(m,n)` means that the plots are arranged in an $m\times n$ scheme:
|
||||
Multiple plots can be combined into one figure. The arrangement is determined by the `layout` parameter: `layout=(m,n)` arranges plots in an $m\times n$ grid:
|
||||
|
||||
```{julia}
|
||||
x = range(0, 2π; length = 100)
|
||||
@@ -317,11 +316,11 @@ plot(plots..., layout=mylayout, legend=false, title=["sin" "cos" "tan" "sinc"])
|
||||
|
||||
### Backends
|
||||
|
||||
`Plots.jl` is designed as a unified interface to various _backends_ (graphics engines). One can switch to another backend and use the same plot commands and attributes.
|
||||
`Plots.jl` provides a unified interface to various _backends_ (graphics engines). You can switch backends and use the same plot commands and attributes.
|
||||
|
||||
However, not all _backends_ support all plot types and attributes. An overview is available [here](https://docs.juliaplots.org/stable/generated/supported/).
|
||||
|
||||
So far, the default backend has been used. It is called [GR](https://gr-framework.org/about.html) and is a graphics engine developed at the Jülich Research Center and written primarily in C.
|
||||
So far, we have used the default backend: [GR](https://gr-framework.org/about.html), a graphics engine developed at the Jülich Research Center, primarily in C.
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -330,22 +329,22 @@ backend() # display the selected backend, GR is the default
|
||||
```
|
||||
|
||||
|
||||
Another example
|
||||
Another example:
|
||||
```{julia}
|
||||
x = 1:30
|
||||
y = rand(30)
|
||||
plot(x, y, linecolor =:green, bg_inside =:lightblue1, line =:solid, label = "Wasserstand")
|
||||
plot(x, y, linecolor =:green, bg_inside =:lightblue1, line =:solid, label = "Water level")
|
||||
```
|
||||
|
||||
and here the same plot with the `PlotlyJS` backend.
|
||||
The same plot with the `PlotlyJS` backend:
|
||||
|
||||
|
||||
```{julia}
|
||||
plotlyjs() # change plots backend
|
||||
plot(x, y, linecolor =:green, bg_inside =:lightblue1, line =:solid, label = "Wasserstand")
|
||||
plot(x, y, linecolor =:green, bg_inside =:lightblue1, line =:solid, label = "Water level")
|
||||
```
|
||||
|
||||
This backend enables a certain interactivity using JavaScript. When moving the mouse into the image, one can zoom and pan with the mouse, and 3D plots can also be rotated.
|
||||
This backend enables interactivity via JavaScript. Hovering over the image allows zooming and panning; 3D plots can also be rotated.
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -356,7 +355,7 @@ gr() # return to GR as backend
|
||||
|
||||
### 3D Plots
|
||||
|
||||
The functions `surface()` and `contour()` allow plotting of a function $f(x,y)$. The required arguments are:
|
||||
The `surface()` and `contour()` functions plot a function $f(x,y)$. Required arguments are:
|
||||
|
||||
- a set (vector) $X$ of $x$-values,
|
||||
- a set (vector) $Y$ of $y$-values and
|
||||
@@ -372,8 +371,8 @@ surface( -3:0.02:3, -3:0.02:3, f)
|
||||
contour( -3:0.02:3, -3:0.02:3, f, fill=true, colormap=:summer, levels=20, contour_labels=false)
|
||||
```
|
||||
|
||||
Curves (or simply point sets) in three dimensions can be plotted by calling `plot()` with 3 vectors
|
||||
containing the $x$, $y$ and $z$-coordinates of the data points, respectively.
|
||||
Curves (or point sets) in three dimensions are plotted by calling `plot()` with three vectors
|
||||
containing the $x$, $y$, and $z$ coordinates of the data points.
|
||||
|
||||
```{julia}
|
||||
plotlyjs()
|
||||
@@ -387,25 +386,23 @@ plot(x, y, z, zcolor=reverse(z), markersize=3, markershape= :circle,
|
||||
|
||||
```
|
||||
|
||||
> We use the `plotlyjs` backend here, so the plot is interactive and can be rotated and zoomed with the mouse.
|
||||
> The `plotlyjs` backend makes the plot interactive: it can be rotated and zoomed with the mouse.
|
||||
|
||||
### Plots.jl and _recipes_
|
||||
|
||||
Other packages can extend the capabilities of `Plots.jl` by defining so-called _recipes_ for special plots and data structures, see [https://docs.juliaplots.org/latest/ecosystem/](https://docs.juliaplots.org/latest/ecosystem/), e.g.:
|
||||
Other packages can extend `Plots.jl` by defining so-called _recipes_ for special plots and data structures (see [https://docs.juliaplots.org/latest/ecosystem/](https://docs.juliaplots.org/latest/ecosystem/)), e.g.:
|
||||
|
||||
- `StatsPlots.jl` direct plotting of _Dataframes_, special statistical plots, etc. or
|
||||
- `GraphRecipes.jl` [Plotting of graph structures](https://docs.juliaplots.org/latest/GraphRecipes/examples/)
|
||||
- `StatsPlots.jl`: direct plotting of _DataFrames_, special statistical plots, etc.
|
||||
- `GraphRecipes.jl`: [plotting of graph structures](https://docs.juliaplots.org/latest/GraphRecipes/examples/)
|
||||
|
||||
|
||||
### A Bar Chart
|
||||
|
||||
For the last example, we load a package that provides over 700 free (_"public domain"_) datasets, including, for example:
|
||||
For the last example, we load a package providing over 700 free (_"public domain"_) datasets, including:
|
||||
|
||||
- the passenger list of the _Titanic_,
|
||||
- fuel consumption data of American cars from the 70s or
|
||||
- historical exchange rates
|
||||
|
||||
provides:
|
||||
- the _Titanic_ passenger list,
|
||||
- fuel consumption data for American cars from the 70s, or
|
||||
- historical exchange rates:
|
||||
|
||||
```{julia}
|
||||
using RDatasets
|
||||
@@ -425,7 +422,7 @@ The dataset ["Motor Trend Car Road Tests"](https://rdrr.io/r/datasets/mtcars.htm
|
||||
cars = dataset("datasets", "mtcars")
|
||||
```
|
||||
|
||||
We only need the two columns `cars.Model` and `cars.MPG` for the plot, the fuel consumption in _miles per gallon_ (more means more economical!)
|
||||
We only need two columns for the plot: `cars.Model` and `cars.MPG`, the fuel consumption in _miles per gallon_ (higher is more economical).
|
||||
|
||||
```{julia}
|
||||
theme(:bright)
|
||||
@@ -445,8 +442,8 @@ bar(cars.Model, cars.MPG,
|
||||
|
||||
### What is Missing: Animation
|
||||
|
||||
Please refer to the [documentation](https://docs.juliaplots.org/latest/animations/) and only an example
|
||||
(from <https://www.juliafordatascience.com/animations-with-plots-jl/>) is given:
|
||||
See the [documentation](https://docs.juliaplots.org/latest/animations/); here is an example
|
||||
(from <https://www.juliafordatascience.com/animations-with-plots-jl/>):
|
||||
|
||||
```{julia}
|
||||
#| error: false
|
||||
@@ -465,7 +462,7 @@ gif(anim, fps=50)
|
||||
```
|
||||
|
||||
|
||||
:::: {.content-visible when-format="pdf"}
|
||||
:::: {.content-visible when-format="typst"}
|
||||
```{julia}
|
||||
#| echo: false
|
||||
Random.seed!(123)
|
||||
|
||||
Reference in New Issue
Block a user