Skip to content

Commit afa1cfc

Browse files
committed
Updated some of the examples.
1 parent 401e907 commit afa1cfc

File tree

8 files changed

+120
-104
lines changed

8 files changed

+120
-104
lines changed

examples/pylab_examples/legend_demo.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env python
22
# Thanks to Charles Twardy for this example
33
#
4-
#See http://matplotlib.sf.net/examples/legend_demo2.py for an example
5-
#controlling which lines the legend uses and the order
4+
#See http://matplotlib.org/examples/pylab_examples/legend_demo2.html
5+
# for an example controlling which lines the legend uses and the order
6+
# they are drawn in.
67

78
import numpy as np
89
import matplotlib.pyplot as plt
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
#!/usr/bin/env python
2-
#
31
# Make a legend for specific lines.
4-
from pylab import *
2+
import matplotlib.pyplot as plt
3+
import numpy as np
54

6-
t1 = arange(0.0, 2.0, 0.1)
7-
t2 = arange(0.0, 2.0, 0.01)
5+
6+
t1 = np.arange(0.0, 2.0, 0.1)
7+
t2 = np.arange(0.0, 2.0, 0.01)
88

99
# note that plot returns a list of lines. The "l1, = plot" usage
10-
# extracts the first element of the list inot l1 using tuple
10+
# extracts the first element of the list into l1 using tuple
1111
# unpacking. So l1 is a Line2D instance, not a sequence of lines
12-
l1, = plot(t2, exp(-t2))
13-
l2, l3 = plot(t2, sin(2*pi*t2), '--go', t1, log(1+t1), '.')
14-
l4, = plot(t2, exp(-t2)*sin(2*pi*t2), 'rs-.')
12+
l1, = plt.plot(t2, np.exp(-t2))
13+
l2, l3 = plt.plot(t2, np.sin(2 * np.pi * t2), '--go', t1, np.log(1 + t1), '.')
14+
l4, = plt.plot(t2, np.exp(-t2) * np.sin(2 * np.pi * t2), 'rs-.')
1515

16-
legend( (l2, l4), ('oscillatory', 'damped'), 'upper right', shadow=True)
17-
xlabel('time')
18-
ylabel('volts')
19-
title('Damped oscillation')
20-
#axis([0,2,-1,1])
21-
show()
16+
plt.legend( (l2, l4), ('oscillatory', 'damped'), 'upper right', shadow=True)
17+
plt.xlabel('time')
18+
plt.ylabel('volts')
19+
plt.title('Damped oscillation')
20+
plt.show()
2221

2322

