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

Skip to content

Commit 747baec

Browse files
committed
docs: fix various example plots; add info to plot_directive warnings
1 parent 775b229 commit 747baec

6 files changed

Lines changed: 114 additions & 93 deletions

File tree

doc/pyplots/plotmap.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
14
try:
25
from mpl_toolkits.basemap import Basemap
3-
import matplotlib.pyplot as plt
4-
import numpy as np
6+
have_basemap = True
7+
except ImportError:
8+
have_basemap = False
9+
510

11+
def plotmap():
612
# create figure
713
fig = plt.figure(figsize=(8,8))
814
# set up orthographic map projection with
@@ -40,6 +46,16 @@
4046
# draw blue marble image in background.
4147
# (downsample the image by 50% for speed)
4248
map.bluemarble(scale=0.5)
43-
plt.show()
44-
except ImportError:
45-
pass
49+
50+
def plotempty():
51+
# create figure
52+
fig = plt.figure(figsize=(8,8))
53+
fig.text(0.5, 0.5, "Sorry, could not import Basemap",
54+
horizontalalignment='center')
55+
56+
if have_basemap:
57+
plotmap()
58+
else:
59+
plotempty()
60+
plt.show()
61+

doc/pyplots/tex_demo.png

-18.3 KB
Binary file not shown.

examples/pylab_examples/demo_tight_layout.py

Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,97 +10,96 @@ def example_plot(ax):
1010
ax.set_ylabel('y-label', fontsize=random.choice(fontsizes))
1111
ax.set_title('Title', fontsize=random.choice(fontsizes))
1212

13-
if 1:
14-
fig, ax = plt.subplots()
15-
example_plot(ax)
16-
plt.tight_layout()
13+
fig, ax = plt.subplots()
14+
example_plot(ax)
15+
plt.tight_layout()
1716

18-
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
19-
example_plot(ax1)
20-
example_plot(ax2)
21-
example_plot(ax3)
22-
example_plot(ax4)
23-
plt.tight_layout()
17+
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
18+
example_plot(ax1)
19+
example_plot(ax2)
20+
example_plot(ax3)
21+
example_plot(ax4)
22+
plt.tight_layout()
2423

25-
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
26-
example_plot(ax1)
27-
example_plot(ax2)
28-
plt.tight_layout()
24+
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
25+
example_plot(ax1)
26+
example_plot(ax2)
27+
plt.tight_layout()
2928

30-
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
31-
example_plot(ax1)
32-
example_plot(ax2)
33-
plt.tight_layout()
29+
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
30+
example_plot(ax1)
31+
example_plot(ax2)
32+
plt.tight_layout()
3433

35-
fig, axes = plt.subplots(nrows=3, ncols=3)
36-
for row in axes:
37-
for ax in row:
38-
example_plot(ax)
39-
plt.tight_layout()
34+
fig, axes = plt.subplots(nrows=3, ncols=3)
35+
for row in axes:
36+
for ax in row:
37+
example_plot(ax)
38+
plt.tight_layout()
4039

4140

42-
fig = plt.figure()
41+
fig = plt.figure()
4342

44-
ax1 = plt.subplot(221)
45-
ax2 = plt.subplot(223)
46-
ax3 = plt.subplot(122)
43+
ax1 = plt.subplot(221)
44+
ax2 = plt.subplot(223)
45+
ax3 = plt.subplot(122)
4746

48-
example_plot(ax1)
49-
example_plot(ax2)
50-
example_plot(ax3)
47+
example_plot(ax1)
48+
example_plot(ax2)
49+
example_plot(ax3)
5150

52-
plt.tight_layout()
51+
plt.tight_layout()
5352

5453

55-
fig = plt.figure()
54+
fig = plt.figure()
5655

57-
ax1 = plt.subplot2grid((3, 3), (0, 0))
58-
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
59-
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
60-
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
56+
ax1 = plt.subplot2grid((3, 3), (0, 0))
57+
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
58+
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
59+
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
6160

62-
example_plot(ax1)
63-
example_plot(ax2)
64-
example_plot(ax3)
65-
example_plot(ax4)
61+
example_plot(ax1)
62+
example_plot(ax2)
63+
example_plot(ax3)
64+
example_plot(ax4)
6665

67-
plt.tight_layout()
66+
plt.tight_layout()
6867

69-
plt.show()
68+
plt.show()
7069

7170

72-
fig = plt.figure()
71+
fig = plt.figure()
7372

74-
import matplotlib.gridspec as gridspec
73+
import matplotlib.gridspec as gridspec
7574

76-
gs1 = gridspec.GridSpec(3, 1)
77-
ax1 = fig.add_subplot(gs1[0])
78-
ax2 = fig.add_subplot(gs1[1])
79-
ax3 = fig.add_subplot(gs1[2])
75+
gs1 = gridspec.GridSpec(3, 1)
76+
ax1 = fig.add_subplot(gs1[0])
77+
ax2 = fig.add_subplot(gs1[1])
78+
ax3 = fig.add_subplot(gs1[2])
8079

81-
example_plot(ax1)
82-
example_plot(ax2)
83-
example_plot(ax3)
80+
example_plot(ax1)
81+
example_plot(ax2)
82+
example_plot(ax3)
8483

85-
gs1.tight_layout(fig, rect=[None, None, 0.45, None])
84+
gs1.tight_layout(fig, rect=[None, None, 0.45, None])
8685

87-
gs2 = gridspec.GridSpec(2, 1)
88-
ax4 = fig.add_subplot(gs2[0])
89-
ax5 = fig.add_subplot(gs2[1])
86+
gs2 = gridspec.GridSpec(2, 1)
87+
ax4 = fig.add_subplot(gs2[0])
88+
ax5 = fig.add_subplot(gs2[1])
9089

