[Solved] how to make easy and efficient plots on Python


Matplotlib provides an object oriented API. This means that all the elements of the figure are acutally objects for which one can get and set properties and which can be easily manipulated. This makes matplotlib really flexible such that it can produce almost any plot you’d imagine.

Since a plot may consist of a hundred or more elements, a function that would allow the same flexibility would need that amount of possible arguments. It is not necessarily easier to remember all possible arguments of a function than all possible attributes of a class.

Having a single function call that does all of this, does not necessarily mean that you have to type in less characters. The commands would just be ordered differently.

Furthermore the object oriented approach allows to keep things seperate. Some properties of the axes, like the grid or the axis labels are completely independend on what you plot to the axes. So you wouldn’t want to set the xticks in the call to plot, because they are simply not related and it may be very confusing to set twice the same ticklabels when plotting two lines in the same axes.

On the other hand, matplotlib is really easy. In order to produce a plot you need two lines

import matplotlib.pyplot as plt
plt.plot([1,2,3],[2,1,3])

which sets most of the parameters exactly as they are needed. The more you want to customize this plot, the more settings you have to apply. Which is fine as it allows the user himself to determine how much in depth he wants to control the appearance of the plot.

Most matplotlib codes can be separated into three parts.

  1. Setting the style
  2. Creating the plot
  3. Customizing the plot

Setting the style in the case of the code from the question involves e.g. the ticklabel size and the use of a grid. Those properties can set as it’s done in the code but it may indeed be that one always wants to use the same properities here and finds it annoying to type the same parameters in every time one creates a plot. Therefore matplotlib provides general style settings, called rcParams. They can be set at the beginning of a script, e.g.

plt.rcParams['lines.linewidth'] = 2
plt.rcParams['axes.grid '] = True
plt.rcParams['axes.labelsize'] = 18

and will be applied to all plots within the script. It is also possible to define a complete stylesheet using those parameters. For more information see the Customizing matplotlib article.
It is equally possible to use predefined stylesheets for certain applications.
Simply importing import seaborn is also a possible way to change the style.

Creating the plot can not be simplified much more. It’s clear that one needs as many plotting commands as items to plot. Creating the figure and axes like

fig, ax = plt.subplots()

saves one line though.

Equally no simplification is possible if customizing ticks or tickmarks are required. One may however consider to use Tickers and Formatters for this purpose.

At the end one may of course consider to write a custom function which performs much of those tasks, but everyone can decide if that is useful for himself.

solved how to make easy and efficient plots on Python