diff --git a/README.html b/README.html index f879ad5..5fab993 100644 --- a/README.html +++ b/README.html @@ -3,7 +3,7 @@ - + Matplotlib tutorial @@ -12,17 +12,17 @@

Matplotlib tutorial

Nicolas P. Rougier

-https://zenodo.org/badge/doi/10.5281/zenodo.28747.png +https://zenodo.org/badge/doi/10.5281/zenodo.28747.svg
-

Table of Contents

+

Table of Contents

Sources are available from @@ -30,25 +30,23 @@

Nicolas P. Rougier

All code and material is licensed under a Creative Commons Attribution-ShareAlike 4.0.

You can test your installation before the tutorial using the check-installation.py script.

-

Tutorial can be read at http://www.labri.fr/perso/nrougier/teaching/matplotlib/matplotlib.html

See also:

-

Introduction

+

Introduction

matplotlib is probably the single most used Python package for 2D-graphics. It provides both a very quick way to visualize data from Python and publication-quality figures in many formats. We are going to explore matplotlib in interactive mode covering most common cases.

-
-

IPython and the pylab mode

+
+

IPython

IPython is an enhanced interactive Python shell that has lots of interesting features including named inputs and outputs, access to -shell commands, improved debugging and many more. When we start it with the -command line argument -pylab (--pylab since IPython version 0.12), it allows +shell commands, improved debugging and much more. It allows interactive matplotlib sessions that have Matlab/Mathematica-like functionality.

@@ -60,18 +58,18 @@

pyplot

-

Simple plot

+

Simple plot

In this section, we want to draw the cosine and sine functions on the same plot. Starting from the default settings, we'll enrich the figure step by step to make it nicer.

-

First step is to get the data for the sine and cosine functions:

+

The first step is to get the data for the sine and cosine functions:

 import numpy as np
 
-X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
-C,S = np.cos(X), np.sin(X)
+X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
+C, S = np.cos(X), np.sin(X)
 
-

X is now a numpy array with 256 values ranging from -π to +π (included). C is +

X is now a NumPy array with 256 values ranging from -π to +π (included). C is the cosine (256 values) and S is the sine (256 values).

To run the example, you can download each of the examples and run it using:

@@ -80,7 +78,7 @@ 

Simple plot

You can get source for each step by clicking on the corresponding figure.

Using defaults

-
+

Documentation

  • plot tutorial
  • @@ -95,10 +93,10 @@

    Using defaults

    are rather good in most cases, you may want to modify some properties for specific cases.

    -import numpy as np
    -import matplotlib.pyplot as plt
    +import numpy as np
    +import matplotlib.pyplot as plt
     
    -X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
    +X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
     C,S = np.cos(X), np.sin(X)
     
     plt.plot(X,C)
    @@ -109,7 +107,7 @@ 

    Using defaults

Instantiating defaults

-
+

Documentation

  • Customizing matplotlib
  • @@ -122,16 +120,16 @@

    Instantiating defaults

    to explore their affect (see Line properties and Line styles below).

     # Imports
    -import numpy as np
    -import matplotlib.pyplot as plt
    +import numpy as np
    +import matplotlib.pyplot as plt
     
     # Create a new figure of size 8x6 points, using 100 dots per inch
    -plt.figure(figsize=(8,6), dpi=80)
    +plt.figure(figsize=(8,6), dpi=100)
     
     # Create a new subplot from a grid of 1x1
     plt.subplot(111)
     
    -X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
    +X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
     C,S = np.cos(X), np.sin(X)
     
     # Plot cosine using blue color with a continuous line of width 1 (pixels)
    @@ -144,13 +142,13 @@ 

    Instantiating defaults

    plt.xlim(-4.0,4.0) # Set x ticks -plt.xticks(np.linspace(-4,4,9,endpoint=True)) +plt.xticks(np.linspace(-4,4,9,endpoint=True)) # Set y limits plt.ylim(-1.0,1.0) # Set y ticks -plt.yticks(np.linspace(-1,1,5,endpoint=True)) +plt.yticks(np.linspace(-1,1,5,endpoint=True)) # Save figure using 72 dots per inch # savefig("../figures/exercice_2.png",dpi=72) @@ -161,7 +159,7 @@

    Instantiating defaults

Changing colors and line widths

-
+

Documentation

figures/exercice_3.png -

First step, we want to have the cosine in blue and the sine in red and a +

As a first step, we want to have the cosine in blue and the sine in red and a slightly thicker line for both of them. We'll also slightly alter the figure size to make it more horizontal.

@@ -182,7 +180,7 @@ 

Changing colors and line widths

Setting limits

-
+

Documentation

Setting ticks

-
+

Documentation

Setting tick labels

-
+

Documentation

Moving spines

-
+

Documentation

  • Spines
  • @@ -279,7 +277,7 @@

    Moving spines

Adding a legend

-
+

Documentation

Annotate some points

-
+

Documentation

figures/exercice_9.png -

Let's annotate some interesting points using the annotate command. We chose the +

Let's annotate some interesting points using the annotate command. We choose the 2π/3 value and we want to annotate both the sine and the cosine. We'll first draw a marker on the curve as well as a straight dotted line. Then, we'll use the annotate command to display some text with an arrow.

@@ -338,7 +336,7 @@

Annotate some points

Devil is in the details

-
+

Documentation

  • Artists
  • @@ -360,7 +358,7 @@

    Devil is in the details

-

Figures, Subplots, Axes and Ticks

+

Figures, Subplots, Axes and Ticks

