View on GitHub

CenTuri-Course

Python Course

← previous - home - next →

Table of contents

7. Preparing and saving a figure

Once we are happy with our figure, we can save it with fig.savefig. The function fig.savefig can take multiple out formats such as png, jpeg, pdf or svg:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
X1 = np.linspace(0, 2*np.pi)
X2 = np.linspace(0, 2*np.pi)
Y1 = np.sin(X1*2)/2
Y2 = np.cos(X2)

fig, ax = plt.subplots(figsize=(10, 3))
ax.plot(X1, Y1, '-', label='sin', lw=5)
ax.plot(X2, Y2, '-', label='cosin', lw=3, alpha=.5)
ax.fill_between(X1[5:30], Y1[5:30], Y2[5:30], color='y')
sns.despine(trim=True, offset=15, ax=ax)
ax.legend();
fig.savefig('Sin-test.pdf')
fig.savefig('Sin-test.png')
fig.savefig('Sin-test.jpeg')

png

You should see that the figure actually does not fit in the figure produced. It can happen sometimes (we choose here an example when it happens). There is a very efficient and practical way to solve the issue: fig.tight_layout:

import seaborn as sns
X1 = np.linspace(0, 2*np.pi)
X2 = np.linspace(0, 2*np.pi)
Y1 = np.sin(X1*2)/2
Y2 = np.cos(X2)

fig, ax = plt.subplots(figsize=(10, 3))
ax.plot(X1, Y1, '-', label='sin', lw=5)
ax.plot(X2, Y2, '-', label='cosin', lw=3, alpha=.5)
ax.fill_between(X1[5:30], Y1[5:30], Y2[5:30], color='y')
sns.despine(trim=True, offset=15, ax=ax)
ax.legend();
fig.tight_layout()
fig.savefig('Sin-test-2.png')

png

The function fig.tight_layout actually takes your current figure and adjusts it so that it fits as best as possible within your output figure layout, conserving all the current properties of the figure (the scaling for example). What it means is that to be as efficient as possible, this function should be called just before saving the figure.

Moreover, there are multiple potentially useful options to save a figure for example:

8. Using colormaps

Choosing colours can be a fairly hard task. Thankfully, matplotlib has a handful of colormaps already made for us, you can find them there.

The way colormaps usually work within matplotlib is by giving a colour code provided a value between 0 and 1. In other words, the colormap is a linear mapping between the interval $[0, 1]$ and the colours of the colormap.

Here is an example on how to use them:

# here, we get the colormap
cmap = mpl.cm.get_cmap('viridis')

X = np.linspace(0, 2*np.pi)
nb_curves = 30
fig, ax = plt.subplots(figsize=(10, 8))
for i in range(nb_curves):
    curve = np.sin(X + i*2*np.pi/nb_curves)
    color = cmap(i/nb_curves)
    ax.plot(curve, color=color)

png

While, unfortunately, I don’t know a way of listing all the available colormaps in matplotlib (you should therefore go there), calling mpl.cm.get_cmap without assigning it to a variable plots the colormap:

mpl.cm.get_cmap('Spectral')

Exercise 1:

Make a plot that resemble the following one:

png

To do so you will need to use the parameter zorder from ax.plot and ax.fill_between. You can find some documentation there.

You can find in Resources.UsefulFunctions the function build_curve to help you generate random curves. That being said, you building such a function would be a good training (that’s why it is not directly accessible from the help, to prevent a bit the temptation)

np.random.seed(1)
from Resources.UsefulFunctions import build_curve
...
Ellipsis

Now, we know more or less how to plot curves using matplotlib. While looking at curves is interesting it might not encapsulate everything you would need. For example, when looking at the distribution of a given variable, curves are less useful. There are many other ways to plot data, we will go through some amount of it through this session.

If there are specific types of plots that you can think of that we will not deal with during this course, feel free to let me know.

Here is the list of plots that we will look at:

We will NOT look at barplots.

← previous - home - next →