PlotPlants

Set global font

The PyPlot package of Julia uses sans font by default, by my favorite font is serif type. Also, some fonts do not have normal greek letters in their library, like the Times only have italic greek letters. To change the default font to serif and use normal greek letters, the PlotPlants package provides a use_serif_tex function, which use LaTeX amsmath and upgreek packages. Also, sans-serif font and normal text render are also supported.

Symbols and units

To facilitate the use of LaTeX packages, functions latex_symbol and latex_unit are provided to rapidly generate equation symbols and units. By default,

  • if the number of letters is one, the letter will be set to italic, otherwise, the letters will be normal (not italic);
  • if the letter is greek, the letter will be normal;
  • all sub- and super-scripts are normal;
PlotPlants.latex_symbolFunction
latex_symbol(
            mid::String;
            sub::String = "",
            sup::String = "",
            presub::String = "",
            presup::String = "",
            option::String = "mathrm"
)

Return the latex symbol string, given

  • mid Center symbol, italic only when length>1
  • sub Optional: subscript after the mid
  • sup Optional: supscript after the mid
  • presub Optional: subscript before the mid
  • presup Optional: supscript before the mid
  • option Optional: choose from text and mathrm (default)
source
PlotPlants.latex_unitFunction
latex_unit(id::String;
           paren::Bool = true,
           squared::Bool = false,
           space::Bool = true,
           unicode::Bool = false
)

Return the unit, given

  • id Identifier of the unit (if not in libary, use id as unit)
  • paren Optional: if true, include parenthesis
  • squared Optional: if true, use [], otherwise, use ()
  • space Optional: if true, add a SPACE at the beginning
  • unicode Optional. If true, return unicode; otherwise, return upgreek string

The predefined units include

  • A μmol CO₂ m⁻² s⁻¹
  • E mol H₂O m⁻² s⁻¹
  • E_MMOL mmol H₂O m⁻² s⁻¹
  • G mol m⁻² s⁻¹
  • PAR μmol m⁻² s⁻¹
  • T °C
  • WUE μmol mol⁻¹
source
PlotPlants.parse_symbolFunction
parse_symbol(letter::String, unicode::Bool=false)

Return the unicode or latex string, given

  • id Identifier for the greek letter, e.g., "alpha"
  • unicode Optional. If true, return unicode; otherwise, return upgreek string
source
PlotPlants.subscriptFunction
subscript(ss::Number; option="mathrm", standalone=true)
subscript(ss::String; option="mathrm", standalone=true)

Return a string of subscript, given

source
PlotPlants.superscriptFunction
superscript(ss::Number; option="mathrm", standalone=true)
superscript(ss::String; option="mathrm", standalone=true)

Return a string of superscript, given

source

Create and save canvas

An customized funtion create_canvas is provided to genrate figure and an array of axis from a few keywords. For example,

  • create_canvas(fig_id) returns a figure with one axis;
  • create_canvas(fig_id, nrow=2, ncol=2) returns a figure will four subplots;
  • create_canvas(fig_id, nrow=2, ncol=2, axs=[1,2,4]) returns a figure with three subplot (the left bottom panel is empty);
PlotPlants.create_canvasFunction
create_canvas(
            id::Union{Int,String};
            ncol::Int = 1,
            nrow::Int = 1,
            axids::Array{Int,1} = Int[],
            figsize::Tuple{Number,Number} = (0.5+ncol*3, 0.5+nrow*3),
            dpi::Number = 100
)

Create a canvas, given

  • id ID of the figure
  • ncol Number of columns in the figure
  • nrow Number of rows in the figure
  • axids Given indicies of the subplots in the figure
  • figsize Given canvas size
  • dpi Given pixels per inch
source
PlotPlants.save_canvas!Function
save_canvas!(fig::Figure, path::String, saving::Bool = false)

Save a canvas, given

  • fig Canvas to save
  • path Path to save the figure
  • saving Optional. If true, save the figure