So far we have used implicit figure and axes creation. This is handy for fast plots. We can have more control over the display using figure, subplot, and axes explicitly. A figure in matplotlib means the whole window in the user @@ -419,7 +417,7 @@

Figures

The defaults can be specified in the resource file and will be used most of the time. Only the number of the figure is frequently changed.

When you work with the GUI you can close a figure by clicking on the x in the -upper right corner. But you can close a figure programmatically by calling +upper right corner. You can also close a figure programmatically by calling close. Depending on the argument it closes (1) the current figure (no argument), (2) a specific figure (figure number or figure instance as argument), or (3) all figures (all as argument).

@@ -449,13 +447,13 @@

Ticks

figures. Matplotlib provides a totally configurable system for ticks. There are tick locators to specify where ticks should appear and tick formatters to give ticks the appearance you want. Major and minor ticks can be located and -formatted independently from each other. Per default minor ticks are not shown, +formatted independently from each other. By default minor ticks are not shown, i.e. there is only an empty list for them because it is as NullLocator (see below).

Tick Locators

There are several locators for different kind of requirements:

- +
@@ -511,13 +509,13 @@

Tick Locators

-

Animation

+

Animation

For quite a long time, animation in matplotlib was not an easy task and was done mainly through clever hacks. However, things have started to change since version 1.1 and the introduction of tools for creating animation very intuitively, with the possibility to save them in all kind of formats (but don't -expect to be able to run very complex animation at 60 fps though).

-
+expect to be able to run very complex animations at 60 fps though).

+

Documentation

  • See Animation
  • @@ -539,14 +537,14 @@

    Drip drop

    fig = plt.figure(figsize=(6,6), facecolor='white') # New axis over the whole figure, no frame and a 1:1 aspect ratio -ax = fig.add_axes([0,0,1,1], frameon=False, aspect=1) +ax = fig.add_axes([0,0,1,1], frameon=False, aspect=1)

    Next, we need to create several rings. For this, we can use the scatter plot object that is generally used to visualize points cloud, but we can also use it -to draw rings by specifying we don't have a facecolor. We have also to take -care of initial size and color for each ring such that we have all size between -a minimum and a maximum size and also to make sure the largest ring is almost -transparent.

    +to draw rings by specifying we don't have a facecolor. We also have to take +care of initial size and color for each ring such that we have all sizes between +a minimum and a maximum size. In addition, we need to make sure the largest ring +is almost transparent.

    figures/rain-static.png
     # Number of ring
    @@ -574,10 +572,10 @@ 

    Drip drop

    ax.set_ylim(0,1), ax.set_yticks([])

    Now, we need to write the update function for our animation. We know that at -each time step each ring should grow be more transparent while largest ring -should be totally transparent and thus removed. Of course, we won't actually -remove the largest ring but re-use it to set a new ring at a new random -position, with nominal size and color. Hence, we keep the number of ring +each time step each ring should grow and become more transparent while the +largest ring should be totally transparent and thus removed. Of course, we won't +actually remove the largest ring but re-use it to set a new ring at a new random +position, with nominal size and color. Hence, we keep the number of rings constant.

    figures/rain.gif
    @@ -607,24 +605,30 @@ 

    Drip drop

    Last step is to tell matplotlib to use this function as an update function for the animation and display the result or save it as a movie:

    -animation = FuncAnimation(fig, update, interval=10, blit=True, frames=200)
    +animation = FuncAnimation(fig, update, interval=10, blit=True, frames=200)
     # animation.save('rain.gif', writer='imagemagick', fps=30, dpi=40)
     plt.show()
     
    +

    If you use IPython, you'll have to render the animation into an html video +in order to show it in the Jupyter notebook:

    +
    +from IPython.display import HTML
    +HTML(animation.to_html5_video())
    +

Earthquakes

We'll now use the rain animation to visualize earthquakes on the planet from the last 30 days. The USGS Earthquake Hazards Program is part of the National Earthquake Hazards Reduction Program (NEHRP) and provides several data on their -website. Those data are sorted according to +website. Those data are sorted according to earthquakes magnitude, ranging from significant only down to all earthquakes, major or minor. You would be surprised by the number of minor earthquakes happening every hour on the planet. Since this would represent too much data for us, we'll stick to earthquakes with magnitude > 4.5. At the time of writing, this already represent more than 300 earthquakes in the last 30 days.

First step is to read and convert data. We'll use the urllib library that -allows to open and read remote data. Data on the website use the CSV format +allows us to open and read remote data. Data on the website use the CSV format whose content is given by the first line:

 time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type
@@ -635,10 +639,9 @@ 

Earthquakes

