Control operations
Figures and plots initialization/referencing
GRUtils.Figure
— MethodFigure([figsize::Tuple{Float64, Float64}, units::String])
Create a new figure of a given size.
The figure size is defined by figsize
(a 2-tuple with the target width and height), and units
(a string with the abbreviation of the units: "px" for pixels, "in" for inches, "cm" for centimeters or "m" for meters). The default dimensions used by Figure()
with no arguments is 600×450 pixels — or a proportional increased size, if the detected display resolution is high. This constructor also sets the "current figure" to the one that has just been created..
GRUtils.gcf
— Functiongcf()
gcf(fig::Figure)
Get the global current figure, or set it to be fig
.
GRUtils.currentplot
— Functioncurrentplot([fig::Figure])
Get the "current plot", i.e. the target of the next plotting operations in the (optionally) given figure fig
.
If no figure is given, the "current figure" is used (cf. gcf
).
Multiple plots
GRUtils.hold
— Functionhold(flag::Bool)
Set the hold flag for combining multiple plots.
hold(true)
prevents clearing previous plots, so that next plots will be drawn on top of the previous one until hold(false)
is called.
Use the keyword argument hold=<true/false>
in plotting functions, to set the hold flag during the creation of plots.
GRUtils.subplot
— Functionsubplot(num_rows, num_columns, indices[, replace])
Set a subplot in the current figure.
By default, the current plot covers the whole window. To display more than one plot, the window can be split into a number of rows and columns, with each plot covering one or more cells in the resulting grid.
Subplot indices are one-based and start at the upper left corner, with a new row starting after every num_cols
subplots.
The arguments num_rows
and num_cols
indicate the number of rows and columns of the grid of plots into which the figure is meant to be divided, and indices
is an integer or a collection of integers that identify a group of cells in that grid. This function returns a plot with the minimum size that spans over all those cells, and appends it to the array of plots of the figure, such that it becomes its current plot.
If the viewport of the new subplot coincides with the viewport of an existing plot, by default the older one is moved to the first plane, and taken as the "current plot"; but if there is only a partial overlap between the new subplot and other plots, the overlapping plots are removed. To override this behavior and keep all previous plots without changes, set the optional argument replace
to false.
Examples
# Create example data
x = randn(100_000)
y = randn(100_000)
# Draw an hexagonal plot in the bigger bottom-right region
subplot(3, 3, (5, 9))
hexbin(x, y, colorbar = false)
# Draw marginal histograms
subplot(3, 3, (2, 3))
histogram(x)
subplot(3, 3, (4, 7))
histogram(y, horizontal = true, xflip = true)
# Draw a shade plot in the smaller top-left region
subplot(3, 3, 1)
shade(x, y)
Animations
GRUtils.video
— Functionvideo(fun::Function, target="webm")
video(figs::AbstractArray{<:Fig}, target="webm")
Make a video from a function or an array of figures.
The first argument can be an array of Figures
that will be displayed as a sequence of frames in the video, or a function without arguments that draws the figures (normally a loop where the figures are created and drawn one after another). That function can be defined anonymously in the call to video
with the do
syntax (see the example).
The second (optional) argument target
must be a string with one of the formats "webm"
(default), "mp4"
or "mov"
.
The output is an object that may be displayed as a video of the given format (depending on the supported MIME outputs of the environment). Use videofile
to save such a video in a file.
Examples
# Make a plot with example data
x = LinRange(0, 800, 100)
y = sind.(x)
plot(x,y)
# Make a video sliding over the X axis
video("webm") do
for d = 0:10:440
xlim(d, d+360)
draw(gcf())
end
end
Save to files
GRUtils.savefig
— Functionsavefig(filename[, fig])
Save a figure to a file.
Draw the current figure in a file of the given name. Which file types are supported depends on the installed workstation types, but GR usually is built with support for .png, .jpg, .pdf, .ps, .gif and various other file formats.
If no figure is given (optional argument fig
), the current figure is used.
Examples
# Create a simple plot
x = 1:100
plot(x, 1 ./ (x .+ 1))
# Save the figure to a file
savefig("example.png")
GRUtils.videofile
— Functionvideofile(fun::Function, target, overwrite=false)
videofile(figs::AbstractArray{<:Fig}, target, overwrite=false)
Make a video from a function or an array of figures and save it into a file.
The first argument can be an array of Figures
that will be displayed as a sequence of frames in the video, or a function without arguments that draws the figures (normally a loop where the figures are created and drawn one after another). That function can be defined anonymously in the call to videofile
with the do
syntax (see the example).
The secondargument target
must be a string with the name of the video file, whose format is determined by the extension of the file. The supported extensions are "webm"
, "mp4"
or "mov"
.
Use overwrite=true
to force the creation of target
if the file already exists (otherwise an error will be thrown in such cases).
Examples
# Make a plot with example data
x = LinRange(0, 800, 100)
y = sind.(x)
plot(x,y)
# Make a video sliding over the X axis
videofile("sin.mp4") do
for d = 0:10:440
xlim(d, d+360)
draw(gcf())
end
end