diff --git a/README.html b/README.html index 24291c1..6c8b075 100644 --- a/README.html +++ b/README.html @@ -161,7 +161,7 @@

Changing colors and line widths

figures/exercice_3.png

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

 ...
@@ -414,8 +414,7 @@ 

Figures

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).

-

As with other objects, you can set figure properties also setp or with the -set_something methods.

+

As with other objects, you can set figure properties with the set_something methods.

Subplots

@@ -504,10 +503,10 @@

Tick Locators

Animation

-

For quite a long time, animation in matplotlib was not an easy taks and was +

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 possiblity to save them in all kind of formats (but don't +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).

Documentation

@@ -610,7 +609,7 @@

Earthquakes

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 -eartquakes magnitude, ranging from significant only down to all earthquakes, +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, @@ -658,7 +657,7 @@

Earthquakes

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 -simmple to install and to use. First step is to define a projection to draw the +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.

@@ -709,7 +708,7 @@ 

Earthquakes

animation = FuncAnimation(fig, update, interval=10) plt.show()
-

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

+

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

figures/earthquakes.png
@@ -738,15 +737,16 @@

Regular Plots

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

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
 n = 256
 X = np.linspace(-np.pi,np.pi,n,endpoint=True)
 Y = np.sin(2*X)
 
-plot (X, Y+1, color='blue', alpha=1.00)
-plot (X, Y-1, color='blue', alpha=1.00)
-show()
+plt.plot (X, Y+1, color='blue', alpha=1.00)
+plt.plot (X, Y-1, color='blue', alpha=1.00)
+plt.show()
 

Click on figure for solution.

@@ -760,14 +760,15 @@

Scatter Plots

Starting from the code below, try to reproduce the graphic on the right taking care of marker size, color and transparency.

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
 n = 1024
 X = np.random.normal(0,1,n)
 Y = np.random.normal(0,1,n)
 
-scatter(X,Y)
-show()
+plt.scatter(X,Y)
+plt.show()
 

Click on figure for solution.

@@ -781,21 +782,22 @@

Bar Plots

Starting from the code below, try to reproduce the graphic on the right by adding labels for red bars.

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
 n = 12
 X = np.arange(n)
 Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
 Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
 
-bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
-bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
+plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
+plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
 
 for x,y in zip(X,Y1):
-    text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
+    plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
 
-ylim(-1.25,+1.25)
-show()
+plt.ylim(-1.25,+1.25)
+plt.show()
 

Click on figure for solution.

@@ -810,7 +812,8 @@

Contour Plots

Starting from the code below, try to reproduce the graphic on the right taking care of the colormap (see Colormaps below).

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
 def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
 
@@ -819,9 +822,9 @@ 

Contour Plots

y = np.linspace(-3,3,n) X,Y = np.meshgrid(x,y) -contourf(X, Y, f(X,Y), 8, alpha=.75, cmap='jet') -C = contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5) -show() +plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap='jet') +C = plt.contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5) +plt.show()

Click on figure for solution.

@@ -836,7 +839,8 @@

Imshow

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

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
 def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
 
@@ -844,7 +848,8 @@ 

Imshow

x = np.linspace(-3,3,4*n) y = np.linspace(-3,3,3*n) X,Y = np.meshgrid(x,y) -imshow(f(X,Y)), show() +plt.imshow(f(X,Y)) +plt.show()

Click on figure for solution.

@@ -858,11 +863,13 @@

Pie Charts

Starting from the code below, try to reproduce the graphic on the right taking care of colors and slices size.

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
 n = 20
 Z = np.random.uniform(0,1,n)
-pie(Z), show()
+plt.pie(Z)
+plt.show()
 

Click on figure for solution.

@@ -876,11 +883,13 @@

Quiver Plots

Starting from the code above, try to reproduce the graphic on the right taking care of colors and orientations.

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
 n = 8
 X,Y = np.mgrid[0:n,0:n]
-quiver(X,Y), show()
+plt.quiver(X,Y)
+plt.show()
 

Click on figure for solution.

@@ -890,7 +899,8 @@

Grids

Starting from the code below, try to reproduce the graphic on the right taking care of line styles.

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
 axes = gca()
 axes.set_xlim(0,4)
@@ -898,7 +908,7 @@ 

Grids

axes.set_xticklabels([]) axes.set_yticklabels([]) -show() +plt.show()

Click on figure for solution.