time of event (ok, that's bad, feel free to send me a PR).

 import urllib
-from mpl_toolkits.basemap import Basemap
 
-# -> http://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php
-feed = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/"
+# -> https://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php
+feed = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/"
 
 # Significant earthquakes in the last 30 days
 # url = urllib.request.urlopen(feed + "significant_month.csv")
@@ -654,41 +657,52 @@ 

Earthquakes

# Reading and storage of data data = url.read() -data = data.split(b'\n')[+1:-1] +data = data.split(b'\n')[+1:-1] E = np.zeros(len(data), dtype=[('position', float, 2), - ('magnitude', float, 1)]) + ('magnitude', float)]) for i in range(len(data)): - row = data[i].split(',') + row = data[i].split(b',') E['position'][i] = float(row[2]),float(row[1]) E['magnitude'][i] = float(row[4])
-

Now, we need to draw earth on a figure to show precisely where the earthquake +

Now, we need to draw the earth on a figure to show precisely where the earthquake center is and to translate latitude/longitude in some coordinates matplotlib -can handle. Fortunately, there is the basemap project (that tends to be replaced by the -more complete cartopy) that is really +can handle. Fortunately, there is the basemap project (which is now deprecated in favor +of the cartopy project) that is really simple to install and to use. First step is to define a projection to draw the earth onto a screen (there exists many different projections) and we'll stick to the mill projection which is rather standard for non-specialist like me.

+from mpl_toolkits.basemap import Basemap
 fig = plt.figure(figsize=(14,10))
 ax = plt.subplot(1,1,1)
 
-earth = Basemap(projection='mill')
+map = Basemap(projection='mill')
 

Next, we request to draw coastline and fill continents:

-earth.drawcoastlines(color='0.50', linewidth=0.25)
-earth.fillcontinents(color='0.95')
+map.drawcoastlines(color='0.50', linewidth=0.25)
+map.fillcontinents(color='0.95')
+
+

For cartopy, the steps are quite similar:

+
+import cartopy
+ax = plt.axes(projection=cartopy.crs.Miller())
+ax.coastlines(color='0.50', linewidth=0.25)
+ax.add_feature(cartopy.feature.LAND, color='0.95')
+ax.set_global()
+trans = cartopy.crs.PlateCarree()
 
-

The earth object will also be used to translate coordinate quite -automatically. We are almost finished. Last step is to adapt the rain code and -put some eye candy:

+

We are almost finished. Last step is to adapt the rain code and +put some eye candy. For basemap we use the map object to +transform the coordinates whereas for cartopy we use the transform_point +function of the chosen Miller projection:

 P = np.zeros(50, dtype=[('position', float, 2),
-                         ('size',     float, 1),
-                         ('growth',   float, 1),
-                         ('color',    float, 4)])
+                         ('size',    float),
+                         ('growth',  float),
+                         ('color',   float, 4)])
 scat = ax.scatter(P['position'][:,0], P['position'][:,1], P['size'], lw=0.5,
                   edgecolors = P['color'], facecolors='None', zorder=10)
 
@@ -700,7 +714,8 @@ 

Earthquakes

P['size'] += P['growth'] magnitude = E['magnitude'][current] - P['position'][i] = earth(*E['position'][current]) + P['position'][i] = map(*E['position'][current]) if use_basemap else \ + cartopy.crs.Miller().transform_point(*E['position'][current], cartopy.crs.PlateCarree()) P['size'][i] = 5 P['growth'][i]= np.exp(magnitude) * 0.1 @@ -715,7 +730,7 @@

Earthquakes

return scat, -animation = FuncAnimation(fig, update, interval=10) +animation = FuncAnimation(fig, update, interval=10, blit=True) plt.show()

If everything went well, you should obtain something like this (with animation):

@@ -723,7 +738,7 @@

Earthquakes

-

Other Types of Plots

+

Other Types of Plots

figures/plot.png figures/scatter.png figures/bar.png @@ -739,13 +754,13 @@

Other Types of Plots

Regular Plots

figures/plot_ex.png -
+

Hints

You need to use the fill_between command.

Starting from the code below, try to reproduce the graphic on the right taking -care of filled areas:

+care of filled areas.

 import numpy as np
 import matplotlib.pyplot as plt
@@ -763,7 +778,7 @@ 

Regular Plots

Scatter Plots

figures/scatter_ex.png -
+

Hints

Color is given by angle of (X,Y).

@@ -785,7 +800,7 @@

Scatter Plots

Bar Plots

figures/bar_ex.png -
+

Hints

You need to take care of text alignment.

@@ -814,7 +829,7 @@

Bar Plots

Contour Plots

figures/contour_ex.png -
+

Hints

You need to use the clabel command.

@@ -841,10 +856,10 @@

Contour Plots

Imshow

figures/imshow_ex.png -
+

Hints

You need to take care of the origin of the image in the imshow command and -use a colorbar

+use a colorbar.

Starting from the code below, try to reproduce the graphic on the right taking care of colormap, image interpolation and origin.

@@ -866,7 +881,7 @@

Imshow

Pie Charts

figures/pie_ex.png -
+

Hints

You need to modify Z.

@@ -886,7 +901,7 @@

Pie Charts

Quiver Plots

figures/quiver_ex.png -
+

Hints

You need to draw arrows twice.

@@ -925,7 +940,7 @@

Grids

Multi Plots

figures/multiplot_ex.png -
+

Hints

You can use several subplots with different partition.

@@ -945,9 +960,9 @@

Multi Plots

Polar Axis

figures/polar_ex.png -
+

Hints

-

You only need to modify the axes line

+

You only need to modify the axes line.

Starting from the code below, try to reproduce the graphic on the right.

@@ -973,9 +988,9 @@ 

Polar Axis

3D Plots

figures/plot3d_ex.png -
+

Hints

-

You need to use contourf

+

You need to use contourf.

Starting from the code below, try to reproduce the graphic on the right.

@@ -1000,16 +1015,16 @@ 

3D Plots

Text

figures/text_ex.png -
+

Hints

Have a look at the matplotlib logo.

-

Try to do the same from scratch !

+

Try to do the same from scratch!

Click on figure for solution.

-

Beyond this tutorial

+

Beyond this tutorial

Matplotlib benefits from extensive documentation as well as a large community of users and developpers. Here are some links of interest:

@@ -1091,8 +1106,8 @@

Code documentation

The code is fairly well documented and you can quickly access a specific command from within a python session:

