Interactive online version: Binder badge

Plotting

Import the LArray library:

[1]:
from larray import *

Import the test array population from the demography_eurostat dataset:

[2]:
demography_eurostat = load_example_data('demography_eurostat')
population = demography_eurostat.population / 1_000_000

# show the 'population' array
population
[2]:
country  gender\time       2013       2014       2015       2016       2017
Belgium         Male   5.472856   5.493792   5.524068   5.569264   5.589272
Belgium       Female   5.665118   5.687048   5.713206   5.741853   5.762455
 France         Male  31.772665  32.045129  32.174258  32.247386  32.318973
 France       Female  33.827685  34.120851  34.283895  34.391005  34.485148
Germany         Male  39.380976  39.556923  39.835457  40.514123  40.697118
Germany       Female   41.14277   41.21054   41.36208  41.661561  41.824535

Inline matplotlib (required in notebooks):

[3]:
%matplotlib inline

In a Python script, add the following import on top of the script:

[4]:
import matplotlib.pyplot as plt

Create and show a simple plot (last axis define the different curves to draw):

[5]:
population['Belgium'].plot()
# shows the figure
plt.show()
../_images/tutorial_tutorial_plotting_10_0.png
  • Create a Line plot with grid, title, label on y axis and user-defined xticks.
  • Save the plot as a png file (using plt.savefig()).
  • Show the plot:
[6]:
population['Belgium'].plot(grid=True, xticks=population.time, title='Belgium')
# add a label aling the y axis
plt.ylabel('population (millions)')
# saves figure in a file (see matplotlib.pyplot.savefig documentation for more details)
plt.savefig('Belgium_population.png')
# WARNING: show() reset the current figure after showing it! Do not call it before savefig
plt.show()
../_images/tutorial_tutorial_plotting_12_0.png

Specify line styles and width:

[7]:
# line styles: '-' for solid line, '--' for dashed line, '-.' for dash-dotted line and ':' for dotted line
population['Male'].plot(style=['-', '--', '-.'], linewidth=2, xticks=population.time, title='Male')
plt.ylabel('population (millions)')
plt.show()
../_images/tutorial_tutorial_plotting_14_0.png

Move the legend inside the graph (using plt.legend(loc='position')):

[8]:
population['Belgium'].plot(xticks=population.time, title='Male')
plt.ylabel('population (millions)')
# available values for loc are:
# 'best' (default), 'upper right', 'upper left', 'lower left', 'lower right', 'right',
# center left', 'center right', 'lower center', 'upper center', 'center'
plt.legend(loc='lower right')
plt.show()
../_images/tutorial_tutorial_plotting_16_0.png

Put the legend outside the graph (using plt.legend(bbox_to_anchor=(x, y))):

[9]:
population['Belgium'].plot(xticks=population.time, title='Male')
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
../_images/tutorial_tutorial_plotting_18_0.png

Create a Bar plot:

[10]:
population['Belgium'].plot.bar(title='Belgium')
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
../_images/tutorial_tutorial_plotting_20_0.png

Create a stacked Bar plot:

[11]:
population['Belgium'].plot.bar(title='Belgium', stacked=True)
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
../_images/tutorial_tutorial_plotting_22_0.png

Create a multiplot figure (using plt.subplot(nrows,ncols,index)):

[12]:
figure, axes = plt.subplots(nrows=len(population.country), ncols=1, sharex=True, figsize=(5, 15))

for row, c in enumerate(population.country):
    population[c].plot(ax=axes[row], title=str(c))
    plt.ylabel('population (millions)')

plt.xticks(population.time)
plt.show()
../_images/tutorial_tutorial_plotting_24_0.png

See plot for more details and examples.

See pyplot tutorial for a short introduction to matplotlib.pyplot.