91 lines
22 KiB
HTML
91 lines
22 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Basic usage · Gnuplot.jl</title><link href="https://fonts.googleapis.com/css?family=Lato|Roboto+Mono" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL=".."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../assets/documenter.js"></script><script src="../siteinfo.js"></script><script src="../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-dark.css" data-theme-name="documenter-dark"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../assets/themeswap.js"></script></head><body><div id="documenter"><nav class="docs-sidebar"><a class="docs-logo" href="../"><img src="../assets/logo.png" alt="Gnuplot.jl logo"/></a><div class="docs-package-name"><span class="docs-autofit">Gnuplot.jl</span></div><form class="docs-search" action="../search/"><input class="docs-search-query" id="documenter-search-query" name="q" type="text" placeholder="Search docs"/></form><ul class="docs-menu"><li><a class="tocitem" href="../">Home</a></li><li><a class="tocitem" href="../install/">Installation</a></li><li class="is-active"><a class="tocitem" href>Basic usage</a><ul class="internal"><li><a class="tocitem" href="#plots2d-1"><span>2D plots</span></a></li><li><a class="tocitem" href="#Keywords-for-common-commands-1"><span>Keywords for common commands</span></a></li><li><a class="tocitem" href="#Plot-images-1"><span>Plot images</span></a></li><li><a class="tocitem" href="#plots3d-1"><span>3D plots</span></a></li><li><a class="tocitem" href="#Palettes-and-line-types-1"><span>Palettes and line types</span></a></li><li><a class="tocitem" href="#Exporting-plots-to-files-1"><span>Exporting plots to files</span></a></li><li><a class="tocitem" href="#Gnuplot-scripts-1"><span>Gnuplot scripts</span></a></li></ul></li><li><a class="tocitem" href="../advanced/">Advanced usage</a></li><li><a class="tocitem" href="../style/">Style guide</a></li><li><a class="tocitem" href="../terminals/">Gnuplot terminals</a></li><li><a class="tocitem" href="../examples/">Examples</a></li><li><a class="tocitem" href="../api/">API</a></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href>Basic usage</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>Basic usage</a></li></ul></nav><div class="docs-right"><a class="docs-edit-link" href="https://github.com/gcalderone/Gnuplot.jl/blob/master/docs/src/basic.md" title="Edit on GitHub"><span class="docs-icon fab"></span><span class="docs-label is-hidden-touch">Edit on GitHub</span></a><a class="docs-settings-button fas fa-cog" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-sidebar-button fa fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a></div></header><article class="content" id="documenter-page"><h1 id="Basic-usage-1"><a class="docs-heading-anchor" href="#Basic-usage-1">Basic usage</a><a class="docs-heading-anchor-permalink" href="#Basic-usage-1" title="Permalink"></a></h1><p>The main purpose of the <strong>Gnuplot.jl</strong> package is to send data and commands to the underlying gnuplot process, in order to generate plots. Unlike other packages, however, the actual commands to plot, or the plot attributes, are not specified through function calls. This is what makes <strong>Gnuplot.jl</strong> <em>easy to learn and use</em>: there are no functions or keywords names to memorize<sup class="footnote-reference"><a id="citeref-1" href="#footnote-1">[1]</a></sup>.</p><p>The most important symbols exported by the package are the <a href="../api/#Gnuplot.@gp"><code>@gp</code></a> (for 2D plots) and <a href="../api/#Gnuplot.@gsp"><code>@gsp</code></a> (for 3D plots) macros, both accepting any number of arguments, and whose meaning is interpreted as follows:</p><ul><li><p>one, or a group of consecutive, array(s) build up a dataset. The different arrays are accessible as columns 1, 2, etc. from the gnuplot process. The number of required input arrays depends on the chosen plot style (see gnuplot documentation);</p></li><li><p>a string occurring before a dataset is interpreted as a gnuplot command (e.g. <code>set grid</code>);</p></li><li><p>a string occurring immediately after a dataset is interpreted as a <em>plot element</em> for the dataset, by which you can specify <code>using</code> clause, <code>with</code> clause, line styles, etc.;</p></li><li><p>the special symbol <code>:-</code>, whose meaning is to avoid starting a new plot (if given as first argument), or to avoid immediately running all commands to create the final plot (if given as last argument). Its purpose is to allow splitting one long statement into multiple (shorter) ones.</p></li></ul><p>The above list shows all the fundamental concepts to follow the examples presented below. The <a href="../api/#Gnuplot.@gp"><code>@gp</code></a> and <a href="../api/#Gnuplot.@gsp"><code>@gsp</code></a> macros also accepts further arguments, but their use will be discussed in <a href="../advanced/#Advanced-usage-1">Advanced usage</a>.</p><h2 id="plots2d-1"><a class="docs-heading-anchor" href="#plots2d-1">2D plots</a><a class="docs-heading-anchor-permalink" href="#plots2d-1" title="Permalink"></a></h2><p>Here we will show a few examples to generate 2D plots. The examples are intentionally very simple to highlight the behavior of <strong>Gnuplot.jl</strong>. See <a href="../examples/#Examples-1">Examples</a> for more complex ones.</p><p>Remember to run:</p><pre><code class="language-julia">using Gnuplot</code></pre><p>before running the examples.</p><h3 id="Simple-examples-involving-just-gnuplot-commands:-1"><a class="docs-heading-anchor" href="#Simple-examples-involving-just-gnuplot-commands:-1">Simple examples involving just gnuplot commands:</a><a class="docs-heading-anchor-permalink" href="#Simple-examples-involving-just-gnuplot-commands:-1" title="Permalink"></a></h3><hr/><h4 id="Plot-a-sinusoid:-1"><a class="docs-heading-anchor" href="#Plot-a-sinusoid:-1">Plot a sinusoid:</a><a class="docs-heading-anchor-permalink" href="#Plot-a-sinusoid:-1" title="Permalink"></a></h4><pre><code class="language-julia">@gp "plot sin(x)"</code></pre><p><img src="../assets/ex001.png" alt/></p><hr/><h4 id="Plot-two-curves:-1"><a class="docs-heading-anchor" href="#Plot-two-curves:-1">Plot two curves:</a><a class="docs-heading-anchor-permalink" href="#Plot-two-curves:-1" title="Permalink"></a></h4><pre><code class="language-julia">@gp "set key left" "plot sin(x)" "pl cos(x)"</code></pre><p><img src="../assets/ex002.png" alt/></p><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>Note that all gnuplot commands can be abbreviated as long as the resulting string is not ambiguous. In the example above we used <code>pl</code> in place of <code>plot</code>.</p></div></div><hr/><h4 id="Split-a-@gp-call-in-three-statements:-1"><a class="docs-heading-anchor" href="#Split-a-@gp-call-in-three-statements:-1">Split a <code>@gp</code> call in three statements:</a><a class="docs-heading-anchor-permalink" href="#Split-a-@gp-call-in-three-statements:-1" title="Permalink"></a></h4><pre><code class="language-julia">@gp "set grid" :-
|
|
@gp :- "p sin(x)" :-
|
|
@gp :- "plo cos(x)"</code></pre><p><img src="../assets/ex003.png" alt/></p><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>The trailing <code>:-</code> symbol means the plot will not be updated until the last statement.</p></div></div><h3 id="Send-data-from-Julia-to-gnuplot:-1"><a class="docs-heading-anchor" href="#Send-data-from-Julia-to-gnuplot:-1">Send data from Julia to gnuplot:</a><a class="docs-heading-anchor-permalink" href="#Send-data-from-Julia-to-gnuplot:-1" title="Permalink"></a></h3><h4 id="Plot-a-parabola-1"><a class="docs-heading-anchor" href="#Plot-a-parabola-1">Plot a parabola</a><a class="docs-heading-anchor-permalink" href="#Plot-a-parabola-1" title="Permalink"></a></h4><pre><code class="language-julia">@gp (1:20).^2</code></pre><p><img src="../assets/ex004.png" alt/></p><hr/><h4 id="Plot-a-parabola-with-scaled-x-axis,-lines-and-legend-1"><a class="docs-heading-anchor" href="#Plot-a-parabola-with-scaled-x-axis,-lines-and-legend-1">Plot a parabola with scaled x axis, lines and legend</a><a class="docs-heading-anchor-permalink" href="#Plot-a-parabola-with-scaled-x-axis,-lines-and-legend-1" title="Permalink"></a></h4><pre><code class="language-julia">x = 1:20
|
|
@gp "set key left" x ./ 20 x.^2 "with lines tit 'Parabola'"</code></pre><p><img src="../assets/ex005.png" alt/></p><hr/><h4 id="Multiple-datasets,-logarithmic-axis,-labels-and-colors,-etc.-1"><a class="docs-heading-anchor" href="#Multiple-datasets,-logarithmic-axis,-labels-and-colors,-etc.-1">Multiple datasets, logarithmic axis, labels and colors, etc.</a><a class="docs-heading-anchor-permalink" href="#Multiple-datasets,-logarithmic-axis,-labels-and-colors,-etc.-1" title="Permalink"></a></h4><pre><code class="language-julia">x = 1:0.1:10
|
|
@gp "set grid" "set key left" "set logscale y"
|
|
@gp :- "set title 'Plot title'" "set label 'X label'" "set xrange [0:*]"
|
|
@gp :- x x.^0.5 "w l tit 'Pow 0.5' dt 2 lw 2 lc rgb 'red'"
|
|
@gp :- x x "w l tit 'Pow 1' dt 1 lw 3 lc rgb 'blue'"
|
|
@gp :- x x.^2 "w l tit 'Pow 2' dt 3 lw 2 lc rgb 'purple'"</code></pre><p><img src="../assets/ex006.png" alt/></p><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>The above example lacks the trailing <code>:-</code> symbol. This means the plot will be updated at each command, adding one curve at a time.</p></div></div><hr/><h2 id="Keywords-for-common-commands-1"><a class="docs-heading-anchor" href="#Keywords-for-common-commands-1">Keywords for common commands</a><a class="docs-heading-anchor-permalink" href="#Keywords-for-common-commands-1" title="Permalink"></a></h2><p>In order to avoid typing long, and very frequently used gnuplot commands, <strong>Gnuplot.jl</strong> provides a few keywords which can be used in both <code>@gp</code> and <code>@sgp</code> calls:</p><ul><li><code>xrange=[low, high]</code> => <code>"set xrange [low:high]</code>;</li><li><code>yrange=[low, high]</code> => <code>"set yrange [low:high]</code>;</li><li><code>zrange=[low, high]</code> => <code>"set zrange [low:high]</code>;</li><li><code>cbrange=[low, high]</code>=> <code>"set cbrange[low:high]</code>;</li><li><code>key="..."</code> => <code>"set key ..."</code>;</li><li><code>title="..."</code> => <code>"set title \"...\""</code>;</li><li><code>xlabel="..."</code> => <code>"set xlabel \"...\""</code>;</li><li><code>ylabel="..."</code> => <code>"set ylabel \"...\""</code>;</li><li><code>zlabel="..."</code> => <code>"set zlabel \"...\""</code>;</li><li><code>cblabel="..."</code> => <code>"set cblabel \"...\""</code>;</li><li><code>xlog=true</code> => <code>set logscale x</code>;</li><li><code>ylog=true</code> => <code>set logscale y</code>;</li><li><code>zlog=true</code> => <code>set logscale z</code>;</li></ul><p>All such keywords can be abbreviated to unambiguous names.</p><p>By using the above keywords the first lines of the previous example:</p><pre><code class="language-julia">@gp "set grid" "set key left" "set logscale y"
|
|
@gp :- "set title 'Plot title'" "set label 'X label'" "set xrange [0:*]"</code></pre><p>can be replaced with a shorter version:</p><pre><code class="language-julia">@gp "set grid" k="left" ylog=true
|
|
@gp :- tit="Plot title" xlab="X label" xr=[0,NaN]</code></pre><p>where <code>NaN</code> in the <code>xrange</code> keyword means using axis autoscaling.</p><h2 id="Plot-images-1"><a class="docs-heading-anchor" href="#Plot-images-1">Plot images</a><a class="docs-heading-anchor-permalink" href="#Plot-images-1" title="Permalink"></a></h2><p><strong>Gnuplot.jl</strong> can also display images, i.e. 2D arrays:</p><pre><code class="language-julia">img = randn(Float64, 30, 50)
|
|
img[10,:] .= -5
|
|
@gp img "w image notit"</code></pre><p><img src="../assets/ex007a.png" alt/></p><p>Note that the first index in the <code>img</code> matrix corresponds to the <code>x</code> coordinate when the image is displayed.</p><p>If the orientation is not the correct one you may adjust it with the gnuplot <code>rotate=</code> keyword (the following example requires the <code>TestImages</code> package to be installed):</p><pre><code class="language-julia">using TestImages
|
|
img = testimage("lighthouse");
|
|
@gp "set size square" "set autoscale fix" img "rotate=-90deg with rgbimage notit"</code></pre><p><img src="../assets/ex007b.png" alt/></p><p>To display a gray image use <code>with image</code> in place of <code>with rgbimage</code>, e.g.:</p><pre><code class="language-julia">img = testimage("walkbridge");
|
|
@gp palette(:viridis) "set size square" "set autoscale fix" img "rotate=-0.5pi with image notit"</code></pre><p><img src="../assets/ex007c.png" alt/></p><p>Note that we used a custom palette (<code>:lapaz</code>, see <a href="#Palettes-and-line-types-1">Palettes and line types</a>) and the rotation angle has been expressed in radians (<code>-0.5pi</code>).</p><h2 id="plots3d-1"><a class="docs-heading-anchor" href="#plots3d-1">3D plots</a><a class="docs-heading-anchor-permalink" href="#plots3d-1" title="Permalink"></a></h2><p>3D plots follow the same rules as 2D ones, just replace the <code>@gp</code> macro with <code>@gsp</code> and add the required columns (according to the plotting style).</p><p>E.g., to plot a spiral increasing in size along the <code>X</code> direction:</p><pre><code class="language-julia">x = 0:0.1:10pi
|
|
@gsp cbr=[-1,1].*30 x sin.(x) .* x cos.(x) .* x x./20 "w p pt 7 ps var lc pal"</code></pre><p><img src="../assets/ex008.png" alt/></p><p>Note that the fourth array in the dataset, <code>x./20</code>, is used as by gnuplot as point size (<code>ps var</code>). Also note that all the keywords discussed above can also be used in 3D plots.</p><h2 id="Palettes-and-line-types-1"><a class="docs-heading-anchor" href="#Palettes-and-line-types-1">Palettes and line types</a><a class="docs-heading-anchor-permalink" href="#Palettes-and-line-types-1" title="Permalink"></a></h2><p>The <strong>Gnuplot.jl</strong> package comes with all the <a href="https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Pre-defined-schemes-1">ColorSchemes</a> palettes readily available.</p><p>A gnuplot-compliant palette can be retrieved with <a href="../api/#Gnuplot.palette"><code>palette()</code></a>, and used as any other command. The previous example may use an alternative palette with:</p><pre><code class="language-julia">x = 0:0.1:10pi
|
|
@gsp palette(:viridis) cbr=[-1,1].*30 :-
|
|
@gsp :- x sin.(x) .* x cos.(x) .* x x./20 "w p pt 7 ps var lc pal"</code></pre><p><img src="../assets/ex008a.png" alt/></p><p>The <a href="https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Pre-defined-schemes-1">ColorSchemes</a> palettes can also be used to generate line types (actually just line colors), by means of the <a href="../api/#Gnuplot.linetypes"><code>linetypes()</code></a> function, e.g.</p><pre><code class="language-julia">@gp linetypes(:deepsea)
|
|
x = 1:0.1:4pi
|
|
for i in 1:5
|
|
@gp :- x i.* sin.(x) "w l notit lw 5"
|
|
end</code></pre><p><img src="../assets/ex009.png" alt/></p><p>The list of all available palette can be retrieved with <a href="../api/#Gnuplot.palette_names"><code>palette_names()</code></a>:</p><pre><code class="language-julia-repl">julia> palette_names()
|
|
450-element Array{Symbol,1}:
|
|
:BuPu_6
|
|
:Spectral_4
|
|
:lighttemperaturemap
|
|
:RdYlGn_5
|
|
:Set2_6
|
|
:PRGn_3
|
|
:greenpink
|
|
:Dark2_7
|
|
:oxy
|
|
:Set2_3
|
|
⋮
|
|
:Set1_8
|
|
:coolwarm
|
|
:picasso
|
|
:Purples_6
|
|
:BrBG_8
|
|
:flag
|
|
:valentine
|
|
:Oranges_4
|
|
:avocado</code></pre><h2 id="Exporting-plots-to-files-1"><a class="docs-heading-anchor" href="#Exporting-plots-to-files-1">Exporting plots to files</a><a class="docs-heading-anchor-permalink" href="#Exporting-plots-to-files-1" title="Permalink"></a></h2><p><strong>Gnuplot.jl</strong> to export all plots (as well as multiplots, see <a href="../advanced/#Multiplot-1">Multiplot</a>) to an external file using one of the many available gnuplot terminals. To check which terminals are available in your platform type:</p><pre><code class="language-julia-repl">julia> terminals()
|
|
59-element Array{String,1}:
|
|
"cairolatex"
|
|
"canvas"
|
|
"cgm"
|
|
"context"
|
|
"domterm"
|
|
"dpu414"
|
|
"dumb"
|
|
"dxf"
|
|
"eepic"
|
|
"emf"
|
|
⋮
|
|
"tikz"
|
|
"tkcanvas"
|
|
"tpic"
|
|
"unknown"
|
|
"vttek"
|
|
"wxt"
|
|
"x11"
|
|
"xlib"
|
|
"xterm"</code></pre><p>(see also <a href="../api/#Gnuplot.terminal"><code>terminal()</code></a> to check your current terminal).</p><p>Once you choose the proper terminal (i.e. format of the exported file), use the <a href="../api/#Gnuplot.save"><code>save()</code></a> function to export. As an example, all the plots in this page have been saved with:</p><pre><code class="language-julia">save(term="pngcairo size 480,360 fontscale 0.8", output="assets/output.png")</code></pre><p>Note that you can pass both the terminal name and its options via the <code>term=</code> keyword. See <a href="../terminals/#Gnuplot-terminals-1">Gnuplot terminals</a> for further info on the terminals.</p><h2 id="Gnuplot-scripts-1"><a class="docs-heading-anchor" href="#Gnuplot-scripts-1">Gnuplot scripts</a><a class="docs-heading-anchor-permalink" href="#Gnuplot-scripts-1" title="Permalink"></a></h2><p>Besides exporting plots in a file <strong>Gnuplot.jl</strong> can also save a <em>script</em>, i.e. a file containing the minimum set of data and commands required to re-create a figure using just gnuplot.</p><p>To generate a script for one of the examples above use:</p><pre><code class="language-julia">save("script.gp")</code></pre><p>after the plot has been displayed. Note that when images or large datasets are involved, <code>save()</code> may store the data in binary files under a directory named <code><script name>_data</code>. In order to work properly both the script and the associated directory must be available in the same directory.</p><p>E.g., the following code:</p><pre><code class="language-julia">x = 1:10
|
|
@gp x x.^2 "w l"
|
|
save("script1.gp")</code></pre><p>will produce the following file, named <code>script1.gp</code>:</p><pre><code class="language-none">reset session
|
|
$data1 << EOD
|
|
1 1
|
|
2 4
|
|
3 9
|
|
4 16
|
|
5 25
|
|
6 36
|
|
7 49
|
|
8 64
|
|
9 81
|
|
10 100
|
|
EOD
|
|
plot \
|
|
$data1 w l
|
|
set output</code></pre><p>While the following:</p><pre><code class="language-julia">img = testimage("lighthouse");
|
|
@gp "set size square" "set autoscale fix" img "rotate=-90deg with rgbimage notit"
|
|
save("script2.gp")</code></pre><p>will produce:</p><pre><code class="language-none">reset session
|
|
set size square
|
|
set autoscale fix
|
|
plot \
|
|
'./script2_data/jl_vH8X4k' binary array=(512, 768) rotate=-90deg with rgbimage notit
|
|
set output</code></pre><p>The above scripts can be loaded into a pure gnuplot session (Julia is no longer needed) as follows:</p><pre><code class="language-none">gunplot> load 'script1.gp'</code></pre><p>to generate a plot identical to the original one.</p><p>The purpose of gnuplot scripts is to allow sharing all data, alongside a plot, in order to foster collaboration among scientists and replicability of results. Moreover, a script can be used at any time to change the details of a plot, without the need to re-run the Julia code used to generate it the first time.</p><p>Finally, the scripts are the only possible output when <a href="../advanced/#Dry-sessions-1">Dry sessions</a> are used (i.e. when gnuplot is not available in the user platform.</p><section class="footnotes is-size-7"><ul><li class="footnote" id="footnote-1"><a class="tag is-link" href="#citeref-1">1</a>a previous knowledge of <a href="http://gnuplot.sourceforge.net/documentation.html">gnuplot</a> usage is, nevertheless, required.</li></ul></section></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../install/">« Installation</a><a class="docs-footer-nextpage" href="../advanced/">Advanced usage »</a></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> on <span class="colophon-date" title="Wednesday 8 April 2020 00:09">Wednesday 8 April 2020</span>. Using Julia version 1.4.0.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
|