91-
#example_plot(ax4)
92-
#example_plot(ax5)
90+
#example_plot(ax4)
91+
#example_plot(ax5)
9392

94-
gs2.tight_layout(fig, rect=[0.45, None, None, None])
93+
gs2.tight_layout(fig, rect=[0.45, None, None, None])
9594

96-
# now match the top and bottom of two gridspecs.
97-
top = min(gs1.top, gs2.top)
98-
bottom = max(gs1.bottom, gs2.bottom)
95+
# now match the top and bottom of two gridspecs.
96+
top = min(gs1.top, gs2.top)
97+
bottom = max(gs1.bottom, gs2.bottom)
9998

100-
gs1.update(top=top, bottom=bottom)
101-
gs2.update(top=top, bottom=bottom)
102-
103-
plt.show()
99+
gs1.update(top=top, bottom=bottom)
100+
gs2.update(top=top, bottom=bottom)
101+
102+
plt.show()
104103

105104

106105

examples/units/artist_tests.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,33 @@
1515

1616
from basic_units import cm, inch
1717
import numpy as np
18-
from pylab import figure, show
18+
from matplotlib.pyplot import figure, show
1919

2020
fig = figure()
2121
ax = fig.add_subplot(111)
2222
ax.xaxis.set_units(cm)
2323
ax.yaxis.set_units(cm)
2424

25-
# test a line collection
26-
verts = []
27-
for i in range(10):
28-
# a random line segment in inches
29-
verts.append(zip(*inch*10*np.random.rand(2, random.randint(2,15))))
30-
lc = collections.LineCollection(verts, axes=ax)
31-
ax.add_collection(lc)
25+
if 0:
26+
# test a line collection
27+
# Not supported at present.
28+
verts = []
29+
for i in range(10):
30+
# a random line segment in inches
31+
verts.append(zip(*inch*10*np.random.rand(2, random.randint(2,15))))
32+
lc = collections.LineCollection(verts, axes=ax)
33+
ax.add_collection(lc)
3234

3335
# test a plain-ol-line
3436
line = lines.Line2D([0*cm, 1.5*cm], [0*cm, 2.5*cm], lw=2, color='black', axes=ax)
3537
ax.add_line(line)
3638

37-
# test a patch
38-
rect = patches.Rectangle( (1*cm, 1*cm), width=5*cm, height=2*cm, alpha=0.2, axes=ax)
39-
ax.add_patch(rect)
39+
if 0:
40+
# test a patch
41+
# Not supported at present.
42+
rect = patches.Rectangle( (1*cm, 1*cm), width=5*cm, height=2*cm, alpha=0.2, axes=ax)
43+
ax.add_patch(rect)
44+
4045

4146
t = text.Text(3*cm, 2.5*cm, 'text label', ha='left', va='bottom', axes=ax)
4247
ax.add_artist(t)
@@ -45,4 +50,6 @@
4550
ax.set_ylim(-1*cm, 10*cm)
4651
#ax.xaxis.set_units(inch)
4752
ax.grid(True)
53+
ax.set_title("Artists with units")
4854
show()
55+

examples/units/evans_test.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
import matplotlib
9-
matplotlib.rcParams['units'] = True
109

1110
from matplotlib.cbook import iterable
1211
import matplotlib.units as units
@@ -29,7 +28,7 @@ def axisinfo(unit, axis):
2928
'return the Foo AxisInfo'
3029
if unit==1.0 or unit==2.0:
3130
return units.AxisInfo(
32-
majloc = ticker.IndexLocator( 4, 0 ),
31+
majloc = ticker.IndexLocator( 8, 0 ),
3332
majfmt = ticker.FormatStrFormatter("VAL: %s"),
3433
label='foo',
3534
)
@@ -73,23 +72,22 @@ def default_units(x, axis):
7372

7473
# plot specifying units
7574
fig = figure()
75+
fig.suptitle("Custom units")
7676
fig.subplots_adjust(bottom=0.2)
77-
ax = fig.add_subplot(111)
77+
ax = fig.add_subplot(1,2,2)
7878
ax.plot( x, y, 'o', xunits=2.0 )
7979
for label in ax.get_xticklabels():
8080
label.set_rotation(30)
8181
label.set_ha('right')
82+
ax.set_title("xunits = 2.0")
8283

83-
#fig.savefig('plot1.png')
8484

8585
# plot without specifying units; will use the None branch for axisinfo
86-
fig2 = figure()
87-
ax = fig2.add_subplot(111)
86+
ax = fig.add_subplot(1,2,1)
8887
ax.plot( x, y ) # uses default units
89-
#p.savefig('plot2.png')
90-
91-
fig3 = figure()
92-
ax = fig3.add_subplot(111)
93-
ax.plot( x, y, xunits=0.5)
88+
ax.set_title('default units')
89+
for label in ax.get_xticklabels():
90+
label.set_rotation(30)
91+
label.set_ha('right')
9492

9593
show()

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,8 @@ def run(arguments, content, options, state_machine, state, lineno):
687687
except PlotError, err:
688688
reporter = state.memo.reporter
689689
sm = reporter.system_message(
690-
2, "Exception occurred in plotting %s: %s" % (output_base, err),
690+
2, "Exception occurred in plotting %s\n from %s:\n%s" % (output_base,
691+
source_file_name, err),
691692
line=lineno)
692693
results = [(code, [])]
693694
errors = [sm]

0 commit comments

Comments
 (0)