Commit 2c7dcf47 authored by Loic Huder's avatar Loic Huder
Browse files

Added some info elements and examples to matplotlib tuto

parent b0fbe1b9
%% Cell type:markdown id: tags:
 
# Python training UGA 2017
 
**A training to acquire strong basis in Python to use it efficiently**
 
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTERRE), Oliver Henriot (GRICAD), Christophe Picard (LJK)
 
# Python scientific ecosystem
# A short introduction to Matplotlib ([gallery](http://matplotlib.org/gallery.html))
 
%% Cell type:markdown id: tags:
 
The default library to plot data is `Matplotlib`.
It allows one the creation of graphs that are ready for publications with the same functionality than Matlab.
 
%% Cell type:code id: tags:
 
``` python
# these ipython commands load special backend for notebooks
# (do not use "notebook" outside jupyter)
# %matplotlib notebook
# for jupyter-lab:
# %matplotlib ipympl
%matplotlib inline
```
 
%% Cell type:markdown id: tags:
 
When running code using matplotlib, it is highly recommended to start ipython with the option `--matplotlib` (or to use the magic ipython command `%matplotlib`).
 
%% Cell type:code id: tags:
 
``` python
import numpy as np
import matplotlib.pyplot as plt
```
 
%% Cell type:code id: tags:
 
``` python
A = np.random.random([5,5])
```
 
%% Cell type:markdown id: tags:
 
You can plot any kind of numerical data.
 
%% Cell type:code id: tags:
 
``` python
lines = plt.plot(A)
```
 
%% Output
 
 
%% Cell type:markdown id: tags:
 
In scripts, the `plt.show` method needs to be invoked at the end of the script.
 
%% Cell type:markdown id: tags:
 
We can plot data by giving specific coordinates.
 
%% Cell type:code id: tags:
 
``` python
x = np.linspace(0, 2, 20)
y = x**2
```
 
%% Cell type:code id: tags:
 
``` python
plt.figure()
plt.plot(x,y)
plt.plot(x,y, label='Square function')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
```
 
%% Output
 
Text(0, 0.5, 'y')
<matplotlib.legend.Legend at 0x7f7646bf5208>
 
 
%% Cell type:markdown id: tags:
 
We can associate the plot with an object figure. This object will allow us to add labels, subplot, modify the axis or save it as an image.
 
%% Cell type:code id: tags:
 
``` python
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, color="red", linestyle='dashed', linewidth=3, marker='o',
res = ax.plot(x, y, color="red", linestyle='dashed', linewidth=3, marker='o',
markerfacecolor='blue', markersize=5)
 
ax.set_xlabel('$Re$')
ax.set_ylabel('$\Pi / \epsilon$')
```
 
%% Output
 
Text(0, 0.5, '$\\Pi / \\epsilon$')
 
 
%% Cell type:markdown id: tags:
 
We can also recover the plotted matplotlib object to get info on it.
%% Cell type:code id: tags:
``` python
line_object = res[0]
print(type(line_object))
print('Color of the line is', line_object.get_color())
print('X data of the plot:', line_object.get_xdata())
```
%% Output
<class 'matplotlib.lines.Line2D'>
Color of the line is red
X data of the plot: [0. 0.10526316 0.21052632 0.31578947 0.42105263 0.52631579
0.63157895 0.73684211 0.84210526 0.94736842 1.05263158 1.15789474
1.26315789 1.36842105 1.47368421 1.57894737 1.68421053 1.78947368
1.89473684 2. ]
%% Cell type:markdown id: tags:
### Example of multiple subplots
%% Cell type:code id: tags:
``` python
fig = plt.figure()
ax1 = fig.add_subplot(211) # First, number of subplots along X (2), then along Y (1), then the id of the subplot (1)
ax2 = fig.add_subplot(212, sharex=ax1) # It is possible to share axes between subplots
X = np.arange(0, 2*np.pi, 0.1)
ax1.plot(X, np.cos(2*X), color="red")
ax2.plot(X, np.sin(2*X), color="magenta")
ax2.set_xlabel('Angle (rad)')
```
%% Output
Text(0.5, 0, 'Angle (rad)')
%% Cell type:markdown id: tags:
## Anatomy of a Matplotlib figure
 
![Anatomy of a figure](images/anatomy.png)
 
For consistent figure changes, define your own stylesheets that are basically a list of parameters to tune the aspect of the figure elements.
See https://matplotlib.org/tutorials/introductory/customizing.html for more info.
%% Cell type:markdown id: tags:
 
We can also plot array of data.
We can also plot 2D data arrays.
 
%% Cell type:code id: tags:
 
``` python
noise = np.random.random((256,256))
plt.figure()
plt.imshow(noise)
```
 
%% Output
 
<matplotlib.image.AxesImage at 0x7f765231f3c8>
 
 
%% Cell type:markdown id: tags:
 
We can also add a colobar and adjust the colormap.
 
%% Cell type:code id: tags:
 
``` python
plt.figure()
plt.imshow(noise, cmap=plt.cm.gray)
plt.colorbar()
```
 
%% Output
 
<matplotlib.colorbar.Colorbar at 0x7f76509fc588>