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

Skip to content

Commit dd4c147

Browse files
committed
Additional example cleanup
* For examples that are meant to be imported, move pyplot import to an if-main block * Remove some unused imports
1 parent d72e92f commit dd4c147

8 files changed

Lines changed: 70 additions & 62 deletions

File tree

examples/api/clippath_demo.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44
import numpy as np
55
import matplotlib.pyplot as plt
6-
import matplotlib.path as path
76
import matplotlib.patches as patches
87

98

examples/api/custom_projection_example.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import unicode_literals
22

3-
import matplotlib.pyplot as plt
43
from matplotlib.axes import Axes
54
from matplotlib.patches import Circle
65
from matplotlib.path import Path
6+
from matplotlib.ticker import NullLocator, Formatter, FixedLocator
77
from matplotlib.transforms import Affine2D, BboxTransformTo, Transform
88
from matplotlib.projections import register_projection
99
import matplotlib.spines as mspines
@@ -55,8 +55,8 @@ def cla(self):
5555
self.set_longitude_grid_ends(75)
5656

5757
# Turn off minor ticking altogether
58-
self.xaxis.set_minor_locator(plt.NullLocator())
59-
self.yaxis.set_minor_locator(plt.NullLocator())
58+
self.xaxis.set_minor_locator(NullLocator())
59+
self.yaxis.set_minor_locator(NullLocator())
6060

6161
# Do not display ticks -- we only want gridlines and text
6262
self.xaxis.set_ticks_position('none')
@@ -282,7 +282,7 @@ def format_coord(self, long, lat):
282282
# \u00b0 : degree symbol
283283
return '%f\u00b0%s, %f\u00b0%s' % (abs(lat), ns, abs(long), ew)
284284

285-
class DegreeFormatter(plt.Formatter):
285+
class DegreeFormatter(Formatter):
286286
"""
287287
This is a custom formatter that converts the native unit of
288288
radians into (truncated) degrees and adds a degree symbol.
@@ -326,7 +326,7 @@ class -- it provides a more convenient interface than
326326
# by degrees.
327327
number = (180.0 / degrees) + 1
328328
self.yaxis.set_major_locator(
329-
plt.FixedLocator(
329+
FixedLocator(
330330
np.linspace(-np.pi / 2.0, np.pi / 2.0, number, True)[1:-1]))
331331
# Set the formatter to display the tick labels in degrees,
332332
# rather than radians.
@@ -446,10 +446,12 @@ def inverted(self):
446446
# it.
447447
register_projection(HammerAxes)
448448

449-
# Now make a simple example using the custom projection.
450-
plt.subplot(111, projection="custom_hammer")
451-
p = plt.plot([-1, 1, 1], [-1, -1, 1], "o-")
452-
plt.grid(True)
449+
if __name__ == '__main__':
450+
import matplotlib.pyplot as plt
451+
# Now make a simple example using the custom projection.
452+
plt.subplot(111, projection="custom_hammer")
453+
p = plt.plot([-1, 1, 1], [-1, -1, 1], "o-")
454+
plt.grid(True)
453455

454-
plt.show()
456+
plt.show()
455457

examples/api/custom_scale_example.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import numpy as np
44
from numpy import ma
5-
import matplotlib.pyplot as plt
65
from matplotlib import scale as mscale
76
from matplotlib import transforms as mtransforms
7+
from matplotlib.ticker import Formatter, FixedLocator
88

99

1010
class MercatorLatitudeScale(mscale.ScaleBase):
@@ -70,13 +70,13 @@ def set_default_locators_and_formatters(self, axis):
7070
the radians to degrees and put a degree symbol after the
7171
value::
7272
"""
73-
class DegreeFormatter(plt.Formatter):
73+
class DegreeFormatter(Formatter):
7474
def __call__(self, x, pos=None):
7575
# \u00b0 : degree symbol
7676
return "%d\u00b0" % ((x / np.pi) * 180.0)
7777

