Import the LArray library:
In [ ]:
from larray import *
Import the test array population
from the demography_eurostat
dataset:
In [ ]:
demography_eurostat = load_example_data('demography_eurostat')
population = demography_eurostat.population / 1_000_000
# show the 'population' array
population
Inline matplotlib (required in notebooks):
In [ ]:
%matplotlib inline
In a Python script, add the following import on top of the script:
In [ ]:
import matplotlib.pyplot as plt
Create and show a simple plot (last axis define the different curves to draw):
In [ ]:
population['Belgium'].plot()
# shows the figure
plt.show()
plt.savefig()
).
In [ ]:
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()
Specify line styles and width:
In [ ]:
# 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()
Move the legend inside the graph (using plt.legend(loc='position')
):
In [ ]:
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()
Put the legend outside the graph (using plt.legend(bbox_to_anchor=(x, y))
):
In [ ]:
population['Belgium'].plot(xticks=population.time, title='Male')
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
Create a Bar plot:
In [ ]:
population['Belgium'].plot.bar(title='Belgium')
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
Create a stacked Bar plot:
In [ ]:
population['Belgium'].plot.bar(title='Belgium', stacked=True)
plt.ylabel('population (millions)')
plt.legend(bbox_to_anchor=(1.25, 0.6))
plt.show()
Create a multiplot figure (using plt.subplot(nrows,ncols,index)
):
In [ ]:
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()
See plot for more details and examples.
See pyplot tutorial for a short introduction to matplotlib.pyplot
.