Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)
%% Cell type:markdown id: tags:
## Introduction
This is the second part of the introductive presentation given in the [Python initiation training](https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga/py-training-2017/blob/master/ipynb/pres111_intro_matplotlib.ipynb).
The aim is to present more advanced usecases of matplotlib.
%% Cell type:markdown id: tags:
## Quick reminders
%% Cell type:code id: tags:
``` python
importnumpyasnp
importmatplotlib.pyplotasplt
```
%% Cell type:code id: tags:
``` python
X=np.arange(0,2,0.01)
Y=np.exp(X)-1
plt.plot(X,X,linewidth=3)
plt.plot(X,Y)
plt.plot(X,X**2)
plt.xlabel('Abscisse')
plt.ylabel('Ordinate')
```
%% Cell type:markdown id: tags:
## Object-oriented plots
While doing the job, the previous example does not allow to unveil the power of matplotlib. For that, we need to keep in mind that in matplotlib plots, **everything** is an object.
Know first that other plotting libraries offers interactions more smoothly (`plotly`, `bokeh`, ...). Nevertheless, `matplotlib` gives access to backend-independent methods to add interactivity to plots.
These methods use [`Events`](https://matplotlib.org/api/backend_bases_api.html#matplotlib.backend_bases.Event) to catch user interactions (mouse clicks, key presses, mouse hovers, etc...).
These events must be connected to callback functions using the `mpl_connect` method of `Figure.Canvas`:
But, here we are referencing `x_data` and `y_data` in `add_datapoint` that are defined outside the function : this breaks encapsulation !
A nicer solution would be to use an object to handle the interactivity. We can also take advantage of this to add more functionality (such as clearing of the figure when the mouse exits) :
More examples could be shown but it always revolves around the same process: connecting an `Event` to a callback function.
Note that the connection can be severed using `mpl_disconnect` that takes the callback id in arg (in the previous case `self.button_callback` or `self.clear_callback`.
Some usages of interactivity:
- Print the value of a point on click
- Trigger a plot in the third dimension of a 3D plot displayed in 2D
- Save a figure on closing
- Ideas ?
%% Cell type:markdown id: tags:
## Animations
From the matplotlib page (https://matplotlib.org/api/animation_api.html):
> The easiest way to make a live animation in matplotlib is to use one of the Animation classes.
><table>
<tr><td>FuncAnimation</td><td>Makes an animation by repeatedly calling a function func.</td></tr>
<tr><td>ArtistAnimation</td><td>Animation using a fixed set of Artist objects.</td></tr>
</table>
%% Cell type:markdown id: tags:
### Example from matplotlib page
This example uses `FuncAnimation` to animate the plot of a sin function.
The animation consists in making repeated calls to the `update` function that adds at each frame a datapoint to the plot.
The previous code executed in a regular Python script should display the animation without problem. In a Jupyter Notebook, if we use `%matplotlib inline`, we can use IPython to display it in HTML.
%% Cell type:code id: tags:
``` python
fromIPython.displayimportHTML
HTML(ani.to_jshtml())
```
%% Cell type:markdown id: tags:
### Stroop test
The [Stroop effect](http://en.wikipedia.org/wiki/Stroop_effect) is when a psychological cause inteferes with the reaction time of a task.
A common demonstration of this effect (called a Stroop test) is naming the color in which a word is written if the word describes another color. This usually takes longer than for a word that is not a color.
Ex: Naming blue for <divstyle='text-align:center; font-size:36px'><spanstyle='color:blue'>RED</span> vs. <spanstyle='color:blue'>BIRD</span></div>
_Funfact: As this test relies on the significance of the words, people that are more used to English should find the test more difficult !_
In this part, we show how `matplotlib` animations can generate a Stroop test that shows random color words in random colors at random positions.
#### With `FuncAnimation`
We will generate a single object `word` whose position, color and text will be updated by the repeatedly called function.
Rather than updating through a function, `ArtistAnimation` requires to generate first all the `Artists` that will be displayed during the whole animation.
A list of `Artists` must therefore be supplied for each frame. Then, all frame lists must be compiled in a single list (of lists) that will be given in argument of `ArtistAnimation`.
In our case, to reproduce the behaviour above, we need to have only one word per frame. Each frame will therefore have a list of a single element (the colored word for this frame).