Skip to content

Commit

Permalink
Fix typos and remove pylab interface in favor of pyplot
Browse files Browse the repository at this point in the history
  • Loading branch information
rougier committed Aug 19, 2015
1 parent e297336 commit f6035f9
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 197 deletions.
103 changes: 58 additions & 45 deletions README.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ <h2>Changing colors and line widths</h2>
</div>
<a class="reference external image-reference" href="scripts/exercice_3.py"><img alt="figures/exercice_3.png" class="align-right" src="figures/exercice_3.png" /></a>
<p>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.</p>
<pre class="literal-block">
...
Expand Down Expand Up @@ -414,8 +414,7 @@ <h2>Figures</h2>
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).</p>
<p>As with other objects, you can set figure properties also setp or with the
set_something methods.</p>
<p>As with other objects, you can set figure properties with the set_something methods.</p>
</div>
<div class="section" id="subplots">
<h2>Subplots</h2>
Expand Down Expand Up @@ -504,10 +503,10 @@ <h3>Tick Locators</h3>
</div>
<div class="section" id="animation">
<h1><a class="toc-backref" href="#id7">Animation</a></h1>
<p>For quite a long time, animation in matplotlib was not an easy taks and was
<p>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).</p>
<div class="admonition-documentation admonition">
<p class="first admonition-title">Documentation</p>
Expand Down Expand Up @@ -610,7 +609,7 @@ <h2>Earthquakes</h2>
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
<a class="reference external" href="http://earthquake.usgs.gov">website</a>. 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 &gt; 4.5. At the time of writing,
Expand Down Expand Up @@ -658,7 +657,7 @@ <h2>Earthquakes</h2>
center is and to translate latitude/longitude in some coordinates matplotlib
can handle. Fortunately, there is the <a class="reference external" href="http://matplotlib.org/basemap/">basemap</a> project (that tends to be replaced by the
more complete <a class="reference external" href="http://scitools.org.uk/cartopy/">cartopy</a>) 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 <cite>mill</cite> projection which is rather standard for non-specialist like me.</p>
<pre class="code python literal-block">
Expand Down Expand Up @@ -709,7 +708,7 @@ <h2>Earthquakes</h2>
<span class="name">animation</span> <span class="operator">=</span> <span class="name">FuncAnimation</span><span class="punctuation">(</span><span class="name">fig</span><span class="punctuation">,</span> <span class="name">update</span><span class="punctuation">,</span> <span class="name">interval</span><span class="operator">=</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">show</span><span class="punctuation">()</span>
</pre>
<p>If eveything went well, you should obtain something like this (with animation):</p>
<p>If everything went well, you should obtain something like this (with animation):</p>
<a class="reference external image-reference" href="scripts/earthquakes.py"><img alt="figures/earthquakes.png" src="figures/earthquakes.png" style="width: 50%;" /></a>
</div>
</div>
Expand Down Expand Up @@ -738,15 +737,16 @@ <h2>Regular Plots</h2>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of filled areas:</p>
<pre class="literal-block">
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -760,14 +760,15 @@ <h2>Scatter Plots</h2>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of marker size, color and transparency.</p>
<pre class="literal-block">
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -781,21 +782,22 @@ <h2>Bar Plots</h2>
<p>Starting from the code below, try to reproduce the graphic on the right by
adding labels for red bars.</p>
<pre class="literal-block">
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -810,7 +812,8 @@ <h2>Contour Plots</h2>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of the colormap (see <a class="reference internal" href="#colormaps">Colormaps</a> below).</p>
<pre class="literal-block">
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)

Expand All @@ -819,9 +822,9 @@ <h2>Contour Plots</h2>
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -836,15 +839,17 @@ <h2>Imshow</h2>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of colormap, image interpolation and origin.</p>
<pre class="literal-block">
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)

n = 10
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -858,11 +863,13 @@ <h2>Pie Charts</h2>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of colors and slices size.</p>
<pre class="literal-block">
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -876,11 +883,13 @@ <h2>Quiver Plots</h2>
<p>Starting from the code above, try to reproduce the graphic on the right taking
care of colors and orientations.</p>
<pre class="literal-block">
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -890,15 +899,16 @@ <h2>Grids</h2>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of line styles.</p>
<pre class="literal-block">
from pylab import *
import numpy as np
import maplotlib.pyplot as plt

axes = gca()
axes.set_xlim(0,4)
axes.set_ylim(0,3)
axes.set_xticklabels([])
axes.set_yticklabels([])

show()
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -911,13 +921,14 @@ <h2>Multi Plots</h2>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right.</p>
<pre class="literal-block">
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -930,21 +941,22 @@ <h2>Polar Axis</h2>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right.</p>
<pre class="literal-block">
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()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand All @@ -957,10 +969,11 @@ <h2>3D Plots</h2>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right.</p>
<pre class="literal-block">
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)
Expand All @@ -970,7 +983,7 @@ <h2>3D Plots</h2>

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

show()
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
Expand Down
Loading

0 comments on commit f6035f9

Please sign in to comment.