PythonPlot.jl
Plotting for Julia based on matplotlib.pyplot
Install / Use
/learn @JuliaPy/PythonPlot.jlREADME
The PythonPlot module for Julia
This module provides a Julia interface to the
Matplotlib plotting library from Python, and
specifically to the matplotlib.pyplot module.
PythonPlot uses the Julia PythonCall.jl package to call Matplotlib directly from Julia with little or no overhead (arrays are passed without making a copy). It is based on a fork of the PyPlot.jl package, which uses the older PyCall.jl interface to Python, and is intended to function as a mostly drop-in replacement for PyPlot.jl.
This package takes advantage of Julia's multimedia I/O API to display plots in any Julia graphical backend, including as inline graphics in IJulia. Alternatively, you can use a Python-based graphical Matplotlib backend to support interactive plot zooming etcetera.
Installation
The PythonPlot package uses the CondaPkg.jl package to automatically install Matplotlib as needed.
(If you configure PythonCall to use some custom Python installation, you will need to install Matplotlib yourself.)
You can either do inline plotting with IJulia, which doesn't require a GUI backend, or use the Qt, wx, or GTK+ backends of Matplotlib as described below.
Basic usage
Once Matplotlib and PythonPlot are installed, and you are using a
graphics-capable Julia environment such as IJulia, you can simply type
using PythonPlot and begin calling functions in the
matplotlib.pyplot API.
For example:
using PythonPlot
x = range(0; stop=2*pi, length=1000); y = sin.(3 * x + 4 * cos.(2 * x));
plot(x, y, color="red", linewidth=2.0, linestyle="--")
title("A sinusoidally modulated sinusoid")
In general, all of the arguments, including keyword arguments, are
exactly the same as in Python. (With minor translations, of course,
e.g. Julia uses true and nothing instead of Python's True and
None.)
The full matplotlib.pyplot API is far too extensive to describe here;
see the matplotlib.pyplot documentation for more
information. The Matplotlib
version number is returned by PythonPlot.version.
Differences from PyPlot.jl
Compared to the PyPlot.jl package, there are a few differences in the API.
- To avoid type piracy, the functions
show,close,step, andfillare renamed toplotshow,plotclose,plotstep, andplotfill, respectively. (You can also access them asPythonPlot.showetcetera.) - The
matplotlib.pyplotmodule is exported aspyplotrather than asplt. - The PythonCall package performs many fewer automatic conversions from Python types to Julia types (in comparison to PyCall). If you need to convert Matplotlib return values to native Julia objects, you'll need to do
using PythonCalland call itspyconvert(T, o)or other conversion functions.
Exported functions
Only the currently documented matplotlib.pyplot API is exported. To use
other functions in the module, you can also call matplotlib.pyplot.foo(...)
as pyplot.foo(...). For example, pyplot.plot(x, y) also works. (And
the raw Py object for the matplotlib module itself is also accessible
as PythonPlot.matplotlib.)
Matplotlib is somewhat inconsistent about capitalization: it has
contour3D but bar3d, etcetera. PyPlot renames all such functions
to use a capital D (e.g. it has hist2D, bar3D, and so on).
You must also explicitly qualify some functions
built-in Julia functions. In particular, PythonPlot.xcorr,
PythonPlot.axes, and PythonPlot.isinteractive
must be used to access matplotlib.pyplot.xcorr
etcetera.
If you wish to access all of the PyPlot functions exclusively
through pyplot.somefunction(...), as is conventional in Python, you can
do import PythonPlot as pyplot instead of using PythonPlot.
Figure objects
You can get the current figure as a Figure object (a wrapper
around matplotlib.pyplot.Figure) by calling gcf().
The Figure type supports Julia's multimedia I/O
API,
so you can use display(fig) to show a fig::PyFigure and
show(io, mime, fig) (or writemime in Julia 0.4) to write it to a given mime type string
(e.g. "image/png" or "application/pdf") that is supported by the
Matplotlib backend.
Non-interactive plotting
If you use PythonPlot from an interactive Julia prompt, such as the Julia
command-line prompt
or an IJulia notebook, then plots appear immediately after a plotting
function (plot etc.) is evaluated.
However, if you use PythonPlot from a Julia script that is run non-interactively
(e.g. julia myscript.jl), then Matplotlib is executed in
non-interactive mode:
a plot window is not opened until you run plotshow() (equivalent to pyplot.show()
in the Python examples).
Interactive versus Julia graphics
PythonPlot can use any Julia graphics backend capable of displaying PNG,
SVG, or PDF images, such as the IJulia environment. To use a
different backend, simply call pushdisplay with the desired
Display; see the Julia multimedia display
API
for more detail.
On the other hand, you may wish to use one of the Python Matplotlib backends to open an interactive window for each plot (for interactive zooming, panning, etcetera). You can do this at any time by running:
pygui(true)
to turn on the Python-based GUI (if possible) for subsequent plots,
while pygui(false) will return to the Julia backend. Even when a
Python GUI is running, you can display the current figure with the
Julia backend by running display(gcf()).
If no Julia graphics backend is available when PythonPlot is imported, then
pygui(true) is the default.
Choosing a Python GUI toolkit
Only the Tk, wxWidgets,
GTK+ (version 2 or 3), and Qt (version 4 or 5; via the PyQt5,
PyQt4 or
PySide), Python GUI backends are
supported by PythonPlot. (Obviously, you must have installed one of these
toolkits for Python first.) By default, PythonPlot picks one of these
when it starts up (based on what you have installed), but you can
force a specific toolkit to be chosen by setting the MPLBACKEND environment variable
to the desired Python backend before
importing PythonPlot:
ENV["MPLBACKEND"] = backend
using PythonPlot
where backend is typically one of "wxagg", "gtkagg", "gtk3agg", "qt5agg", "qt4agg", or "tkagg".
You can
also set a default via the Matplotlib rcParams['backend'] parameter in your
matplotlibrc file.
Color maps
The PythonPlot module also exports some functions and types based on the matplotlib.colors and matplotlib.cm modules to simplify management of color maps (which are used to assign values to colors in various plot types). In particular:
-
ColorMap: a wrapper around the matplotlib.colors.Colormap type. The following constructors are provided:-
ColorMap{T<:Colorant}(name::String, c::AbstractVector{T}, n=256, gamma=1.0)constructs ann-component colormap by linearly interpolating the colors in the arraycofColorants (from the ColorTypes.jl package). If you want anameto be constructed automatically, callColorMap(c, n=256, gamma=1.0)instead. Alternatively, instead of passing an array of colors, you can pass a 3- or 4-column matrix of RGB or RGBA components, respectively (similar to ListedColorMap in Matplotlib). -
Even more general color maps may be defined by passing arrays of (x,y0,y1) tuples for the red, green, blue, and (optionally) alpha components, as defined by the matplotlib.colors.LinearSegmentedColormap constructor, via:
ColorMap{T<:Real}(name::String, r::AbstractVector{(T,T,T)}, g::AbstractVector{(T,T,T)}, b::AbstractVector{(T,T,T)}, n=256, gamma=1.0)orColorMap{T<:Real}(name::String, r::AbstractVector{(T,T,T)}, g::AbstractVector{(T,T,T)}, b::AbstractVector{(T,T,T)}, alpha::AbstractVector{(T,T,T)}, n=256, gamma=1.0) -
ColorMap(name::String)returns an existing (registered) colormap, equivalent to matplotlib.pyplot.get_cmap(name). -
matplotlib.colors.Colormapobjects returned by Python functions are automatically converted to theColorMaptype.
-
-
get_cmap(name::String)orget_cmap(name::String, lut::Integer)call the matplotlib.pyplot.get_cmap function. -
register_cmap(c::ColorMap)orregister_cmap(name::String, c::ColorMap)call the [matplotlib.colormaps.register](https://matplotlib.org/stable/api/cm_api.h
Related Skills
node-connect
341.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
341.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.6kCommit, push, and open a PR
