
The codes of this case can be downloaded here
Matplotlib is a Python plot library which produces publication quality figures. This blog will introduce that how to plot and improve a figure.
Matplotlib is easy to install with Python tool pip
1 |
python -mpip install -U pip |
I suggest that you install Anaconda, Anaconda is a welcome Python data science platform, it includes many Python packages like Numpy, SciPy and Matplotlib.
The following parts of this blog will show you how to plot and improve a figure with Matplotlib step by step.
A simple case
Here is a simple case from the gallery of Matplotlib
1 |
import numpy as np |
In my github repository Learn Python, the procedure is described that how to remove the axes and the white margin of an image object that ploted by pyplot?. Just use fig = plt.figure() to produce a figure, then save it with
1 |
fig.savefig("pyplot.png", bbox_inches='tight', pad_inches = 0) |
The following shows the produced figure.

This figure is well enough to put into a paper or other normal publications, because it has different markers, clear ticks and a wrap border. But it can be improved to make it perfect.
Improvement
Axis ticks
Notice that the arrange of t is $[0,50]$, and the spacing between values is 1, but the tick on the figure is 10. A more appropriate axis ticks can be set with
1 |
ax.xaxis.set_major_locator(ticker.MultipleLocator(5.00)) |
Oh, you must create a subplot firstly
1 |
ax = plt.subplot(111) |
Now we have the following figure.

$rm TeX$ used format
Default font of the figure is not fit with $rm TeX$ font, if the text needs $rm TeX$ font, the following command should be included.
1 |
from matplotlib import rc |
For clarity, you’d better add label and legend and set the font size of label and ticks with
1 |
# Add label |
Now we have a new figure.

Add grid
Last but not least, a grid should be added to make the figure more accuracy,
1 |
ax.set_axisbelow(True) # Set grid below the figure border when the grid's color is not black. |
The purpose that setting ‘kwargs’ linewidth and color is to make a soft vision when you put it into a pdf file. If you use the default color black, when you zoom in the pdf, it will be like this

When you zoom out, guess what happend?

It looks sharply! A soft vision is quite important, too.
The final version of our figure is





近期评论