@@ -911,13 +921,14 @@

Multi Plots

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

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
-subplot(2,2,1)
-subplot(2,2,3)
-subplot(2,2,4)
+plt.subplot(2,2,1)
+plt.subplot(2,2,3)
+plt.subplot(2,2,4)
 
-show()
+plt.show()
 

Click on figure for solution.

@@ -930,21 +941,22 @@

Polar Axis

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

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 
-axes([0,0,1,1])
+plt.axes([0,0,1,1])
 
 N = 20
 theta = np.arange(0.0, 2*np.pi, 2*np.pi/N)
 radii = 10*np.random.rand(N)
 width = np.pi/4*np.random.rand(N)
-bars = bar(theta, radii, width=width, bottom=0.0)
+bars = plt.bar(theta, radii, width=width, bottom=0.0)
 
 for r,bar in zip(radii, bars):
     bar.set_facecolor( cm.jet(r/10.))
     bar.set_alpha(0.5)
 
-show()
+plt.show()
 

Click on figure for solution.

@@ -957,10 +969,11 @@

3D Plots

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

-from pylab import *
+import numpy as np
+import maplotlib.pyplot as plt
 from mpl_toolkits.mplot3d import Axes3D
 
-fig = figure()
+fig = plt.figure()
 ax = Axes3D(fig)
 X = np.arange(-4, 4, 0.25)
 Y = np.arange(-4, 4, 0.25)
@@ -970,7 +983,7 @@ 

3D Plots

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot') -show() +plt.show()

Click on figure for solution.