source
PlotPlants.save_gif!Function
save_gif!(figs::Array, fps::Int, gif::String)
save_gif!(imgs::Array{Array,1}, fps::Int, fn::String)

Save an array of figures as GIF, given

  • figs An array of figures
  • fps Frame per second
  • fn File name of the GIF to save
  • imgs An array of array (loaded figures)
source

Shapes

A few customized shapes can be added to the canvas very conveniently, for example, stoma and ellipse. These shortcut functions include

PlotPlants.plot_ellipse!Function
plot_ellipse!(
            ax::PyObject,
            xy::Tuple{Number,Number};
            width::Number = 10,
            height::Number = 10,
            angle::Number = 0,
            color::String = "black",
            edgecolor::String = color,
            facecolor::String = color,
            alpha::Number = 0.5
)

Plot an ellipse on axis, given

  • ax Axis to plot on
  • xy Center of the ellipse
  • width Width of the ellipse
  • height Height of the ellipse
  • angle Rotation angle of the ellipse
  • color Color of the ellipse
  • edgecolor Edgecolor of the ellipse
  • facecolor Face color of the ellipse
  • alpha Transparency of the ellipse
source
PlotPlants.plot_stoma!Function
plot_stoma!(
           ax::PyObject,
           xy::Tuple{Number,Number};
           width::Number = 10,
           height::Number = 10,
           stoma::Number = 0.2,
           angle::Number = 0
)

Plot a stoma on the axis, given

  • ax Axis to plot on
  • xy Center of the stoma
  • width Width of the stoma
  • height Height of the stoma
  • stoma Stomatal pore width ratio
  • angle Rotation angle of the stoma
source

Set titles, lims, ticks, and ticklabels

Function set_titles! will add panel titles like (a) to all the panels, and if labels options is given, the labels will be added after the letter (e.g., (a) label).

PlotPlants.set_titles!Function
set_titles!(axs::Array;
            labels::Array{String,1} = String[],
            paren::Bool = true,
            capital::Bool = false,
            dotsep::Bool = false,
            fontsize::Number = 16,
            loc::String = "center",
            usetex::Bool = true
)

Set titles for the axes, given

  • axs An array of axis
  • labels Optional: labels after the panel title, e.g., (a) label
  • paren Optional: if true, use format like (a)
  • capital Optional: if true, use capital letters like (A)
  • dotsep Optional: if true, add a dot after the label like (a).
  • fontsize Optional: fontsize of the title
  • loc Optional: location of the title
  • usetex Optional: use latex render
source

Similarly, the package provides shortcuts to set the X-axis and Y-axis labels, limits, ticks, and tick labels.

PlotPlants.set_xlabels!Function
set_xlabels!(
            axs::Array,
            xlabels::Array{String,1};
            fontsize::Number = 16
)
set_xlabels!(
            axs::Array,
            xlabels::String;
            fontsize::Number = 16
)

Set X-axis labels for the axes, given

  • axs An array of axis
  • labels X-axis labels
  • fontsize Optional: fontsize of the label
source
PlotPlants.set_ylabels!Function
set_ylabels!(
            axs::Array,
            ylabels::Array{String,1};
            fontsize::Number = 16
)
set_ylabels!(
            axs::Array,
            ylabels::String;
            fontsize::Number = 16
)

Set Y-axis labels for the axes, given

  • axs An array of axis
  • labels Y-axis labels
  • fontsize Optional: fontsize of the label
source
PlotPlants.set_xylabels!Function
set_xylabels!(
            axs::Array,
            xlabels::Union{Array{String,1},String},
            ylabels::Union{Array{String,1},String};
            fontsize::Number = 16
)

Set X-axis and Y-axis labels for the axes, given

  • axs An array of axis
  • xlabels X-axis labels
  • ylabels Y-axis labels
  • fontsize Optional: fontsize of the label
source
PlotPlants.set_xlims!Function
set_xlims!(axs::Array, xlims::Array)
set_xlims!(axs::Array,
           xlims::Union{Array{Int,1},Array{Float32,1},Array{Float64,1}}
)

