39 lines
12 KiB
HTML
39 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Plot recipes · 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><a class="tocitem" href="../basic/">Basic usage</a></li><li><a class="tocitem" href="../advanced/">Advanced usage</a></li><li><a class="tocitem" href="../options/">Package options</a></li><li><a class="tocitem" href="../style/">Style guide</a></li><li><a class="tocitem" href="../terminals/">Gnuplot terminals</a></li><li class="is-active"><a class="tocitem" href>Plot recipes</a><ul class="internal"><li><a class="tocitem" href="#Explicit-recipe-(example)-1"><span>Explicit recipe (example)</span></a></li><li><a class="tocitem" href="#Histogram-recipes-1"><span>Histogram recipes</span></a></li><li><a class="tocitem" href="#Contour-lines-recipes-1"><span>Contour lines recipes</span></a></li><li><a class="tocitem" href="#Image-recipes-1"><span>Image recipes</span></a></li></ul></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>Plot recipes</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>Plot recipes</a></li></ul></nav><div class="docs-right"><a class="docs-edit-link" href="https://github.com/gcalderone/Gnuplot.jl/blob/master/docs/src/recipes.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="Plot-recipes-1"><a class="docs-heading-anchor" href="#Plot-recipes-1">Plot recipes</a><a class="docs-heading-anchor-permalink" href="#Plot-recipes-1" title="Permalink"></a></h1><p>A plot <em>recipe</em> is a quicklook visualization procedure aimed at reducing the amount of repetitive code to generate a plot. More specifically, a recipe is a function that convert data from the "Julia world" into a form suitable to be ingested in <strong>Gnuplot.jl</strong>, namely a scalar (or a vector of) <a href="../api/#Gnuplot.PlotElement"><code>Gnuplot.PlotElement</code></a> object(s). The latter contain informations on how to create a plot, or a part of it, and can be used directly as arguments in a <code>@gp</code> or <code>@gsp</code> call.</p><p>There are two kinds of recipes:</p><ul><li><p><em>explicit</em> recipe: a function which is explicitly invoked by the user. It can have any name and accept any number of arguments and keywords. It is typically used when the visualization of a data type requires some extra information, beside data itself (e.g. to plot data from a <code>DataFrame</code> object, see <a href="#Explicit-recipe-(example)-1">Explicit recipe (example)</a>);</p></li><li><p><em>implicit</em> recipe: a function which is automatically called by <strong>Gnuplot.jl</strong>. It must extend the <a href="../api/#Gnuplot.recipe"><code>recipe()</code></a> function, and accept exactly one mandatory argument. It is typically used when the visualization is completely determined by the data type itself (e.g. the visualization of a <code>Matrix{ColorTypes.RGB}</code> object as an image, see <a href="#Image-recipes-1">Image recipes</a>);</p></li></ul><p>An implicit recipe is invoked whenever the data type of an argument to <code>@gp</code> or <code>@gsp</code> is not among the allowed ones (see <a href="../api/#Gnuplot.@gp"><code>@gp()</code></a> documentation). If a suitable recipe do not exists an error is raised. On the other hand, an explicit recipe needs to be invoked by the user, and the output passed directly to <code>@gp</code> or <code>@gsp</code>.</p><p>Although recipes provides very efficient tools for data exploration, their use typically hide the details of plot generation. As a consequence they provide less flexibility than the approaches described in <a href="../basic/#Basic-usage-1">Basic usage</a> and <a href="../advanced/#Advanced-usage-1">Advanced usage</a>.</p><p>Currently, the <strong>Gnuplot.jl</strong> package provides no built-in explicit recipe. The implicit recipes are implemented in <a href="https://github.com/gcalderone/Gnuplot.jl/blob/master/src/recipes.jl">recipes.jl</a>.</p><h2 id="Explicit-recipe-(example)-1"><a class="docs-heading-anchor" href="#Explicit-recipe-(example)-1">Explicit recipe (example)</a><a class="docs-heading-anchor-permalink" href="#Explicit-recipe-(example)-1" title="Permalink"></a></h2><p>To generate a plot using the data contained in a <code>DataFrame</code> object we need, beside the data itself, the name of the columns to use for the X and Y coordinates. The following example shows how to implement an explicit recipe to plot a <code>DataFrame</code> object:</p><pre><code class="language-julia">using RDatasets, DataFrames, Gnuplot
|
|
import Gnuplot: PlotElement, DatasetText
|
|
|
|
function plotdf(df::DataFrame, colx::Symbol, coly::Symbol; group=nothing)
|
|
if isnothing(group)
|
|
return PlotElement(data=DatasetText(df[:, colx], df[:, coly]),
|
|
plot="w p notit",
|
|
xlab=string(colx), ylab=string(coly))
|
|
end
|
|
|
|
out = Vector{Gnuplot.PlotElement}()
|
|
push!(out, PlotElement(;xlab=string(colx), ylab=string(coly)))
|
|
for g in sort(unique(df[:, group]))
|
|
i = findall(df[:, group] .== g)
|
|
if length(i) > 0
|
|
push!(out, PlotElement(data=DatasetText(df[i, colx], df[i, coly]),
|
|
plot="w p t '$g'"))
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
# Load a DataFrame and convert it to a PlotElement
|
|
iris = dataset("datasets", "iris")
|
|
@gp plotdf(iris, :SepalLength, :SepalWidth, group=:Species)</code></pre><p><img src="../assets/recipes001.png" alt/></p><h2 id="Histogram-recipes-1"><a class="docs-heading-anchor" href="#Histogram-recipes-1">Histogram recipes</a><a class="docs-heading-anchor-permalink" href="#Histogram-recipes-1" title="Permalink"></a></h2><p>The object returned by the <a href="../api/#Gnuplot.hist"><code>hist()</code></a> function can be readily visualized by means of implicit recipes defined on the <code>Gnuplot.Histogram1D</code> and <code>Gnuplot.Histogram2D</code> types:</p><pre><code class="language-julia">x = randn(1000);
|
|
@gp hist(x)</code></pre><p><img src="../assets/recipes002.png" alt/></p><pre><code class="language-julia">x = randn(10_000);
|
|
y = randn(10_000);
|
|
@gp hist(x, y)</code></pre><p><img src="../assets/recipes002a.png" alt/></p><h2 id="Contour-lines-recipes-1"><a class="docs-heading-anchor" href="#Contour-lines-recipes-1">Contour lines recipes</a><a class="docs-heading-anchor-permalink" href="#Contour-lines-recipes-1" title="Permalink"></a></h2><p>The object returned by the <a href="../api/#Gnuplot.contourlines"><code>contourlines()</code></a> function can be readily visualized by means of implicit recipes defined on the <code>Gnuplot.IsoContourLines</code> types:</p><pre><code class="language-julia">x = randn(10_000);
|
|
y = randn(10_000);
|
|
h = hist(x, y)
|
|
clines = contourlines(h, "levels discrete 10, 30, 60, 90");
|
|
@gp clines</code></pre><p><img src="../assets/recipes002b.png" alt/></p><h2 id="Image-recipes-1"><a class="docs-heading-anchor" href="#Image-recipes-1">Image recipes</a><a class="docs-heading-anchor-permalink" href="#Image-recipes-1" title="Permalink"></a></h2><p>The <strong>Gnuplot.jl</strong> package provides implicit recipes to display images in the following formats:</p><ul><li><code>Matrix{ColorTypes.RGB{T}}</code>;</li><li><code>Matrix{ColorTypes.RGBA{T}}</code></li><li><code>Matrix{ColorTypes.Gray{T}}</code>;</li><li><code>Matrix{ColorTypes.GrayA{T}}</code>;</li></ul><p>To use these recipes simply pass an image to <code>@gp</code>, e.g.:</p><pre><code class="language-julia">using TestImages
|
|
img = testimage("lighthouse");
|
|
@gp img</code></pre><p><img src="../assets/recipes007b.png" alt/></p><p>All such recipes are defined as:</p><pre><code class="language-julia">function recipe(M::Matrix{ColorTypes.RGB{T}}, opt="flipy")
|
|
...
|
|
end</code></pre><p>with only one mandatory argument. In order to exploit the optional keyword we can explicitly invoke the recipe as follows:</p><pre><code class="language-julia">img = testimage("walkbridge");
|
|
@gp palette(:gray) recipe(img, "flipy rot=15deg")</code></pre><p><img src="../assets/recipes007c.png" alt/></p><p>Note that we used both a palette (<code>:gray</code>, see <a href="../basic/#Palettes-and-line-types-1">Palettes and line types</a>) and a custom rotation angle.</p><p>The <code>flipy</code> option is necessary for proper visualization (see discussion in <a href="../basic/#Plot-matrix-as-images-1">Plot matrix as images</a>).</p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../terminals/">« Gnuplot terminals</a><a class="docs-footer-nextpage" href="../examples/">Examples »</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="Monday 20 April 2020 18:43">Monday 20 April 2020</span>. Using Julia version 1.4.0.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
|