Matplotlib Tips

Scientific Computing Visualization Python


Matplotlib intro and tips

This post has a list of tiny matplotlib tips. There is a lot of indications on internet discussing about how to do things with matplotlib. Here, are written few cases to don’t forget about them! Some implementations might be changes for new versions of matplotlib (after v3 ahead).

How to Change the fonts

There are several possible directories where the matplotlib preference files are stored. In my Linux Distribution they are at /home/USER/.matplotlib, therefore to see if a specific font is avaliable we could write in the terminal.

more .matplotlib/fontList.cache | grep Gentium

Because this file has the names on the fontList.cache of matplotlib then to choose one font, for example, Gentium Basic that we have checked that is in fontList.cache

import matplotlib as mlp
mpl.rcParams[''] = 'Gentium Basic'

Also this can be change directly in the matplotlib rc file. I recommend also take a look to the matplotlib font API.

Plotting Time Series

Just an illustrative example on how to plot time series by including a date-time object.

import matplotlib.dates as mdates

years = mdates.YearLocator()          # every year
months = mdates.MonthLocator()        # every month
yearsFmt = mdates.DateFormatter('%Y')

plt.plot(ts_time, ts_val, ls='-', alpha=0.9, ms=4,
        marker='o', lw=1.4)
plt.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
plt.gca().set_xlim(['1978-01-15 00:00:00','2015-01-15 00:00:00'])