->>> from pylab import *
->>> help(plot)
+>>> import matplotlib.pyplot as plt
+>>> help(plt)
 Help on function plot in module matplotlib.pyplot:
 
 plot(*args, **kwargs)
@@ -1117,7 +1132,6 @@ 

Galleries

The matplotlib gallery is also incredibly useful when you search how to render a given graphic. Each example comes with its source.

-

A smaller gallery is also available here.

Mailing lists

@@ -1127,11 +1141,11 @@

Mailing lists

-

Quick references

+

Quick references

Here is a set of tables that show main properties and styles.

Line properties

-
+
@@ -1218,7 +1232,7 @@

Line properties

Line styles

-
+
@@ -1361,7 +1375,7 @@

Line styles

Markers

-
+
@@ -1541,11 +1555,11 @@

Markers

Colormaps

All colormaps can be reversed by appending _r. For instance, gray_r is the reverse of gray.

-

If you want to know more about colormaps, checks Documenting the matplotlib +

If you want to know more about colormaps, see Documenting the matplotlib colormaps.

Base

-
+
@@ -1621,7 +1635,7 @@

Base

GIST

-
+
@@ -1663,9 +1677,9 @@

GIST

-
-

Sequential

- +
+

Diverging

+
@@ -1715,9 +1729,9 @@

Sequential

-
-

Diverging

- +
+

Sequential

+
@@ -1805,7 +1819,7 @@

Diverging

Qualitative

-
+
@@ -1853,7 +1867,7 @@

Qualitative

Miscellaneous

