Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b6c035

Browse files
committed
more doc examples and cleanups
svn path=/trunk/matplotlib/; revision=5688
1 parent 4838fbe commit 8b6c035

9 files changed

Lines changed: 175 additions & 140 deletions

File tree

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
1-
#!/usr/bin/env python
1+
import numpy as np
2+
import matplotlib.pyplot as plt
23

3-
from pylab import *
4-
figure(1)
5-
plot(10*rand(12), 'o')
6-
xlim(0,15)
7-
xticks([2, 4, 8, 12], ('John', 'Hunter', 'Was', 'Here'))
4+
t = np.arange(-1,2, .01)
5+
s = np.sin(2*np.pi*t)
86

9-
ylim(-1,10)
10-
yticks(range(8))
11-
12-
figure(2)
13-
t = arange(-1,2, .01)
14-
s = sin(2*pi*t)
15-
plot(t,s)
7+
plt.plot(t,s)
168
# draw a thick red hline at y=0 that spans the xrange
17-
l = axhline(linewidth=4, color='r')
9+
l = plt.axhline(linewidth=4, color='r')
1810

1911
# draw a default hline at y=1 that spans the xrange
20-
l = axhline(y=1)
12+
l = plt.axhline(y=1)
2113

2214
# draw a default vline at x=1 that spans the xrange
23-
l = axvline(x=1)
15+
l = plt.axvline(x=1)
2416

2517
# draw a thick blue vline at x=0 that spans the the upper quadrant of
2618
# the yrange
27-
l = axvline(x=0, ymin=0.75, linewidth=4, color='b')
19+
l = plt.axvline(x=0, ymin=0.75, linewidth=4, color='b')
2820

2921
# draw a default hline at y=.5 that spans the the middle half of
3022
# the axes
31-
l = axhline(y=.5, xmin=0.25, xmax=0.75)
23+
l = plt.axhline(y=.5, xmin=0.25, xmax=0.75)
3224

33-
p = axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5)
25+
p = plt.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5)
3426

35-
p = axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
27+
p = plt.axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
3628

37-
axis([-1,2,-1,2])
29+
plt.axis([-1,2,-1,2])
3830

3931

40-
show()
32+
plt.show()
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11

22
#!/usr/bin/env python
3-
# a bar plot with errorbars
4-
from numpy import arange
5-
from matplotlib.pyplot import *
3+
import numpy as np
4+
import matplotlib.pyplot as plt
65

76
N = 5
87
menMeans = (20, 35, 30, 35, 27)
98
menStd = (2, 3, 4, 1, 2)
109

11-
ind = arange(N) # the x locations for the groups
10+
ind = np.arange(N) # the x locations for the groups
1211
width = 0.35 # the width of the bars
1312

14-
figure()
15-
subplot(111)
16-
rects1 = bar(ind, menMeans, width, color='r', yerr=menStd)
13+
14+
plt.subplot(111)
15+
rects1 = plt.bar(ind, menMeans, width, color='r', yerr=menStd)
1716

1817
womenMeans = (25, 32, 34, 20, 25)
1918
womenStd = (3, 5, 2, 3, 3)
20-
rects2 = bar(ind+width, womenMeans, width, color='y', yerr=womenStd)
19+
rects2 = plt.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)
2120

2221
# add some
23-
ylabel('Scores')
24-
title('Scores by group and gender')
25-
xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5') )
22+
plt.ylabel('Scores')
23+
plt.title('Scores by group and gender')
24+
plt.xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5') )
2625

27-
legend( (rects1[0], rects2[0]), ('Men', 'Women') )
26+
plt.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
2827

2928
def autolabel(rects):
3029
# attach some text labels
3130
for rect in rects:
3231
height = rect.get_height()
33-
text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
32+
plt.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
3433
ha='center', va='bottom')
3534

