- Notifications
You must be signed in to change notification settings - Fork0
Matplotlib wrapper with some customisations to plot pretty graphs with very few lines of code. Plots can be updated using an interactive GUI.
License
tfpf/graph-plot
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Customplot is a wrapper around Matplotlib. It can be used to plot beautiful,publication-quality graphs, which can optionally be controlled interactively.
I mainly wrote this to plot graphs of functions in the Cartesian plane, but itcan be used for many other things, too. (Take a gander at thegallery.)
Name | Version | Comment |
---|---|---|
Python | 3.8 | Required |
Matplotlib | 3.3.4 | Required |
NumPy | 1.17 | Required |
curses | 2.2 | Optional |
These requirements are not strict. Slightly older versions should also be okay.
Calllimit
andpolish
on your Matplotlib axes instance before and afterplotting, respectively. Check the fileexamples.py
for details. You can runit using the command
$python3 examples.py
to see how things work.
A good way to start plotting your own graphs might be to copy parts of the codeinexamples.py
to another file, make modifications to that file, and run it.
Customplot cannot replace Matplotlib or Pyplot. It just does a few things whichmake the plot look pretty (to my eyes, at least). For sufficiently complicatedplots, you may have to use functions of Matplotlib or Pyplot directly.
Onlyrectilinear
,polar
and3d
projections of Matplotlib axes aresupported. Other projections can also be used, but their beautification may nothappen automatically.
As seen in the image above, you can interactively adjust some plot elements ofall Matplotlib axes in a figure (provided that the curses module is installedand available). To do so, just usecustomplot.show(fig)
(fig
being theMatplotlib figure instance) instead ofplt.show()
. More details can be foundinexamples.py
.
Matplotlib is not thread-safe! Since the curses GUI is run in a separatethread, you may occasionally have to trigger additional GUI events (e.g.resizing the figure window) to have the desired effect.
Using a good font can significantly improve the appearance of your plot. (Checkout thegallery. The font used in those plots isCochineal.)
In case you want nothing to do with fonts, you can ignore this sectionentirely.
Some fonts are packaged with Matplotlib; you can choose one of them. Mysuggestion is a STIX font. To use it, add the following lines to the filelight.mplstyle
(you can find this file in thestyles
directory).
font.family:STIXGeneralmathtext.fontset:stix
Computer Modern is also available, but I do not recommend using it, because itcannot render minus signs in normal text.
Let's say you want to use Libre Baskerville.
- Download and install Libre Baskerville. (It will probably be freely availableas a package containing multiple font files.) On most operating systems, youcan simply unzip the package and double-click on each of the font files toinstall it.
- Find out where Matplotlib stores its cache.
$python3>>> import matplotlib as mpl>>> mpl.get_cachedir()
- Delete all font-related cache files in that location.
- Add the following lines to the file
light.mplstyle
.
font.family:LibreBaskervillemathtext.bf:LibreBaskerville:boldmathtext.it:LibreBaskerville:italicmathtext.rm:LibreBaskervillemathtext.fontset:custommathtext.fallback:stixmathtext.default:it
Sometimes, subplots (multiple plots in a single figure) don't look prettybecause elements from adjacent plots overlap with each other. To get aroundthis problem, you can interactively adjust the spacing by using the 'Configuresubplots' option in the navigation toolbar of the figure.
After doing this, un-maximise and maximise the figure window. As a result,anything that needs to be redrawn will be redrawn.
If the plot doesn't look quite right, try playing around with the DPI parameterbefore adjusting anything else. In the filelight.mplstyle
, search forfigure.dpi
and change the value next to it.
What the best value is depends on the resolution of your screen as well as itsscaling factor. In my experience, when the scaling factor is 1, a value of 192produces pretty graphs on a 1080p screen, while a 768p screen may requiresetting it to 135 or so. On a 720p screen, 128 should work well. (Reducing theDPI will also reduce the quality of the image. Proceed with caution.)
There are two types of discontinuities most graph plotters struggle with:
- essential discontinuities (like those in the graph ofy = tanx); and
- jump discontinuities (like those in the graph ofy = sgnx).
A vertical line is automatically drawn at each point of discontinuity. This issimply a result of the plotting algorithm used by graph plotters. If thishappens in your plot, try using thesanitise
function with a suitable valueofmaximum_diff
. (A demonstration can be found inexamples.py
.)
About
Matplotlib wrapper with some customisations to plot pretty graphs with very few lines of code. Plots can be updated using an interactive GUI.