diff --git a/README.rst b/README.rst index c594b75..788997b 100644 --- a/README.rst +++ b/README.rst @@ -139,7 +139,7 @@ Changing colors and line widths :target: scripts/exercice_3.py First step, we want to have the cosine in blue and the sine in red and a -slighty thicker line for both of them. We'll also slightly alter the figure +slightly thicker line for both of them. We'll also slightly alter the figure size to make it more horizontal. @@ -416,8 +416,7 @@ 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). -As with other objects, you can set figure properties also setp or with the -set_something methods. +As with other objects, you can set figure properties with the set_something methods. Subplots @@ -521,10 +520,10 @@ matplotlib.dates. Animation ========= -For quite a long time, animation in matplotlib was not an easy taks and was +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 possiblity to save them in all kind of formats (but don't +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). .. admonition:: Documentation @@ -653,7 +652,7 @@ 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 -eartquakes magnitude, ranging from significant only down to all earthquakes, +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, @@ -708,7 +707,7 @@ 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 -simmple to install and to use. First step is to define a projection to draw the +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. @@ -769,7 +768,7 @@ put some eye candy: plt.show() -If eveything went well, you should obtain something like this (with animation): +If everything went well, you should obtain something like this (with animation): .. image:: figures/earthquakes.png :target: scripts/earthquakes.py @@ -832,15 +831,16 @@ Regular Plots Starting from the code below, try to reproduce the graphic on the right taking care of filled areas:: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt n = 256 X = np.linspace(-np.pi,np.pi,n,endpoint=True) Y = np.sin(2*X) - plot (X, Y+1, color='blue', alpha=1.00) - plot (X, Y-1, color='blue', alpha=1.00) - show() + plt.plot (X, Y+1, color='blue', alpha=1.00) + plt.plot (X, Y-1, color='blue', alpha=1.00) + plt.show() Click on figure for solution. @@ -863,14 +863,15 @@ care of marker size, color and transparency. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt n = 1024 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) - scatter(X,Y) - show() + plt.scatter(X,Y) + plt.show() Click on figure for solution. @@ -894,21 +895,22 @@ adding labels for red bars. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt n = 12 X = np.arange(n) Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) - bar(X, +Y1, facecolor='#9999ff', edgecolor='white') - bar(X, -Y2, facecolor='#ff9999', edgecolor='white') + plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white') + plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white') for x,y in zip(X,Y1): - text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom') + plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom') - ylim(-1.25,+1.25) - show() + plt.ylim(-1.25,+1.25) + plt.show() Click on figure for solution. @@ -931,7 +933,8 @@ care of the colormap (see `Colormaps`_ below). :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) @@ -940,9 +943,9 @@ care of the colormap (see `Colormaps`_ below). y = np.linspace(-3,3,n) X,Y = np.meshgrid(x,y) - contourf(X, Y, f(X,Y), 8, alpha=.75, cmap='jet') - C = contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5) - show() + plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap='jet') + C = plt.contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5) + plt.show() Click on figure for solution. @@ -967,7 +970,8 @@ care of colormap, image interpolation and origin. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) @@ -975,7 +979,8 @@ care of colormap, image interpolation and origin. x = np.linspace(-3,3,4*n) y = np.linspace(-3,3,3*n) X,Y = np.meshgrid(x,y) - imshow(f(X,Y)), show() + plt.imshow(f(X,Y)) + plt.show() Click on figure for solution. @@ -996,11 +1001,13 @@ care of colors and slices size. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt n = 20 Z = np.random.uniform(0,1,n) - pie(Z), show() + plt.pie(Z) + plt.show() Click on figure for solution. @@ -1022,11 +1029,13 @@ care of colors and orientations. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt n = 8 X,Y = np.mgrid[0:n,0:n] - quiver(X,Y), show() + plt.quiver(X,Y) + plt.show() Click on figure for solution. @@ -1045,7 +1054,8 @@ care of line styles. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt axes = gca() axes.set_xlim(0,4) @@ -1053,7 +1063,7 @@ care of line styles. axes.set_xticklabels([]) axes.set_yticklabels([]) - show() + plt.show() Click on figure for solution. @@ -1074,13 +1084,14 @@ Starting from the code below, try to reproduce the graphic on the right. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt - subplot(2,2,1) - subplot(2,2,3) - subplot(2,2,4) + plt.subplot(2,2,1) + plt.subplot(2,2,3) + plt.subplot(2,2,4) - show() + plt.show() Click on figure for solution. @@ -1101,21 +1112,22 @@ Starting from the code below, try to reproduce the graphic on the right. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt - axes([0,0,1,1]) + plt.axes([0,0,1,1]) N = 20 theta = np.arange(0.0, 2*np.pi, 2*np.pi/N) radii = 10*np.random.rand(N) width = np.pi/4*np.random.rand(N) - bars = bar(theta, radii, width=width, bottom=0.0) + bars = plt.bar(theta, radii, width=width, bottom=0.0) for r,bar in zip(radii, bars): bar.set_facecolor( cm.jet(r/10.)) bar.set_alpha(0.5) - show() + plt.show() Click on figure for solution. @@ -1137,10 +1149,11 @@ Starting from the code below, try to reproduce the graphic on the right. :: - from pylab import * + import numpy as np + import maplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D - fig = figure() + fig = plt.figure() ax = Axes3D(fig) X = np.arange(-4, 4, 0.25) Y = np.arange(-4, 4, 0.25) @@ -1150,7 +1163,7 @@ Starting from the code below, try to reproduce the graphic on the right. ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot') - show() + plt.show() Click on figure for solution. diff --git a/scripts/bar.py b/scripts/bar.py index 8fb0177..e59d3f1 100644 --- a/scripts/bar.py +++ b/scripts/bar.py @@ -1,28 +1,34 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt n = 16 X = np.arange(n) Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) -bar(X, +Y1, facecolor='#9999ff', edgecolor='white') -bar(X, -Y2, facecolor='#ff9999', edgecolor='white') -xlim(-.5,n), xticks([]) -ylim(-1,+1), yticks([]) +plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white') +plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white') +plt.xlim(-.5,n), plt.xticks([]) +plt.ylim(-1,+1), plt.yticks([]) -text(-0.05, 1.05, " Bar Plot \n\n", - horizontalalignment='left', - verticalalignment='top', - family='Lint McCree Intl BB', - size='x-large', - bbox=dict(facecolor='white', alpha=1.0, width=350,height=60), - transform = gca().transAxes) +plt.text(-0.05, 1.05, " Bar Plot \n\n", + horizontalalignment='left', + verticalalignment='top', + family='Lint McCree Intl BB', + size='x-large', + bbox=dict(facecolor='white', alpha=1.0, width=375, height=65), + transform = plt.gca().transAxes) -text(-0.05, .975, " Make a bar plot with rectangles ", - horizontalalignment='left', - verticalalignment='top', - family='Lint McCree Intl BB', - size='medium', - transform = gca().transAxes) +plt.text(-0.05, .975, " Make a bar plot with rectangles ", + horizontalalignment='left', + verticalalignment='top', + family='Lint McCree Intl BB', + size='medium', + transform = plt.gca().transAxes) -savefig('../figures/bar.png', dpi=64) +# plt.savefig('../figures/bar.png', dpi=64) +plt.show() diff --git a/scripts/bar_ex.py b/scripts/bar_ex.py index 5f24fa6..b9ca161 100644 --- a/scripts/bar_ex.py +++ b/scripts/bar_ex.py @@ -1,22 +1,27 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt n = 12 X = np.arange(n) Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) -axes([0.025,0.025,0.95,0.95]) -bar(X, +Y1, facecolor='#9999ff', edgecolor='white') -bar(X, -Y2, facecolor='#ff9999', edgecolor='white') +plt.axes([0.025,0.025,0.95,0.95]) +plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white') +plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white') for x,y in zip(X,Y1): - text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom') + plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom') for x,y in zip(X,Y2): - text(x+0.4, -y-0.05, '%.2f' % y, ha='center', va= 'top') + plt.text(x+0.4, -y-0.05, '%.2f' % y, ha='center', va= 'top') -xlim(-.5,n), xticks([]) -ylim(-1.25,+1.25), yticks([]) +plt.xlim(-.5,n), plt.xticks([]) +plt.ylim(-1.25,+1.25), plt.yticks([]) # savefig('../figures/bar_ex.png', dpi=48) -show() +plt.show() diff --git a/scripts/contour_ex.py b/scripts/contour_ex.py index 4641ec4..7b23fce 100644 --- a/scripts/contour_ex.py +++ b/scripts/contour_ex.py @@ -1,18 +1,24 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt -def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) +def f(x,y): + return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) n = 256 x = np.linspace(-3,3,n) y = np.linspace(-3,3,n) X,Y = np.meshgrid(x,y) -axes([0.025,0.025,0.95,0.95]) +plt.axes([0.025,0.025,0.95,0.95]) -contourf(X, Y, f(X,Y), 8, alpha=.75, cmap=cm.hot) -C = contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5) -clabel(C, inline=1, fontsize=10) +plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap=plt.cm.hot) +C = plt.contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5) +plt.clabel(C, inline=1, fontsize=10) -xticks([]), yticks([]) +plt.xticks([]), plt.yticks([]) # savefig('../figures/contour_ex.png',dpi=48) -show() +plt.show() diff --git a/scripts/grid_ex.py b/scripts/grid_ex.py index cc8f626..8f238d0 100644 --- a/scripts/grid_ex.py +++ b/scripts/grid_ex.py @@ -1,13 +1,18 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt -ax = axes([0.025,0.025,0.95,0.95]) +ax = plt.axes([0.025,0.025,0.95,0.95]) ax.set_xlim(0,4) ax.set_ylim(0,3) -ax.xaxis.set_major_locator(MultipleLocator(1.0)) -ax.xaxis.set_minor_locator(MultipleLocator(0.1)) -ax.yaxis.set_major_locator(MultipleLocator(1.0)) -ax.yaxis.set_minor_locator(MultipleLocator(0.1)) +ax.xaxis.set_major_locator(plt.MultipleLocator(1.0)) +ax.xaxis.set_minor_locator(plt.MultipleLocator(0.1)) +ax.yaxis.set_major_locator(plt.MultipleLocator(1.0)) +ax.yaxis.set_minor_locator(plt.MultipleLocator(0.1)) ax.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='0.75') ax.grid(which='minor', axis='x', linewidth=0.25, linestyle='-', color='0.75') ax.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='0.75') @@ -16,4 +21,4 @@ ax.set_yticklabels([]) # savefig('../figures/grid_ex.png',dpi=48) -show() +plt.show() diff --git a/scripts/imshow_ex.py b/scripts/imshow_ex.py index aa53555..7583cb5 100644 --- a/scripts/imshow_ex.py +++ b/scripts/imshow_ex.py @@ -1,6 +1,12 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt -def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) +def f(x,y): + return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) n = 10 x = np.linspace(-3,3,3.5*n) @@ -8,10 +14,10 @@ def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) X,Y = np.meshgrid(x,y) Z = f(X,Y) -axes([0.025,0.025,0.95,0.95]) -imshow(Z,interpolation='nearest', cmap='bone', origin='lower') -colorbar(shrink=.92) +plt.axes([0.025,0.025,0.95,0.95]) +plt.imshow(Z,interpolation='nearest', cmap='bone', origin='lower') +plt.colorbar(shrink=.92) -xticks([]), yticks([]) +plt.xticks([]), plt.yticks([]) # savefig('../figures/imshow_ex.png', dpi=48) -show() +plt.show() diff --git a/scripts/multiplot_ex.py b/scripts/multiplot_ex.py index 16e2238..6b7639d 100644 --- a/scripts/multiplot_ex.py +++ b/scripts/multiplot_ex.py @@ -1,19 +1,24 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt -fig =figure() +fig = plt.figure() fig.subplots_adjust(bottom=0.025, left=0.025, top = 0.975, right=0.975) -subplot(2,1,1) -xticks([]), yticks([]) +plt.subplot(2,1,1) +plt.xticks([]), plt.yticks([]) -subplot(2,3,4) -xticks([]), yticks([]) +plt.subplot(2,3,4) +plt.xticks([]), plt.yticks([]) -subplot(2,3,5) -xticks([]), yticks([]) +plt.subplot(2,3,5) +plt.xticks([]), plt.yticks([]) -subplot(2,3,6) -xticks([]), yticks([]) +plt.subplot(2,3,6) +plt.xticks([]), plt.yticks([]) -savefig('../figures/multiplot_ex.png',dpi=48) -show() +# plt.savefig('../figures/multiplot_ex.png',dpi=48) +plt.show() diff --git a/scripts/pie_ex.py b/scripts/pie_ex.py index f90a7f2..9a56881 100644 --- a/scripts/pie_ex.py +++ b/scripts/pie_ex.py @@ -1,14 +1,19 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt n = 20 Z = np.ones(n) Z[-1] *= 2 -axes([0.025,0.025,0.95,0.95]) +plt.axes([0.025,0.025,0.95,0.95]) -pie(Z, explode=Z*.05, colors = ['%f' % (i/float(n)) for i in range(n)]) -gca().set_aspect('equal') -xticks([]), plt.yticks([]) +plt.pie(Z, explode=Z*.05, colors = ['%f' % (i/float(n)) for i in range(n)]) +plt.gca().set_aspect('equal') +plt.xticks([]), plt.yticks([]) # savefig('../figures/pie_ex.png',dpi=48) -show() +plt.show() diff --git a/scripts/plot3d_ex.py b/scripts/plot3d_ex.py index 543da1d..e610922 100644 --- a/scripts/plot3d_ex.py +++ b/scripts/plot3d_ex.py @@ -1,7 +1,12 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D -fig = figure() +fig = plt.figure() ax = Axes3D(fig) X = np.arange(-4, 4, 0.25) Y = np.arange(-4, 4, 0.25) @@ -9,9 +14,9 @@ R = np.sqrt(X**2 + Y**2) Z = np.sin(R) -ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.hot) -ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=cm.hot) +ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot) +ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot) ax.set_zlim(-2,2) # savefig('../figures/plot3d_ex.png',dpi=48) -show() +plt.show() diff --git a/scripts/plot_ex.py b/scripts/plot_ex.py index 4350657..5734150 100644 --- a/scripts/plot_ex.py +++ b/scripts/plot_ex.py @@ -1,19 +1,24 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt n = 256 X = np.linspace(-np.pi,np.pi,n,endpoint=True) Y = np.sin(2*X) -axes([0.025,0.025,0.95,0.95]) +plt.axes([0.025,0.025,0.95,0.95]) -plot (X, Y+1, color='blue', alpha=1.00) -fill_between(X, 1, Y+1, color='blue', alpha=.25) +plt.plot (X, Y+1, color='blue', alpha=1.00) +plt.fill_between(X, 1, Y+1, color='blue', alpha=.25) -plot (X, Y-1, color='blue', alpha=1.00) -fill_between(X, -1, Y-1, (Y-1) > -1, color='blue', alpha=.25) -fill_between(X, -1, Y-1, (Y-1) < -1, color='red', alpha=.25) +plt.plot (X, Y-1, color='blue', alpha=1.00) +plt.fill_between(X, -1, Y-1, (Y-1) > -1, color='blue', alpha=.25) +plt.fill_between(X, -1, Y-1, (Y-1) < -1, color='red', alpha=.25) -xlim(-np.pi,np.pi), xticks([]) -ylim(-2.5,2.5), yticks([]) +plt.xlim(-np.pi,np.pi), plt.xticks([]) +plt.ylim(-2.5,2.5), plt.yticks([]) # savefig('../figures/plot_ex.png',dpi=48) -show() +plt.show() diff --git a/scripts/polar_ex.py b/scripts/polar_ex.py index ce3de0f..15f59dd 100644 --- a/scripts/polar_ex.py +++ b/scripts/polar_ex.py @@ -1,18 +1,23 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt -ax = axes([0.025,0.025,0.95,0.95], polar=True) +ax = plt.axes([0.025,0.025,0.95,0.95], polar=True) N = 20 theta = np.arange(0.0, 2*np.pi, 2*np.pi/N) radii = 10*np.random.rand(N) width = np.pi/4*np.random.rand(N) -bars = bar(theta, radii, width=width, bottom=0.0) +bars = plt.bar(theta, radii, width=width, bottom=0.0) for r,bar in zip(radii, bars): - bar.set_facecolor( cm.jet(r/10.)) + bar.set_facecolor( plt.cm.jet(r/10.)) bar.set_alpha(0.5) ax.set_xticklabels([]) ax.set_yticklabels([]) # savefig('../figures/polar_ex.png',dpi=48) -show() +plt.show() diff --git a/scripts/quiver_ex.py b/scripts/quiver_ex.py index cc66440..45cda6c 100644 --- a/scripts/quiver_ex.py +++ b/scripts/quiver_ex.py @@ -1,4 +1,9 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt n = 8 X,Y = np.mgrid[0:n,0:n] @@ -6,12 +11,12 @@ R = 10+np.sqrt((Y-n/2.0)**2+(X-n/2.0)**2) U,V = R*np.cos(T), R*np.sin(T) -axes([0.025,0.025,0.95,0.95]) -quiver(X,Y,U,V,R, alpha=.5) -quiver(X,Y,U,V, edgecolor='k', facecolor='None', linewidth=.5) +plt.axes([0.025,0.025,0.95,0.95]) +plt.quiver(X,Y,U,V,R, alpha=.5) +plt.quiver(X,Y,U,V, edgecolor='k', facecolor='None', linewidth=.5) -xlim(-1,n), xticks([]) -ylim(-1,n), yticks([]) +plt.xlim(-1,n), plt.xticks([]) +plt.ylim(-1,n), plt.yticks([]) # savefig('../figures/quiver_ex.png',dpi=48) -show() +plt.show() diff --git a/scripts/scatter_ex.py b/scripts/scatter_ex.py index 5dd87c1..0ef0abd 100644 --- a/scripts/scatter_ex.py +++ b/scripts/scatter_ex.py @@ -1,14 +1,19 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt n = 1024 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) T = np.arctan2(Y,X) -axes([0.025,0.025,0.95,0.95]) -scatter(X,Y, s=75, c=T, alpha=.5) +plt.axes([0.025,0.025,0.95,0.95]) +plt.scatter(X,Y, s=75, c=T, alpha=.5) -xlim(-1.5,1.5), xticks([]) -ylim(-1.5,1.5), yticks([]) +plt.xlim(-1.5,1.5), plt.xticks([]) +plt.ylim(-1.5,1.5), plt.yticks([]) # savefig('../figures/scatter_ex.png',dpi=48) -show() +plt.show() diff --git a/scripts/text_ex.py b/scripts/text_ex.py index 68b2030..2151ef6 100644 --- a/scripts/text_ex.py +++ b/scripts/text_ex.py @@ -1,4 +1,9 @@ -from pylab import * +# ----------------------------------------------------------------------------- +# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved. +# Distributed under the (new) BSD License. See LICENSE.txt for more info. +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt eqs = [] eqs.append((r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$")) @@ -8,7 +13,7 @@ eqs.append((r"$F_G = G\frac{m_1m_2}{r^2}$")) -axes([0.025,0.025,0.95,0.95]) +plt.axes([0.025,0.025,0.95,0.95]) for i in range(24): index = np.random.randint(0,len(eqs)) @@ -16,8 +21,9 @@ size = np.random.uniform(12,32) x,y = np.random.uniform(0,1,2) alpha = np.random.uniform(0.25,.75) - text(x, y, eq, ha='center', va='center', color="#11557c", alpha=alpha, - transform=gca().transAxes, fontsize=size, clip_on=True) -xticks([]), yticks([]) + plt.text(x, y, eq, ha='center', va='center', color="#11557c", alpha=alpha, + transform=plt.gca().transAxes, fontsize=size, clip_on=True) + +plt.xticks([]), plt.yticks([]) # savefig('../figures/text_ex.png',dpi=48) -show() +plt.show()