2423

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Shows how to combine Normalization and Colormap instances to draw
3+
"levels" in pcolor, pcolormesh and imshow type plots in a similar
4+
way to the levels keyword argument to contour/contourf.
5+
6+
"""
7+
8+
import matplotlib.pyplot as plt
9+
from matplotlib.colors import BoundaryNorm
10+
from matplotlib.ticker import MaxNLocator
11+
import numpy as np
12+
13+
14+
# make these smaller to increase the resolution
15+
dx, dy = 0.05, 0.05
16+
17+
# generate 2 2d grids for the x & y bounds
18+
y, x = np.mgrid[slice(1, 5 + dy, dy),
19+
slice(1, 5 + dx, dx)]
20+
21+
z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
22+
23+
# x and y are bounds, so z should be the value *inside* those bounds.
24+
# Therefore, remove the last value from the z array.
25+
z = z[:-1, :-1]
26+
levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())
27+
28+
29+
# pick the desired colormap, sensible levels, and define a normalization
30+
# instance which takes data values and translates those into levels.
31+
cmap = plt.get_cmap('PiYG')
32+
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
33+
34+
plt.subplot(2, 1, 1)
35+
im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm)
36+
plt.colorbar()
37+
# set the limits of the plot to the limits of the data
38+
plt.axis([x.min(), x.max(), y.min(), y.max()])
39+
plt.title('pcolormesh with levels')
40+
41+
42+
43+
plt.subplot(2, 1, 2)
44+
# contours are *point* based plots, so convert our bound into point
45+
# centers
46+
plt.contourf(x[:-1, :-1] + dx / 2.,
47+
y[:-1, :-1] + dy / 2., z, levels=levels,
48+
cmap=cmap)
49+
plt.colorbar()
50+
plt.title('contourf with levels')
51+
52+
53+
plt.show()

examples/pylab_examples/poormans_contour.py

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
#!/usr/bin/env python
21
"""
32
You can use TeX to render all of your matplotlib text if the rc
43
parameter text.usetex is set. This works currently on the agg and ps
54
backends, and requires that you have tex and the other dependencies
6-
described at http://matplotlib.sf.net/matplotlib.texmanager.html
5+
described at http://matplotlib.org/users/usetex.html
76
properly installed on your system. The first time you run a script
87
you will see a lot of output from tex and associated tools. The next
98
time, the run may be silent, as a lot of the information is cached in
109
~/.tex.cache
1110
1211
"""
13-
from matplotlib import rc
14-
from numpy import arange, cos, pi
15-
from matplotlib.pyplot import figure, axes, plot, xlabel, ylabel, title, \
16-
grid, savefig, show
12+
import numpy as np
13+
import matplotlib.pyplot as plt
1714

1815

19-
rc('text', usetex=True)
20-
rc('font', family='serif')
21-
figure(1, figsize=(6,4))
22-
ax = axes([0.1, 0.1, 0.8, 0.7])
23-
t = arange(0.0, 1.0+0.01, 0.01)
24-
s = cos(2*2*pi*t)+2
25-
plot(t, s)
16+
plt.rc('text', usetex=True)
17+
plt.rc('font', family='serif')
18+
plt.figure(1, figsize=(6,4))
19+
ax = plt.axes([0.1, 0.1, 0.8, 0.7])
20+
t = np.linspace(0.0, 1.0, 100)
21+
s = np.cos(4 * np.pi * t) + 2
22+
plt.plot(t, s)
2623

27-
xlabel(r'\textbf{time (s)}')
28-
ylabel(r'\textit{voltage (mV)}',fontsize=16)
29-
title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
30-
fontsize=16, color='r')
31-
grid(True)
32-
savefig('tex_demo')
33-
show()
24+
plt.xlabel(r'\textbf{time (s)}')
25+
plt.ylabel(r'\textit{voltage (mV)}',fontsize=16)
26+
plt.title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty"
27+
r"\frac{-e^{i\pi}}{2^n}$!", fontsize=16, color='r')
28+
plt.grid(True)
29+
plt.savefig('tex_demo')
30+
plt.show()
Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,59 @@
11
#!/usr/bin/env python
22
# -*- noplot -*-
3-
# This example shows how to use the agg backend directly to create
4-
# images, which may be of use to web application developers who want
5-
# full control over their code without using the pylab interface to
6-
# manage figures, figure closing etc.
7-
#
8-
# The rc command is used to create per-script default figure
9-
# customizations of the rc parameters; see
10-
# http://matplotlib.sf.net/matplotlibrc . You may prefer to set the
11-
# rc parameters in the rc file itself. Note that you can keep
12-
# directory level default configurations by placing different rc files
13-
# in the directory that the script runs in.
14-
#
15-
# I am making no effort here to make a figure that looks good --
16-
# rather I am just trying to show the various ways to use matplotlib
17-
# to customize your figure using the matplotlib API
3+
"""
4+
This example shows how to use the agg backend directly to create
5+
images, which may be of use to web application developers who want
6+
full control over their code without using the pyplot interface to
7+
manage figures, figure closing etc.
8+
9+
.. note::
10+
11+
It is not necessary to avoid using the pyplot interface in order to
12+
create figures without a graphical front-end - simply setting
13+
the backend to "Agg" would be sufficient.
14+
15+
16+
It is also worth noting that, because matplotlib can save figures to file-like
17+
object, matplotlib can also be used inside a cgi-script *without* needing to
18+
write a figure to disk.
19+
20+
"""
1821

19-
import matplotlib
20-
matplotlib.use('Agg') # force the antigrain backend
21-
from matplotlib import rc
2222
from matplotlib.backends.backend_agg import FigureCanvasAgg
2323
from matplotlib.figure import Figure
24-
from matplotlib.cbook import iterable
2524
import numpy as np
2625

26+
2727
def make_fig():
2828
"""
29-
make a figure
29+
Make a figure and save it to "webagg.png".
3030
31-
No need to close figures or clean up since the objects will be
32-
destroyed when they go out of scope
3331
"""
3432
fig = Figure()
35-
#ax = fig.add_subplot(111) # add a standard subplot
36-
37-
# add an axes at left, bottom, width, height; by making the bottom
38-
# at 0.3, we save some extra room for tick labels
39-
ax = fig.add_axes([0.2, 0.3, 0.7, 0.6])
33+
ax = fig.add_subplot(1, 1, 1)
4034

41-
line, = ax.plot([1,2,3], 'ro--', markersize=12, markerfacecolor='g')
35+
ax.plot([1, 2, 3], 'ro--', markersize=12, markerfacecolor='g')
4236

4337
# make a translucent scatter collection
4438
x = np.random.rand(100)
4539
y = np.random.rand(100)
46-
area = np.pi*(10 * np.random.rand(100))**2 # 0 to 10 point radiuses
47-
c = ax.scatter(x,y,area)
40+
area = np.pi * (10 * np.random.rand(100)) ** 2 # 0 to 10 point radiuses
41+
c = ax.scatter(x, y, area)
4842
c.set_alpha(0.5)
4943

5044
# add some text decoration
5145
ax.set_title('My first image')
5246
ax.set_ylabel('Some numbers')
53-
ax.set_xticks( (.2,.4,.6,.8) )
47+
ax.set_xticks((.2, .4, .6, .8))
5448
labels = ax.set_xticklabels(('Bill', 'Fred', 'Ted', 'Ed'))
5549

5650
# To set object properties, you can either iterate over the
5751
# objects manually, or define you own set command, as in setapi
5852
# above.
59-
for l in labels:
60-
l.set_rotation(45)
61-
l.set_fontsize(12)
53+
for label in labels:
54+
label.set_rotation(45)
55+
label.set_fontsize(12)
6256

63-
canvas = FigureCanvasAgg(fig)
64-
canvas.print_figure('webapp', dpi=150)
57+
FigureCanvasAgg(fig).print_png('webapp.png', dpi=150)
6558

66-
make_fig()
59+
make_fig()

examples/tests/backend_driver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
'nan_test.py',
211211
'newscalarformatter_demo.py',
212212
'pcolor_demo.py',
213+
'pcolor_demo2.py',
213214
'pcolor_log.py',
214215
'pcolor_small.py',
215216
'pie_demo2.py',

lib/matplotlib/ticker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,8 @@ def view_limits(self, vmin, vmax):
910910
"""
911911
select a scale for the range from vmin to vmax
912912
913-
Normally This will be overridden.
913+
Normally this method is overridden by subclasses to
914+
change locator behaviour.
914915
"""
915916
return mtransforms.nonsingular(vmin, vmax)
916917

0 commit comments

Comments
 (0)