Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dboyliao/matplotlib_notes
https://github.com/dboyliao/matplotlib_notes
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/dboyliao/matplotlib_notes
- Owner: dboyliao
- Created: 2014-07-25T09:17:31.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-02-28T05:53:02.000Z (over 8 years ago)
- Last Synced: 2024-03-15T01:43:08.860Z (8 months ago)
- Language: Jupyter Notebook
- Size: 1.85 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Notes for Matplolib
# Intro
There are four privmative drawing objects in `matplotlib`:
- `Line2D`: this object is used to present a 2D line on the graph or any other curves that can be Bezier-approximated.
- `AxesImage`: this object takes a 2D data and interprete it as densities with a colormap. This is usually returned by the `imshow` method.
- `Patch`: It represent a 2D object that has a single colored "face". This object must have "path" which is much like a Line2D object enclosing a a face to filled with that color.
- `Text`: It represent the text in a graph such as legends, labels,...etc. It takes a string, coordinates and font parameters to instantiate this object.Any sophisticated plots in matplotlib are build upon these four objects. In the following notes, I will mostly use these four objects to plot all the graph. See the python scripts in `pracs` directory for more advanced examples.
Switch to `interative` branch to see how to write interative matplotlib applications.
# Events and Callback
## Line Picking
```{python}
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
import picklewith open("../data/tracks.pickle") as pf:
tracks = pickle.load(pf)
tracks = dict((tid, t) for tid, t in tracks.items() if tid != -9)fig, ax = plt.subplots(1, 1)
# Define th callback function.
# `event` obj has some useful attributes such as `x`, `y` (the `position`), `xdata` and `ydata` (the `value`).
def onpick_callback(event):
if not isinstance(event.artist, LineCollection):
return
lws = event.artist.get_linewidths()
print event.ind
for i in event.ind:
lws[i] = 4 if lws[i] != 4 else 1
event.artist.set_linewidths(lws)
fig.canvas.draw_idle()
# `fig.canvas.draw_idle` is equivalent as `fig.canvas.draw` except the
# time when the drawing actually happen. `draw_idle` will wait in a queue
# to be queued.lc = LineCollection(tracks.values(), color = 'b', lw = [1 for _ in range(len(tracks))], picker = True)
ax.add_collection(lc)
ax.autoscale()fig.canvas.mpl_connect('pick_event', onpick_callback)
ax.set_xlabel('Longitude')
ax.set_ylabel("Latitude")plt.show()
```## Drawing Span
# Interative Applications
# References
- [Matplotlib Doc.](http://matplotlib.org/users/index.html)
- [Mapplotlib Gallery](http://matplotlib.org/gallery.html)
- [Challenges](http://www.labri.fr/perso/nrougier/teaching/matplotlib/#d-plots)
- [Colormaps](http://matplotlib.org/users/colormaps.html)
- [Transformation](http://matplotlib.org/users/transforms_tutorial.html)
- [Bezier Curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)
- [Patch Collections](http://matplotlib.org/examples/api/patch_collection.html)
- [3D subplots](http://matplotlib.org/examples/mplot3d/mixed_subplots_demo.html)
- [GridSpec](http://matplotlib.org/users/gridspec.html)
- [fig API](http://matplotlib.org/api/figure_api.html)
- [Matplotlib Collections](http://matplotlib.org/api/collections_api.html)