Git Product home page Git Product logo

tfpf / graph-plot Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 4.78 MB

Matplotlib wrapper with some customisations to plot pretty graphs with very few lines of code. Plots can be updated using an interactive GUI.

License: GNU General Public License v3.0

Python 100.00%
graph-plotter pyplot matplotlib-pyplot matplotlib matplotlib-python publication-quality-graphs plot graph-plot aesthetics polar-plots 2d-plot 3d-plot interactive interactive-plots plotting visualization

graph-plot's Introduction

Introduction

Customplot is a wrapper around Matplotlib. It can be used to plot beautiful, publication-quality graphs, which can optionally be controlled interactively.

Interactive Screengrab

I mainly wrote this to plot graphs of functions in the Cartesian plane, but it can be used for many other things, too. (Take a gander at the gallery.)

Requirements

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.

Usage

Call limit and polish on your Matplotlib axes instance before and after plotting, respectively. Check the file examples.py for details. You can run it 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 code in examples.py to another file, make modifications to that file, and run it.

Notes

General

Customplot cannot replace Matplotlib or Pyplot. It just does a few things which make the plot look pretty (to my eyes, at least). For sufficiently complicated plots, you may have to use functions of Matplotlib or Pyplot directly.

Only rectilinear, polar and 3d projections of Matplotlib axes are supported. Other projections can also be used, but their beautification may not happen automatically.

Interactive Plots

As seen in the image above, you can interactively adjust some plot elements of all Matplotlib axes in a figure (provided that the curses module is installed and available). To do so, just use customplot.show(fig) (fig being the Matplotlib figure instance) instead of plt.show(). More details can be found in examples.py.

Matplotlib is not thread-safe! Since the curses GUI is run in a separate thread, you may occasionally have to trigger additional GUI events (e.g. resizing the figure window) to have the desired effect.

Fonts

Using a good font can significantly improve the appearance of your plot. (Check out the gallery. The font used in those plots is Cochineal.)

In case you want nothing to do with fonts, you can ignore this section entirely.

Using a Packaged Font

Some fonts are packaged with Matplotlib; you can choose one of them. My suggestion is a STIX font. To use it, add the following lines to the file light.mplstyle (you can find this file in the styles directory).

font.family: STIXGeneral
mathtext.fontset: stix

Computer Modern is also available, but I do not recommend using it, because it cannot render minus signs in normal text.

Using a Custom Font

Let's say you want to use Libre Baskerville.

  • Download and install Libre Baskerville. (It will probably be freely available as a package containing multiple font files.) On most operating systems, you can simply unzip the package and double-click on each of the font files to install 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: Libre Baskerville
mathtext.bf: Libre Baskerville:bold
mathtext.it: Libre Baskerville:italic
mathtext.rm: Libre Baskerville
mathtext.fontset: custom
mathtext.fallback: stix
mathtext.default: it

Subplots

Sometimes, subplots (multiple plots in a single figure) don't look pretty because elements from adjacent plots overlap with each other. To get around this problem, you can interactively adjust the spacing by using the 'Configure subplots' 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.

DPI Settings

If the plot doesn't look quite right, try playing around with the DPI parameter before adjusting anything else. In the file light.mplstyle, search for figure.dpi and change the value next to it.

What the best value is depends on the resolution of your screen as well as its scaling factor. In my experience, when the scaling factor is 1, a value of 192 produces pretty graphs on a 1080p screen, while a 768p screen may require setting it to 135 or so. On a 720p screen, 128 should work well. (Reducing the DPI will also reduce the quality of the image. Proceed with caution.)

Essential and Jump Discontinuities

There are two types of discontinuities most graph plotters struggle with:

  • essential discontinuities (like those in the graph of y = tan x); and
  • jump discontinuities (like those in the graph of y = sgn x).

A vertical line is automatically drawn at each point of discontinuity. This is simply a result of the plotting algorithm used by graph plotters. If this happens in your plot, try using the sanitise function with a suitable value of maximum_diff. (A demonstration can be found in examples.py.)

Gallery

Function
Functions
Astroid
Folium of Descartes
Lemniscate
Population Distribution
Potentiometric Titration
Refractometry
Export Revenue
Oxygen Parameters

graph-plot's People

Contributors

tfpf avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.