3635
autolabel(rects1)
3736
autolabel(rects2)
3837
#savefig('barchart_demo')
39-
show()
38+
plt.show()
Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
#!/usr/bin/env python
2-
'''
2+
"""
33
Illustrate simple contour plotting, contours on an image with
44
a colorbar for the contours, and labelled contours.
55
66
See also contour_image.py.
7-
'''
8-
from pylab import *
7+
"""
8+
import matplotlib
9+
import numpy as np
10+
import matplotlib.cm as cm
11+
import matplotlib.mlab as mlab
12+
import matplotlib.pyplot as plt
913

10-
rcParams['xtick.direction'] = 'out'
11-
rcParams['ytick.direction'] = 'out'
14+
matplotlib.rcParams['xtick.direction'] = 'out'
15+
matplotlib.rcParams['ytick.direction'] = 'out'
1216

1317
delta = 0.025
14-
x = arange(-3.0, 3.0, delta)
15-
y = arange(-2.0, 2.0, delta)
16-
X, Y = meshgrid(x, y)
17-
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
18-
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
18+
x = np.arange(-3.0, 3.0, delta)
19+
y = np.arange(-2.0, 2.0, delta)
20+
X, Y = np.meshgrid(x, y)
21+
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
22+
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
1923
# difference of Gaussians
2024
Z = 10.0 * (Z2 - Z1)
2125

@@ -25,77 +29,77 @@
2529
# inline argument to clabel will control whether the labels are draw
2630
# over the line segments of the contour, removing the lines beneath
2731
# the label
28-
figure()
29-
CS = contour(X, Y, Z)
30-
clabel(CS, inline=1, fontsize=10)
31-
title('Simplest default with labels')
32+
plt.figure()
33+
CS = plt.contour(X, Y, Z)
34+
plt.clabel(CS, inline=1, fontsize=10)
35+
plt.title('Simplest default with labels')
3236

3337

3438
# You can force all the contours to be the same color.
35-
figure()
36-
CS = contour(X, Y, Z, 6,
37-
colors='k', # negative contours will be dashed by default
38-
)
39-
clabel(CS, fontsize=9, inline=1)
40-
title('Single color - negative contours dashed')
39+
plt.figure()
40+
CS = plt.contour(X, Y, Z, 6,
41+
colors='k', # negative contours will be dashed by default
42+
)
43+
plt.clabel(CS, fontsize=9, inline=1)
44+
plt.title('Single color - negative contours dashed')
4145

4246
# You can set negative contours to be solid instead of dashed:
43-
rcParams['contour.negative_linestyle'] = 'solid'
44-
figure()
45-
CS = contour(X, Y, Z, 6,
46-
colors='k', # negative contours will be dashed by default
47-
)
48-
clabel(CS, fontsize=9, inline=1)
49-
title('Single color - negative contours solid')
47+
matplotlib.rcParams['contour.negative_linestyle'] = 'solid'
48+
plt.figure()
49+
CS = plt.contour(X, Y, Z, 6,
50+
colors='k', # negative contours will be dashed by default
51+
)
52+
plt.clabel(CS, fontsize=9, inline=1)
53+
plt.title('Single color - negative contours solid')
5054

5155

5256
# And you can manually specify the colors of the contour
53-
figure()
54-
CS = contour(X, Y, Z, 6,
55-
linewidths=arange(.5, 4, .5),
56-
colors=('r', 'green', 'blue', (1,1,0), '#afeeee', '0.5')
57-
)
58-
clabel(CS, fontsize=9, inline=1)
59-
title('Crazy lines')
57+
plt.figure()
58+
CS = plt.contour(X, Y, Z, 6,
59+
linewidths=np.arange(.5, 4, .5),
60+
colors=('r', 'green', 'blue', (1,1,0), '#afeeee', '0.5')
61+
)
62+
plt.clabel(CS, fontsize=9, inline=1)
63+
plt.title('Crazy lines')
6064

6165

6266
# Or you can use a colormap to specify the colors; the default
6367
# colormap will be used for the contour lines
64-
figure()
65-
im = imshow(Z, interpolation='bilinear', origin='lower',
66-
cmap=cm.gray, extent=(-3,3,-2,2))
67-
levels = arange(-1.2, 1.6, 0.2)
68-
CS = contour(Z, levels,
69-
origin='lower',
70-
linewidths=2,
71-
extent=(-3,3,-2,2))
68+
plt.figure()
69+
im = plt.imshow(Z, interpolation='bilinear', origin='lower',
70+
cmap=cm.gray, extent=(-3,3,-2,2))
71+
levels = np.arange(-1.2, 1.6, 0.2)
72+
CS = plt.contour(Z, levels,
73+
origin='lower',
74+
linewidths=2,
75+
extent=(-3,3,-2,2))
7276

7377
#Thicken the zero contour.
7478
zc = CS.collections[6]
75-
setp(zc, linewidth=4)
79+
plt.setp(zc, linewidth=4)
7680

77-
clabel(CS, levels[1::2], # label every second level
78-
inline=1,
79-
fmt='%1.1f',
80-
fontsize=14)
81+
plt.clabel(CS, levels[1::2], # label every second level
82+
inline=1,
83+
fmt='%1.1f',
84+
fontsize=14)
8185

8286
# make a colorbar for the contour lines
83-
CB = colorbar(CS, shrink=0.8, extend='both')
87+
CB = plt.colorbar(CS, shrink=0.8, extend='both')
8488

85-
title('Lines with colorbar')
86-
hot() # Now change the colormap for the contour lines and colorbar
87-
flag()
89+
plt.title('Lines with colorbar')
90+
#plt.hot() # Now change the colormap for the contour lines and colorbar
91+
plt.flag()
8892

8993
# We can still add a colorbar for the image, too.
90-
CBI = colorbar(im, orientation='horizontal', shrink=0.8)
94+
CBI = plt.colorbar(im, orientation='horizontal', shrink=0.8)
9195

9296
# This makes the original colorbar look a bit out of place,
9397
# so let's improve its position.
9498

95-
l,b,w,h = gca().get_position().bounds
99+
l,b,w,h = plt.gca().get_position().bounds
96100
ll,bb,ww,hh = CB.ax.get_position().bounds
97101
CB.ax.set_position([ll, b+0.1*h, ww, h*0.8])
98102

99103

100104
#savefig('contour_demo')
101-
show()
105+
plt.show()
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
#!/usr/bin/env python
21
"""
32
See pcolor_demo2 for a much faster way of generating pcolor plots
43
"""
5-
from __future__ import division
6-
from pylab import *
7-
rc('axes', hold=True)
8-
rc('image', origin='upper')
9-
figure(1, frameon=False)
10-
Z = arange(10000.0); Z.shape = 100,100
11-
Z[:,50:] = 1
12-
jet() # sets the default
13-
im1 = figimage(Z, xo=50, yo=0)
14-
im2 = figimage(Z, xo=100, yo=100, alpha=.8)
15-
#gray() # overrides current and sets default
16-
#savefig('figimage_demo')
4+
import numpy as np
5+
import matplotlib
6+
import matplotlib.cm as cm
7+
import matplotlib.pyplot as plt
178

18-
show()
9+
10+
fig = plt.figure(frameon=False)
11+
Z = np.arange(10000.0)
12+
Z.shape = 100,100
13+
Z[:,50:] = 1.
14+
15+
im1 = plt.figimage(Z, xo=50, yo=0, cmap=cm.jet)
16+
im2 = plt.figimage(Z, xo=100, yo=100, alpha=.8, cmap=cm.jet)
17+
18+
plt.show()
1919

2020

2121

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
#!/usr/bin/env python
1+
import numpy as np
2+
import matplotlib.pyplot as plt
23

3-
from pylab import *
4-
ax1 = axes([0.1, 0.1, 0.4, 0.7])
5-
ax2 = axes([0.55, 0.1, 0.4, 0.7])
4+
fig = plt.figure()
5+
ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.7])
6+
ax2 = fig.add_axes([0.55, 0.1, 0.4, 0.7])
67

7-
x = arange(0.0, 2.0, 0.02)
8-
y1 = sin(2*pi*x)
9-
y2 = exp(-x)
8+
x = np.arange(0.0, 2.0, 0.02)
9+
y1 = np.sin(2*np.pi*x)
10+
y2 = np.exp(-x)
1011
l1, l2 = ax1.plot(x, y1, 'rs-', x, y2, 'go')
1112

12-
y3 = sin(4*pi*x)
13-
y4 = exp(-2*x)
13+
y3 = np.sin(4*np.pi*x)
14+
y4 = np.exp(-2*x)
1415
l3, l4 = ax2.plot(x, y3, 'yd-', x, y3, 'k^')
1516

16-
figlegend((l1, l2), ('Line 1', 'Line 2'), 'upper left')
17-
figlegend((l3, l4), ('Line 3', 'Line 4'), 'upper right')
18-
show()
17+
fig.legend((l1, l2), ('Line 1', 'Line 2'), 'upper left')
18+
fig.legend((l3, l4), ('Line 3', 'Line 4'), 'upper right')
19+
plt.show()
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
#!/usr/bin/env python
2-
from pylab import *
1+
import numpy as np
2+
import matplotlib.pyplot as plt
33

4-
dt = 0.01
5-
t = arange(dt, 20.0, dt)
4+
plt.subplots_adjust(hspace=0.4)
5+
t = np.arange(0.01, 20.0, 0.01)
66

7-
subplot(311)
8-
semilogy(t, exp(-t/5.0))
9-
ylabel('semilogy')
10-
grid(True)
7+
# log y axis
8+
plt.subplot(311)
9+
plt.semilogy(t, np.exp(-t/5.0))
10+
plt.ylabel('semilogy')
11+
plt.grid(True)
1112

12-
subplot(312)
13-
semilogx(t, sin(2*pi*t))
14-
ylabel('semilogx')
13+
# log x axis
14+
plt.subplot(312)
15+
plt.semilogx(t, np.sin(2*np.pi*t))
16+
plt.ylabel('semilogx')
17+
plt.grid(True)
1518

19+
# log x and y axis
20+
plt.subplot(313)
21+
plt.loglog(t, 20*np.exp(-t/10.0), basex=4)
22+
plt.grid(True)
23+
plt.ylabel('loglog base 4 on x')
1624

17-
18-
grid(True)
19-
gca().xaxis.grid(True, which='minor') # minor grid on too
20-
21-
subplot(313)
22-
loglog(t, 20*exp(-t/10.0), basex=4)
23-
grid(True)
24-
ylabel('loglog base 4 on x')
25-
savefig('log_demo')
26-
show()
25+
plt.show()

0 commit comments

Comments
 (0)