7878
deg2rad = np.pi / 180.0
79-
axis.set_major_locator(plt.FixedLocator(
79+
axis.set_major_locator(FixedLocator(
8080
np.arange(-90, 90, 10) * deg2rad))
8181
axis.set_major_formatter(DegreeFormatter())
8282
axis.set_minor_formatter(DegreeFormatter())
@@ -154,16 +154,20 @@ def inverted(self):
154154
# that ``matplotlib`` can find it.
155155
mscale.register_scale(MercatorLatitudeScale)
156156

157-
t = np.arange(-180.0, 180.0, 0.1)
158-
s = t / 360.0 * np.pi
159157

160-
plt.plot(t, s, '-', lw=2)
161-
plt.gca().set_yscale('mercator')
158+
if __name__ == '__main__':
159+
import matplotlib.pyplot as plt
162160

163-
plt.xlabel('Longitude')
164-
plt.ylabel('Latitude')
165-
plt.title('Mercator: Projection of the Oppressor')
166-
plt.grid(True)
161+
t = np.arange(-180.0, 180.0, 0.1)
162+
s = t / 360.0 * np.pi
167163

168-
plt.show()
164+
plt.plot(t, s, '-', lw=2)
165+
plt.gca().set_yscale('mercator')
166+
167+
plt.xlabel('Longitude')
168+
plt.ylabel('Latitude')
169+
plt.title('Mercator: Projection of the Oppressor')
170+
plt.grid(True)
171+
172+
plt.show()
169173

examples/api/date_demo.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
"""
1414
import datetime
1515
import numpy as np
16-
import matplotlib
1716
import matplotlib.pyplot as plt
1817
import matplotlib.dates as mdates
19-
import matplotlib.mlab as mlab
2018
import matplotlib.cbook as cbook
2119

2220
years = mdates.YearLocator() # every year

examples/api/sankey_demo_old.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ def put_labels(labels,positions,output=True):
168168

169169
if __name__=='__main__':
170170

171-
import matplotlib.pyplot as P
171+
import matplotlib.pyplot as plt
172172

173173
outputs = [10.,-20.,5.,15.,-10.,40.]
174174
outlabels = ['First','Second','Third','Fourth','Fifth','Hurray!']
175175
outlabels = [ s+'\n%d%%' % abs(l) for l,s in zip(outputs,outlabels) ]
176176

177177
inputs = [60.,-25.,15.]
178178

179-
fig = P.figure()
179+
fig = plt.figure()
180180
ax = fig.add_subplot(1,1,1, xticks=[],yticks=[],
181181
title="Sankey diagram"
182182
)
@@ -187,5 +187,5 @@ def put_labels(labels,positions,output=True):
187187
outtexts[1].set_color('r')
188188
outtexts[-1].set_fontweight('bold')
189189

190-
P.show()
190+
plt.show()
191191

examples/event_handling/data_browser.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
import numpy as np
2-
import matplotlib.pyplot as plt
32

43

5-
X = np.random.rand(100, 200)
6-
xs = np.mean(X, axis=1)
7-
ys = np.std(X, axis=1)
8-
9-
fig = plt.figure()
10-
ax = fig.add_subplot(211)
11-
ax.set_title('click on point to plot time series')
12-
line, = ax.plot(xs, ys, 'o', picker=5) # 5 points tolerance
13-
ax2 = fig.add_subplot(212)
14-
154
class PointBrowser:
165
"""
176
Click on a point to select and highlight it -- the data that
@@ -74,9 +63,23 @@ def update(self):
7463
fig.canvas.draw()
7564

7665

77-
browser = PointBrowser()
66+
if __name__ == '__main__':
67+
import matplotlib.pyplot as plt
68+
69+
X = np.random.rand(100, 200)
70+
xs = np.mean(X, axis=1)
71+
ys = np.std(X, axis=1)
72+
73+
fig = plt.figure()
74+
ax = fig.add_subplot(211)
75+
ax.set_title('click on point to plot time series')
76+
line, = ax.plot(xs, ys, 'o', picker=5) # 5 points tolerance
77+
ax2 = fig.add_subplot(212)
78+
79+
browser = PointBrowser()
80+
81+
fig.canvas.mpl_connect('pick_event', browser.onpick)
82+
fig.canvas.mpl_connect('key_press_event', browser.onpress)
7883

79-
fig.canvas.mpl_connect('pick_event', browser.onpick)
80-
fig.canvas.mpl_connect('key_press_event', browser.onpress)
84+
plt.show()
8185

82-
plt.show()

examples/event_handling/pick_event_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def pick_handler(event):
6767
from __future__ import print_function
6868
from matplotlib.pyplot import figure, show
6969
from matplotlib.lines import Line2D
70-
from matplotlib.patches import Patch, Rectangle
70+
from matplotlib.patches import Rectangle
7171
from matplotlib.text import Text
7272
from matplotlib.image import AxesImage
7373
import numpy as np

examples/event_handling/poly_editor.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
55
"""
66
import numpy as np
7-
import matplotlib.pyplot as plt
7+
from matplotlib.lines import Line2D
88
from matplotlib.artist import Artist
9-
from matplotlib.patches import Polygon
109
from matplotlib.mlab import dist_point_to_segment
1110

1211

@@ -37,8 +36,7 @@ def __init__(self, ax, poly):
3736
self.poly = poly
3837

3938
x, y = zip(*self.poly.xy)
40-
self.line = plt.Line2D(x, y, marker='o', markerfacecolor='r',
41-
animated=True)
39+
self.line = Line2D(x, y, marker='o', markerfacecolor='r', animated=True)
4240
self.ax.add_line(self.line)
4341
#self._update_line(poly)
4442

@@ -143,22 +141,26 @@ def motion_notify_callback(self, event):
143141
self.canvas.blit(self.ax.bbox)
144142

145143

146-
fig = plt.figure()
147-
theta = np.arange(0, 2*np.pi, 0.1)
148-
r = 1.5
144+
if __name__ == '__main__':
145+
import matplotlib.pyplot as plt
146+
from matplotlib.patches import Polygon
149147

150-
xs = r*np.cos(theta)
151-
ys = r*np.sin(theta)
148+
fig = plt.figure()
149+
theta = np.arange(0, 2*np.pi, 0.1)
150+
r = 1.5
152151

153-
poly = Polygon(zip(xs, ys,), animated=True)
152+
xs = r*np.cos(theta)
153+
ys = r*np.sin(theta)
154154

155-
ax = plt.subplot(111)
156-
ax.add_patch(poly)
157-
p = PolygonInteractor(ax, poly)
155+
poly = Polygon(zip(xs, ys,), animated=True)
158156

159-
#ax.add_line(p.line)
160-
ax.set_title('Click and drag a point to move it')
161-
ax.set_xlim((-2,2))
162-
ax.set_ylim((-2,2))
163-
plt.show()
157+
ax = plt.subplot(111)
158+
ax.add_patch(poly)
159+
p = PolygonInteractor(ax, poly)
160+
161+
#ax.add_line(p.line)
162+
ax.set_title('Click and drag a point to move it')
163+
ax.set_xlim((-2,2))
164+
ax.set_ylim((-2,2))
165+
plt.show()
164166

0 commit comments

Comments
 (0)