Predefined Templates for Easy Plot Customization
What can you find here
A set of Matplotlib example scripts (predefined templates) aiming to remove ambiguity and to make life much easier for those whom are struggling to produce attractive plots, with a more than a default, basic aspect.
Each script is structured as a rich collection of customization snippets, in order to reduce googling and searching through lots of books and tutorials, thus aiming to save time and produce highly customized charts as fast and as easy as possible.
How scripts are structured
Every Matplotlib script contains a number of code snippets, each of them adding a specific functionality or customizing a certain figure component.
Method or function calls and their corresponding parameters are briefly explained. Almost every line has a comment with details such as: role (only where necessary: for example, the role of the “color” attribute is obvious, while “levels” might not be), potential parameter values (where applicable, such as symbols for various line styles, tick position options, etc.), useful links (such as link to colors and color names when the ‘color’ attribute appears for the first time in the program, links to font names, line styles and markers, etc.), advices (on some sections, when necessary) – in order to help user to instantly find all the information needed for that parameter, and to reduce as much as possible his/her searching effort through other sources of information.
Most of the function/method calls include not only the usual parameters, but goes toward full syntax, aiming to add more customization opportunities. Some of them are commented, allowing to user to un-comment them when needed. Users can/should change the existing parameter values so they can adapt the current example to their own needs. Links to official documentation and examples (if available) for every function or method are also given.
As an example, let's see the 'Save figure to file' code snippet from the bar chart predefined template:
For example, the figure from the following section ("How to use these templates") shows the list of code snippets from the bar chart script.
How to use these templates
First method is based solely on the complex file. Just take the complete code for any of the example charts and customize it only by changing the values of the existing parameters, as well as by commenting or un-commenting code lines according to your needs, thus having immediate access to alternative customization options.
The second method starts from a simple, very basic example file that generates a default, non-customized plot. These basic files are also provided. It is the recommended method when the user is primarily interested in making tests with the input data. Then, the plot can be improved by adding detailed snippets from the complex file. Apart from the complex file, the simple one is scarce on comments and information but is easy to understand. Each simple file starts with customization guidance.
For chart types that are not among these examples, one can easily adapt any detailed code snippet (Legend, Grid , X-ticks and Y-ticks, Annotations, etc.) to the new type of plot.
This figure shows the list of code snippets within the bar-chart complex script as well as plots generated by both the complex and basic scripts.
List of available scripts (templates)
For various plot types:
- Line plot
- Vertical bar chart
- Horizontal bar chart
- Bar stack
- Pie chart
- 3D plot
- Contour plot
- Filled contour plot
- Scatter plot
- 2D vector fields
- 2D streamlines
- Heatmaps with imshow()
- Heatmaps with pcolormesh()
For layout customization:
- Subplot layout (five ways of creating and controlling multiple subplot layouts: with pyplot interface; using the add_subplot() method; using GridSpec to create subplots template; object-oriented way, with the add_axes() method; using the pyplot.subplots() utility wrapper);
- Subplots with shared axis (three ways of synchronizing two different subplots, using sharex and sharey arguments);
- Subplots with twin axis (Share the x-axis between two different subplots and define different scales for each of the two y-axis; similar approach for one y-axis and two different x-axis);
- Spines customization (Invisible spines, spine properties, set individual spine limits, offset spines, and more …)
List of available detailed code snippets for each predefined template
To have a benefit from the information in this material and be comfortable with it, users should have basic Python and NumPy skills, and must have followed at least one beginners tutorial on Matplotlib.
There are numerous good, fast online learning resources one can choose from, such as Bernd Kleins’s tutorial website, or the three hours video lecture from Enthought. If this is the first time you are going to use Matplotlib, please return here after you follow a beginner tutorial and become accustomed with basic usage of this amazing Python plotting library.
Who might benefit from this material
It is supposed that the reader has basic skills in using Python, and in creating the input data to be plotted. For simplicity, data input was specified using NumPy arrays or Python lists. These higher level set of scripts with condensed information are mainly intended for:
- Matplotlib beginners;
- Anyone who does not use Matplotlib on a regular basis and need a fast way to plot a nice, almost fully customizable figure, and need to refresh their plotting knowledge;
- Anyone who feels confused or frustrated by the multiple options and solutions to create a plot, or by the endless ocean of more or less structured information regarding the use of Matplotlib, available on countless websites;
- Intermediate users might also appreciate some parts of this set of these example plots.
These scripts are not a replacement for any extensive Matplotlib online or offline documentation.
Need more info?
Open the 28 pages booklet for details that I hope will respond to your questions, as well as to see examples for each of the available Matplotlib scripts.
So, what we aimed to achieve with these predefined templates? Let's see:
- Fast and easy plot customization;
- Reduce googling and searching through books and tutorials;
- No need to be an advanced user;
- Detailed code snippets for almost each figure component;
- Useful links and suggestions at hand for almost every command line.