Set the X-axis limits, given

  • axs Array of axis
  • xlims Array of X-axis limits
source
PlotPlants.set_ylims!Function
set_ylims!(axs::Array, ylims::Array)
set_ylims!(axs::Array,
           ylims::Union{Array{Int,1},Array{Float32,1},Array{Float64,1}}
)

Set the Y-axis limits, given

  • axs Array of axis
  • ylims Array of Y-axis limits
source
PlotPlants.set_xylims!Function
set_xylims!(axs::Array, xlims::Array, ylims::Array)

Set the X-axis limits, given

  • axs Array of axis
  • xlims Array of X-axis limits
  • ylims Array of Y-axis limits
source
PlotPlants.set_xticks!Function
set_xticks!(axs::Array, xticks::Array)
set_xticks!(axs::Array,
            xticks::Union{Array{Int,1},Array{Float32,1},Array{Float64,1}}
)

Set X-axis ticks for a given

  • axs Array of axis
  • xticks Array of X-axis ticks
source
PlotPlants.set_yticks!Function
set_yticks!(axs::Array, yticks::Array)
set_yticks!(axs::Array,
            yticks::Union{Array{Int,1},Array{Float32,1},Array{Float64,1}}
)

Set Y-axis ticks for a given

  • axs Array of axis
  • yticks Array of Y-axis ticks
source
PlotPlants.set_xyticks!Function
set_xyticks!(axs::Array, xticks::Array, yticks::Array)

Set Y-axis ticks for a given

  • axs Array of axis
  • xticks Array of X-axis ticks
  • yticks Array of Y-axis ticks
source
PlotPlants.set_xticklabels!Function
set_xticklabels!(axs::Array, xticklabs::Array)
set_xticklabels!(axs::Array, xticklabs::Array{String,1})

Set X-axis ticks for a given

  • axs Array of axis
  • xticklabs Array of X-axis tick labels
source
PlotPlants.set_yticklabels!Function
set_yticklabels!(axs::Array, yticklabs::Array)
set_yticklabels!(axs::Array, yticklabs::Array{String,1})

Set Y-axis ticks for a given

  • axs Array of axis
  • yticklabs Array of Y-axis tick labels
source
PlotPlants.set_xyticklabels!Function
set_xyticklabels!(axs::Array, xticklabs::Array, yticklabs::Array)

Set X-axis and Y-axis ticks for a given

  • axs Array of axis
  • xticklabs Array of X-axis tick labels
  • yticklabs Array of Y-axis tick labels
source

Add regression

To quickly plot the regression lines, users may use

PlotPlants.plot_line_regress!Function
plot_line_regress!(
            ax::PyObject,
            xs::Array,
            ys::Array;
            linestyle::String = "-",
            intercept::Bool = true,
            interval::Bool = false,
            color::String = "red",
            alpha::Number = 0.3
)

Plor linear regression and confidence interval on the axis, given

  • ax Given axis
  • xs Array of x
  • ys Array of y
  • linestyle Optional. Line style for the regression curve ("-" by default)
  • intercept Optional: if true, fit the data with an intercept
  • interval Optional: if true, plot the confidence interval of fitted y
  • color Color the fitted curve
  • alpha Transparency of the confidence interval (same color as curve)
source

The function will filter out the NaN from the given data, make linear regression using function line_regress, sort the results by the value of x from low to high, store the results in a LinearRegressionResult type struct, and plot the regression line on the given axis. Also, if interval is set to true, the function will also plot the confidence intervals of the predicted y.

PlotPlants.linear_df_xyFunction
linear_df_xy(df::DataFrame, intercept::Bool)

Linear fit the X and Y in a given DataFrame, given

  • df DataFrame that use X and Y to store values
  • intercept Whether using intercept when making the regression
source
PlotPlants.line_regressFunction
line_regress(
            xs::Array,
            ys::Array;
            intercept::Bool = true,
            sorting::Bool = true
)

Make linear regression and return the fitted results, given

  • xs Array of x, can be NaN
  • ys Array of y, can be NaN
  • intercept Optional: if true use intercept in the fitting
  • sorting Optional: if true, sort the values