-
+
diff --git a/README.rst b/README.rst index d3645ab..57edf0d 100644 --- a/README.rst +++ b/README.rst @@ -23,13 +23,10 @@ Attribution-ShareAlike 4.0 You can test your installation before the tutorial using the `check-installation.py `_ script. -Tutorial can be read at http://www.labri.fr/perso/nrougier/teaching/matplotlib/matplotlib.html - - See also: -* `Numpy tutorial `_ -* `100 Numpy exercices `_ +* `From Python to Numpy `_ +* `100 Numpy exercices `_ * `Ten simple rules for better figures `_ @@ -41,13 +38,12 @@ provides both a very quick way to visualize data from Python and publication-quality figures in many formats. We are going to explore matplotlib in interactive mode covering most common cases. -IPython and the pylab mode --------------------------- +IPython +------- `IPython `_ is an enhanced interactive Python shell that has lots of interesting features including named inputs and outputs, access to -shell commands, improved debugging and much more. When we start it with the -command line argument -pylab (--pylab since IPython version 0.12), it allows +shell commands, improved debugging and much more. It allows interactive matplotlib sessions that have Matlab/Mathematica-like functionality. pyplot @@ -653,15 +649,25 @@ the animation and display the result or save it as a movie: # animation.save('rain.gif', writer='imagemagick', fps=30, dpi=40) plt.show() - - + +If you use IPython, you'll have to render the animation into an html video +in order to show it in the Jupyter notebook: + + +.. code:: python + + from IPython.display import HTML + HTML(animation.to_html5_video()) + + + Earthquakes ----------- We'll now use the rain animation to visualize earthquakes on the planet from the last 30 days. The USGS Earthquake Hazards Program is part of the National Earthquake Hazards Reduction Program (NEHRP) and provides several data on their -`website `_. Those data are sorted according to +`website `_. Those data are sorted according to earthquakes magnitude, ranging from significant only down to all earthquakes, major or minor. You would be surprised by the number of minor earthquakes happening every hour on the planet. Since this would represent too much data @@ -679,15 +685,14 @@ whose content is given by the first line:: We are only interested in latitude, longitude and magnitude and we won't parse time of event (ok, that's bad, feel free to send me a PR). - + .. code:: python import urllib - from mpl_toolkits.basemap import Basemap - # -> http://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php - feed = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/" + # -> https://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php + feed = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/" # Significant earthquakes in the last 30 days # url = urllib.request.urlopen(feed + "significant_month.csv") @@ -705,10 +710,10 @@ time of event (ok, that's bad, feel free to send me a PR). data = url.read() data = data.split(b'\n')[+1:-1] E = np.zeros(len(data), dtype=[('position', float, 2), - ('magnitude', float, 1)]) + ('magnitude', float)]) for i in range(len(data)): - row = data[i].split(',') + row = data[i].split(b',') E['position'][i] = float(row[2]),float(row[1]) E['magnitude'][i] = float(row[4]) @@ -716,39 +721,53 @@ time of event (ok, that's bad, feel free to send me a PR). Now, we need to draw the earth on a figure to show precisely where the earthquake center is and to translate latitude/longitude in some coordinates matplotlib can handle. Fortunately, there is the `basemap -`_ project (that tends to be replaced by the -more complete `cartopy `_) that is really +`_ project (which is now deprecated in favor +of the `cartopy `_ project) that is really simple to install and to use. First step is to define a projection to draw the earth onto a screen (there exists many different projections) and we'll stick to the `mill` projection which is rather standard for non-specialist like me. - + .. code:: python + from mpl_toolkits.basemap import Basemap fig = plt.figure(figsize=(14,10)) ax = plt.subplot(1,1,1) - earth = Basemap(projection='mill') + map = Basemap(projection='mill') Next, we request to draw coastline and fill continents: .. code:: python - - earth.drawcoastlines(color='0.50', linewidth=0.25) - earth.fillcontinents(color='0.95') -The `earth` object will also be used to translate coordinates quite -automatically. We are almost finished. Last step is to adapt the rain code and -put some eye candy: + map.drawcoastlines(color='0.50', linewidth=0.25) + map.fillcontinents(color='0.95') + +For cartopy, the steps are quite similar: + +.. code:: python + + import cartopy + ax = plt.axes(projection=cartopy.crs.Miller()) + ax.coastlines(color='0.50', linewidth=0.25) + ax.add_feature(cartopy.feature.LAND, color='0.95') + ax.set_global() + trans = cartopy.crs.PlateCarree() + + +We are almost finished. Last step is to adapt the rain code and +put some eye candy. For basemap we use the map object to +transform the coordinates whereas for cartopy we use the transform_point +function of the chosen Miller projection: .. code:: python P = np.zeros(50, dtype=[('position', float, 2), - ('size', float, 1), - ('growth', float, 1), - ('color', float, 4)]) + ('size', float), + ('growth', float), + ('color', float, 4)]) scat = ax.scatter(P['position'][:,0], P['position'][:,1], P['size'], lw=0.5, edgecolors = P['color'], facecolors='None', zorder=10) @@ -760,7 +779,8 @@ put some eye candy: P['size'] += P['growth'] magnitude = E['magnitude'][current] - P['position'][i] = earth(*E['position'][current]) + P['position'][i] = map(*E['position'][current]) if use_basemap else \ + cartopy.crs.Miller().transform_point(*E['position'][current], cartopy.crs.PlateCarree()) P['size'][i] = 5 P['growth'][i]= np.exp(magnitude) * 0.1 @@ -774,8 +794,8 @@ put some eye candy: scat.set_offsets(P['position']) return scat, - - animation = FuncAnimation(fig, update, interval=10) + + animation = FuncAnimation(fig, update, interval=10, blit=True) plt.show() @@ -785,45 +805,45 @@ If everything went well, you should obtain something like this (with animation): :target: scripts/earthquakes.py :width: 50% - + Other Types of Plots ==================== .. image:: figures/plot.png - :target: `Regular Plots`_ + :target: #regular-plots .. image:: figures/scatter.png - :target: `Scatter Plots`_ + :target: #scatter-plots .. image:: figures/bar.png - :target: `Bar Plots`_ + :target: #bar-plots .. image:: figures/contour.png - :target: `Contour Plots`_ + :target: #contour-plots .. image:: figures/imshow.png - :target: `Imshow`_ + :target: #imshow .. image:: figures/quiver.png - :target: `Quiver Plots`_ + :target: #quiver-plots .. image:: figures/pie.png - :target: `Pie Charts`_ + :target: #pie-charts .. image:: figures/grid.png - :target: `Grids`_ + :target: #grids .. image:: figures/multiplot.png - :target: `Multi Plots`_ + :target: #multi-plots .. image:: figures/polar.png - :target: `Polar Axis`_ + :target: #polar-axis .. image:: figures/plot3d.png - :target: `3D Plots`_ + :target: #3d-plots .. image:: figures/text.png - :target: `Text`_ + :target: #text Regular Plots @@ -1288,8 +1308,8 @@ command from within a python session: :: - >>> from pylab import * - >>> help(plot) + >>> import matplotlib.pyplot as plt + >>> help(plt) Help on function plot in module matplotlib.pyplot: plot(*args, **kwargs) @@ -1315,8 +1335,6 @@ The `matplotlib gallery `_ is also incredibly useful when you search how to render a given graphic. Each example comes with its source. -A smaller gallery is also available `here `_. - Mailing lists -------------- @@ -1757,7 +1775,7 @@ GIST - .. image:: figures/cmap-gist_yarg.png -Sequential +Diverging .......... .. list-table:: @@ -1796,7 +1814,7 @@ Sequential -Diverging +Sequential ......... .. list-table:: diff --git a/scripts/aliased.py b/scripts/aliased.py index fe2c5c3..08ac28e 100644 --- a/scripts/aliased.py +++ b/scripts/aliased.py @@ -1,16 +1,16 @@ -from pylab import * +import matplotlib.pyplot as plt size = 128,16 dpi = 72.0 figsize= size[0]/float(dpi),size[1]/float(dpi) -fig = figure(figsize=figsize, dpi=dpi) +fig = plt.figure(figsize=figsize, dpi=dpi) fig.patch.set_alpha(0) -axes([0,0,1,1], frameon=False) +plt.axes([0,0,1,1], frameon=False) -rcParams['text.antialiased'] = False -text(0.5,0.5,"Aliased",ha='center',va='center') +plt.rcParams['text.antialiased'] = False +plt.text(0.5,0.5,"Aliased",ha='center',va='center') plt.xlim(0,1),plt.ylim(0,1), plt.xticks([]),plt.yticks([]) -savefig('../figures/aliased.png', dpi=dpi) +plt.savefig('../figures/aliased.png', dpi=dpi) diff --git a/scripts/alpha.py b/scripts/alpha.py index 1dc7403..f22207d 100644 --- a/scripts/alpha.py +++ b/scripts/alpha.py @@ -1,15 +1,15 @@ -from pylab import * +import matplotlib.pyplot as plt size = 256,16 dpi = 72.0 figsize= size[0]/float(dpi),size[1]/float(dpi) -fig = figure(figsize=figsize, dpi=dpi) +fig = plt.figure(figsize=figsize, dpi=dpi) fig.patch.set_alpha(0) -axes([0,0.1,1,.8], frameon=False) +plt.axes([0,0.1,1,.8], frameon=False) for i in range(1,11): plt.axvline(i, linewidth=1, color='blue',alpha=.25+.75*i/10.) -xlim(0,11) -xticks([]),yticks([]) -savefig('../figures/alpha.png', dpi=dpi) +plt.xlim(0,11) +plt.xticks([]), plt.yticks([]) +plt.savefig('../figures/alpha.png', dpi=dpi) diff --git a/scripts/antialiased.py b/scripts/antialiased.py index 3b42c28..232dc57 100644 --- a/scripts/antialiased.py +++ b/scripts/antialiased.py @@ -1,16 +1,16 @@ -from pylab import * +import matplotlib.pyplot as plt size = 128,16 dpi = 72.0 figsize= size[0]/float(dpi),size[1]/float(dpi) -fig = figure(figsize=figsize, dpi=dpi) +fig = plt.figure(figsize=figsize, dpi=dpi) fig.patch.set_alpha(0) -axes([0,0,1,1], frameon=False) +plt.axes([0,0,1,1], frameon=False) -rcParams['text.antialiased'] = True -text(0.5,0.5,"Anti-aliased",ha='center',va='center') +plt.rcParams['text.antialiased'] = True +plt.text(0.5,0.5,"Anti-aliased",ha='center',va='center') plt.xlim(0,1),plt.ylim(0,1), plt.xticks([]),plt.yticks([]) -savefig('../figures/antialiased.png', dpi=dpi) +plt.savefig('../figures/antialiased.png', dpi=dpi) diff --git a/scripts/axes-2.py b/scripts/axes-2.py index 6e54a01..0a995d1 100644 --- a/scripts/axes-2.py +++ b/scripts/axes-2.py @@ -1,20 +1,20 @@ -from pylab import * +import matplotlib.pyplot as plt -axes([0.1,0.1,.5,.5]) -xticks([]), yticks([]) -text(0.1,0.1, 'axes([0.1,0.1,.5,.5])',ha='left',va='center',size=16,alpha=.5) +plt.axes([0.1,0.1,.5,.5]) +plt.xticks([]), plt.yticks([]) +plt.text(0.1,0.1, 'axes([0.1,0.1,.5,.5])',ha='left',va='center',size=16,alpha=.5) -axes([0.2,0.2,.5,.5]) -xticks([]), yticks([]) -text(0.1,0.1, 'axes([0.2,0.2,.5,.5])',ha='left',va='center',size=16,alpha=.5) +plt.axes([0.2,0.2,.5,.5]) +plt.xticks([]), plt.yticks([]) +plt.text(0.1,0.1, 'axes([0.2,0.2,.5,.5])',ha='left',va='center',size=16,alpha=.5) -axes([0.3,0.3,.5,.5]) -xticks([]), yticks([]) -text(0.1,0.1, 'axes([0.3,0.3,.5,.5])',ha='left',va='center',size=16,alpha=.5) +plt.axes([0.3,0.3,.5,.5]) +plt.xticks([]), plt.yticks([]) +plt.text(0.1,0.1, 'axes([0.3,0.3,.5,.5])',ha='left',va='center',size=16,alpha=.5) -axes([0.4,0.4,.5,.5]) -xticks([]), yticks([]) -text(0.1,0.1, 'axes([0.4,0.4,.5,.5])',ha='left',va='center',size=16,alpha=.5) +plt.axes([0.4,0.4,.5,.5]) +plt.xticks([]), plt.yticks([]) +plt.text(0.1,0.1, 'axes([0.4,0.4,.5,.5])',ha='left',va='center',size=16,alpha=.5) # plt.savefig("../figures/axes-2.png",dpi=64) -show() +plt.show() diff --git a/scripts/axes.py b/scripts/axes.py index 2cf495a..09d1527 100644 --- a/scripts/axes.py +++ b/scripts/axes.py @@ -1,12 +1,12 @@ -from pylab import * +import matplotlib.pyplot as plt -axes([0.1,0.1,.8,.8]) -xticks([]), yticks([]) -text(0.6,0.6, 'axes([0.1,0.1,.8,.8])',ha='center',va='center',size=20,alpha=.5) +plt.axes([0.1,0.1,.8,.8]) +plt.xticks([]), plt.yticks([]) +plt.text(0.6,0.6, 'axes([0.1,0.1,.8,.8])',ha='center',va='center',size=20,alpha=.5) -axes([0.2,0.2,.3,.3]) -xticks([]), yticks([]) -text(0.5,0.5, 'axes([0.2,0.2,.3,.3])',ha='center',va='center',size=16,alpha=.5) +plt.axes([0.2,0.2,.3,.3]) +plt.xticks([]), plt.yticks([]) +plt.text(0.5,0.5, 'axes([0.2,0.2,.3,.3])',ha='center',va='center',size=16,alpha=.5) plt.savefig("../figures/axes.png",dpi=64) -show() +plt.show()() diff --git a/scripts/check-installation.py b/scripts/check-installation.py index 759394f..9cd856b 100644 --- a/scripts/check-installation.py +++ b/scripts/check-installation.py @@ -37,23 +37,30 @@ else: print("ok") -# Check for basemap +# Check for basemap or cartopy try: - import mpl_toolkits.basemap as basemap + import mpl_toolkits.basemap as basemap + check_for_cartopy = False except: - print("This tutorial requires basemap\n") - sys.exit() -print("Check for basemap: ", end="") -if LooseVersion(basemap.__version__) < LooseVersion("1.0"): - print("basemape is too old (< 1.0) \n") - sys.exit() -else: - print("ok") - -# Check for urllib -try: - import urllib -except: - print("This tutorial requires urllib") + check_for_cartopy = True else: - print("Check for urllib: ok") + print("Check for basemap: ", end="") + if LooseVersion(basemap.__version__) < LooseVersion("1.0"): + print("basemap is too old (< 1.0) \n") + sys.exit() + else: + print("ok") + +if check_for_cartopy: + try: + import cartopy + except: + print("This tutorial requires either basemap or cartopy\n") + sys.exit() + + print("Check for cartopy: ", end="") + if LooseVersion(cartopy.__version__) < LooseVersion("0.15"): + print("cartopy is too old (< 0.15) \n") + sys.exit() + else: + print("ok") diff --git a/scripts/color.py b/scripts/color.py index 38a84d8..9d7e93b 100644 --- a/scripts/color.py +++ b/scripts/color.py @@ -1,14 +1,14 @@ -from pylab import * +import matplotlib.pyplot as plt size = 256,16 dpi = 72.0 figsize= size[0]/float(dpi),size[1]/float(dpi) -fig = figure(figsize=figsize, dpi=dpi) +fig = plt.figure(figsize=figsize, dpi=dpi) fig.patch.set_alpha(0) -axes([0,0.1,1,.8], frameon=False) +plt.axes([0,0.1,1,.8], frameon=False) for i in range(1,11): - plot( [i,i], [0,1], lw=1.5 ) -xlim(0,11) -xticks([]),yticks([]) -savefig('../figures/color.png', dpi=dpi) + plt.plot( [i,i], [0,1], lw=1.5 ) +plt.xlim(0,11) +plt.xticks([]), plt.yticks([]) +plt.savefig('../figures/color.png', dpi=dpi) diff --git a/scripts/colormaps.py b/scripts/colormaps.py index c2ee0d5..9f50fbe 100644 --- a/scripts/colormaps.py +++ b/scripts/colormaps.py @@ -1,4 +1,5 @@ -from pylab import * +import matplotlib.pyplot as plt +import numpy as np def colormap(cmap,filename): n = 512 @@ -8,14 +9,14 @@ def colormap(cmap,filename): figsize= size[0]/float(dpi),size[1]/float(dpi) fig = plt.figure(figsize=figsize, dpi=dpi) fig.patch.set_alpha(0) - axes([0.,0.,1.,1.], frameon=False) - xticks([]), yticks([]) - imshow(Z,aspect='auto',cmap=cmap,origin="lower") - savefig( "../figures/cmap-%s.png" % filename, dpi=dpi ) + plt.axes([0.,0.,1.,1.], frameon=False) + plt.xticks([]), plt.yticks([]) + plt.imshow(Z,aspect='auto',cmap=cmap,origin="lower") + plt.savefig( "../figures/cmap-%s.png" % filename, dpi=dpi ) -cmaps = [m for m in cm.datad if not m.endswith("_r")] +cmaps = [m for m in plt.cm.datad if not m.endswith("_r")] cmaps.sort() #print """ diff --git a/scripts/dash_capstyle.py b/scripts/dash_capstyle.py index 7411fc4..9b98be2 100644 --- a/scripts/dash_capstyle.py +++ b/scripts/dash_capstyle.py @@ -1,20 +1,21 @@ -from pylab import * +import matplotlib.pyplot as plt +import numpy as np size = 256,16 dpi = 72.0 figsize= size[0]/float(dpi),size[1]/float(dpi) -fig = figure(figsize=figsize, dpi=dpi) +fig = plt.figure(figsize=figsize, dpi=dpi) fig.patch.set_alpha(0) -axes([0,0,1,1], frameon=False) +plt.axes([0,0,1,1], frameon=False) -plot(np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'butt') +plt.plot(np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'butt') -plot(5+np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'round') +plt.plot(5+np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'round') -plot(10+np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'projecting') +plt.plot(10+np.arange(4), np.ones(4), color="blue", dashes=[15,15], linewidth=8, dash_capstyle = 'projecting') -xlim(0,14) -xticks([]),yticks([]) +plt.xlim(0,14) +plt.xticks([]), plt.yticks([]) -savefig('../figures/dash_capstyle.png', dpi=dpi) +plt.savefig('../figures/dash_capstyle.png', dpi=dpi) #show() diff --git a/scripts/dash_joinstyle.py b/scripts/dash_joinstyle.py index 8f2f682..dee29b5 100644 --- a/scripts/dash_joinstyle.py +++ b/scripts/dash_joinstyle.py @@ -1,18 +1,19 @@ -from pylab import * +import matplotlib.pyplot as plt +import numpy as np size = 256,16 dpi = 72.0 figsize= size[0]/float(dpi),size[1]/float(dpi) -fig = figure(figsize=figsize, dpi=dpi) +fig = plt.figure(figsize=figsize, dpi=dpi) fig.patch.set_alpha(0) -axes([0,0,1,1], frameon=False) +plt.axes([0,0,1,1], frameon=False) -plot(np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'miter') -plot(4+np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'bevel') -plot(8+np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'round') +plt.plot(np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'miter') +plt.plot(4+np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'bevel') +plt.plot(8+np.arange(3), [0,1,0], color="blue", dashes=[12,5], linewidth=8, dash_joinstyle = 'round') -xlim(0,12), ylim(-1,2) -xticks([]),yticks([]) +plt.xlim(0,12), plt.ylim(-1,2) +plt.xticks([]), plt.yticks([]) -savefig('../figures/dash_joinstyle.png', dpi=dpi) +plt.savefig('../figures/dash_joinstyle.png', dpi=dpi) #show() diff --git a/scripts/earthquakes.py b/scripts/earthquakes.py index 079c986..d1cebbd 100644 --- a/scripts/earthquakes.py +++ b/scripts/earthquakes.py @@ -10,14 +10,20 @@ import matplotlib matplotlib.rcParams['toolbar'] = 'None' import matplotlib.pyplot as plt -from mpl_toolkits.basemap import Basemap from matplotlib.animation import FuncAnimation +try: + from mpl_toolkits.basemap import Basemap + use_basemap = True +except ImportError: + import cartopy + use_basemap = False + # Open the earthquake data # ------------------------- -# -> http://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php -feed = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/" +# -> https://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php +feed = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/" # Significant earthquakes in the past 30 days # url = urllib.urlopen(feed + "significant_month.csv") @@ -35,7 +41,7 @@ data = url.read() data = data.split(b'\n')[+1:-1] E = np.zeros(len(data), dtype=[('position', float, 2), - ('magnitude', float, 1)]) + ('magnitude', float)]) for i in range(len(data)): row = data[i].split(b',') E['position'][i] = float(row[2]),float(row[1]) @@ -43,16 +49,24 @@ fig = plt.figure(figsize=(14,10)) -ax = plt.subplot(1,1,1) P = np.zeros(50, dtype=[('position', float, 2), - ('size', float, 1), - ('growth', float, 1), + ('size', float), + ('growth', float), ('color', float, 4)]) -# Basemap projection -map = Basemap(projection='mill') -map.drawcoastlines(color='0.50', linewidth=0.25) -map.fillcontinents(color='0.95') +if use_basemap: + ax = plt.subplot(1,1,1) + # Basemap projection + map = Basemap(projection='mill') + map.drawcoastlines(color='0.50', linewidth=0.25) + map.fillcontinents(color='0.95') +else: + # Cartopy projection + ax = plt.axes(projection=cartopy.crs.Miller()) + ax.coastlines(color='0.50', linewidth=0.25) + ax.add_feature(cartopy.feature.LAND, color='0.95') + ax.set_global() + scat = ax.scatter(P['position'][:,0], P['position'][:,1], P['size'], lw=0.5, edgecolors = P['color'], facecolors='None', zorder=10) @@ -65,7 +79,8 @@ def update(frame): P['size'] += P['growth'] magnitude = E['magnitude'][current] - P['position'][i] = map(*E['position'][current]) + P['position'][i] = map(*E['position'][current]) if use_basemap else \ + cartopy.crs.Miller().transform_point(*E['position'][current], cartopy.crs.PlateCarree()) P['size'][i] = 5 P['growth'][i]= np.exp(magnitude) * 0.1 @@ -77,7 +92,8 @@ def update(frame): scat.set_facecolors(P['color']*(1,1,1,0.25)) scat.set_sizes(P['size']) scat.set_offsets(P['position']) + return scat, plt.title("Earthquakes > 4.5 in the last 30 days") -animation = FuncAnimation(fig, update, interval=10) +animation = FuncAnimation(fig, update, interval=10, blit=True) plt.show() diff --git a/scripts/exercice_4-bis.py b/scripts/exercice_4-bis.py index fb88ec3..f37eefa 100644 --- a/scripts/exercice_4-bis.py +++ b/scripts/exercice_4-bis.py @@ -1,17 +1,18 @@ -from pylab import * +import matplotlib.pyplot as plt +import numpy as np -figure(figsize=(8,5), dpi=80) -subplot(111) +plt.figure(figsize=(8,5), dpi=80) +plt.subplot(111) X = np.linspace(-np.pi, np.pi, 256,endpoint=True) C,S = np.cos(X), np.sin(X) -plot(X, C, color="blue", linewidth=2.5, linestyle="-") -plot(X+.1, C, color="blue", linewidth=2.5, linestyle="-",alpha=.15) -plot(X, S, color="red", linewidth=2.5, linestyle="-") +plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-") +plt.plot(X+.1, C, color="blue", linewidth=2.5, linestyle="-",alpha=.15) +plt.plot(X, S, color="red", linewidth=2.5, linestyle="-") -xlim(X.min()*1.1, X.max()*1.1) -ylim(C.min()*1.1,C.max()*1.1) +plt.xlim(X.min()*1.1, X.max()*1.1) +plt.ylim(C.min()*1.1,C.max()*1.1) # savefig("../figures/exercice_4.png",dpi=72) -show() +plt.show()() diff --git a/scripts/linestyles.py b/scripts/linestyles.py index 83b9bdd..e062172 100644 --- a/scripts/linestyles.py +++ b/scripts/linestyles.py @@ -12,7 +12,7 @@ def linestyle(ls,name): plot(X,Y,ls,color=(.0,.0,1,1), lw=3, ms=10, mfc=(.75,.75,1,1), mec=(0,0,1,1)) xlim(0,10) xticks([]), yticks([]) - print'../figures/linestyle-%s.png' % name + print('../figures/linestyle-%s.png' % name) savefig('../figures/linestyle-%s.png' % name, dpi=dpi) for ls in ['-','--',':',',','o','^','v','<','>','s', diff --git a/scripts/markers.py b/scripts/markers.py index 9b813a2..4a82c9c 100644 --- a/scripts/markers.py +++ b/scripts/markers.py @@ -12,7 +12,7 @@ def marker(m,name): plot(X,Y,color='w', lw=1, marker=m, ms=10, mfc=(.75,.75,1,1), mec=(0,0,1,1)) xlim(0,10) xticks([]), yticks([]) - print '../figures/marker-%s.png' % name + print('../figures/marker-%s.png' % name) savefig('../figures/marker-%s.png' % name, dpi=dpi) #print"""