source
PlotPlants.LinearRegressionResultType
mutable struct LinearRegressionResult

Fields

  • lm::Any

    Regression result

  • r2::Number

    Adjusted R square

  • inter::Number

    Intercept of fitting

  • slope::Number

    Slope of fitting

  • inter_p::Number

    P value of intercept

  • slope_p::Number

    P value of slopes

  • inter_ci::Array

    Confidence interval of intercept

  • slope_ci::Array

    Confidence interval of slopes

  • df::DataFrames.DataFrame

    Predictions DataFrame

source

Further, the PlotPlants module provides a quick tool to test whether the slope differs from a given value, see

PlotPlants.test_slopeFunction
test_slope(xs::Array,
           ys::Array;
           slope::Number = 0,
           intercept::Bool = true
)

Make linear regression and return the p value of whether the regression slope differs from the given slope, given

  • xs Array of x, can be NaN
  • ys Array of y, can be NaN
  • slope Slope to test
  • intercept Optional: if true use intercept in the fitting
source

Density plot

PlotPlants.calculate_densityFunction
calculate_density(xs::Array, ys::Array)
calculate_density(xs::Array, ys::Array, nthread::Int)

Plot density plot on axis, given

  • xs Array of X
  • ys Array of Y
  • nthread Number of threads to run in parallel
source
PlotPlants.plot_density!Function
plot_density!(
            ax::PyObject,
            xs::Array,
            ys::Array;
            cmap::String = "viridis",
            markersize::Number = 8,
            dmax::Number = NaN
)
plot_density!(
            ax::PyObject,
            df::DataFrame;
            cmap::String = "viridis",
            markersize::Number = 8,
            dmax::Number = NaN
)

Plot density plot on axis, given

  • ax Axis to plot on
  • xs Array of X
  • ys Array of Y
  • cmap Optional. Color map scheme
  • markersize Optional. Marker size dimension, scatter size is markersize^2
  • dmax Maximal density. If dmax is not NaN, use dmax as maximum density
  • df A dataframe with column names of ("X","Y","C")
source
PlotPlants.plot_hexbin!Function
plot_hexbin!(
            ax::PyObject,
            xs::Array,
            ys::Array;
            cmap::String = "Greys",
            logbins::Bool = false,
            gridsize::Number = 25
)
plot_hexbin!(
            ax::PyObject,
            xs::Array,
            ys::Array,
            xlims::Array,
            ylims::Array;
            cmap::String = "Greys",
            logbins::Bool = false,
            gridsize::Number = 25
)

Plot density plot on axis, given

  • ax Axis to plot on
  • xs Array of X
  • ys Array of Y
  • cmap Optional. Color map scheme
  • logbins Optional. If true, use log(count) to color the bins
  • gridsize Number of bins on both directions
  • xlim Limits of x axis. Used to make plot region equal among subplots
  • ylim Limits of y axis. Used to make plot region equal among subplots
source

Preview dataset

PlotPlants.preview_dataFunction
preview_data(
            xs::Array,
            ys::Array;
            title = randstring(10),
            figsize::Tuple{Number,Number} = (4,3),
            xlab::String = "X label",
            ylab::String = "Y label",
            marker::String = "",
            linestyle::String = "-",
            label_fontsize::Int = 12
)

Preview data, given

  • xs Array of x
  • ys Array of y
  • title Axis titile
  • figsize Canvas size
  • xlab X axis label
  • ylab Y axis label
  • marker Marker style
  • linestyle Line style
  • label_fontsize X and Y axes label font size
source
PlotPlants.preview_dataset!Function
preview_dataset!(ax::PyObject, filename::String, label)
preview_dataset!(
            ax::PyObject,
            filename::String,
            label::String,
            format::FormatNC
)

Preview dataset, given

  • filename Dataset file to preview
  • label Label of the data to preview, variable name in NC files, band name in TIFF files
  • format AbstractFormat type file format
source