Skip to content

Cleanup of merged pylab examples #8677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/animation/image_slices_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

"""
from __future__ import print_function
import numpy

import numpy as np
import matplotlib.pyplot as plt


Expand All @@ -24,9 +25,9 @@ def __init__(self, ax, X):
def onscroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = numpy.clip(self.ind + 1, 0, self.slices - 1)
self.ind = (self.ind + 1) % self.slices
else:
self.ind = numpy.clip(self.ind - 1, 0, self.slices - 1)
self.ind = (self.ind - 1) % self.slices
self.update()

def update(self):
Expand All @@ -37,7 +38,7 @@ def update(self):

fig, ax = plt.subplots(1, 1)

X = numpy.random.rand(20, 20, 40)
X = np.random.rand(20, 20, 40)

tracker = IndexTracker(ax, X)

Expand Down
8 changes: 4 additions & 4 deletions examples/event_handling/pick_event_demo2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
mean vs stddev. When you click on one of the mu, sigma points, plot the raw
data from the dataset that generated the mean and stddev.
"""
import numpy
import numpy as np
import matplotlib.pyplot as plt


X = numpy.random.rand(100, 1000)
xs = numpy.mean(X, axis=1)
ys = numpy.std(X, axis=1)
X = np.random.rand(100, 1000)
xs = np.mean(X, axis=1)
ys = np.std(X, axis=1)

fig, ax = plt.subplots()
ax.set_title('click on point to plot time series')
Expand Down
4 changes: 2 additions & 2 deletions examples/event_handling/zoom_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
points**2, so their size is independent of the zoom
"""
from matplotlib.pyplot import figure, show
import numpy
import numpy as np
figsrc = figure()
figzoom = figure()

Expand All @@ -23,7 +23,7 @@
autoscale_on=False)
axsrc.set_title('Click to zoom')
axzoom.set_title('zoom window')
x, y, s, c = numpy.random.rand(4, 200)
x, y, s, c = np.random.rand(4, 200)
s *= 200


Expand Down
4 changes: 2 additions & 2 deletions examples/frontpage/membrane.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import numpy as np


datafile = cbook.get_sample_data('membrane.dat', asfileobj=False)
x = np.fromfile(datafile, np.float32)
with cbook.get_sample_data('membrane.dat') as datafile:
x = np.fromfile(datafile, np.float32)
# 0.0005 is the sample interval

fig, ax = plt.subplots()
Expand Down
4 changes: 2 additions & 2 deletions examples/images_contours_and_fields/image_clip_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import matplotlib.cbook as cbook


image_file = cbook.get_sample_data('grace_hopper.png')
image = plt.imread(image_file)
with cbook.get_sample_data('grace_hopper.png') as image_file:
image = plt.imread(image_file)

fig, ax = plt.subplots()
im = ax.imshow(image)
Expand Down
46 changes: 22 additions & 24 deletions examples/images_contours_and_fields/image_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
# It is also possible to show images of pictures.

# A sample image
image_file = cbook.get_sample_data('ada.png')
image = plt.imread(image_file)
with cbook.get_sample_data('ada.png') as image_file:
image = plt.imread(image_file)

fig, ax = plt.subplots()
ax.imshow(image)
Expand All @@ -53,19 +53,20 @@

w, h = 512, 512

datafile = cbook.get_sample_data('ct.raw.gz', asfileobj=True)
s = datafile.read()
with cbook.get_sample_data('ct.raw.gz', asfileobj=True) as datafile:
s = datafile.read()
A = np.fromstring(s, np.uint16).astype(float).reshape((w, h))
A /= A.max()

fig, ax = plt.subplots()
extent = (0, 25, 0, 25)
im = plt.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent)
im = ax.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent)

markers = [(15.9, 14.5), (16.8, 15)]
x, y = zip(*markers)
plt.plot(x, y, 'o')
ax.plot(x, y, 'o')

plt.title('CT density')
ax.set_title('CT density')

plt.show()

Expand Down Expand Up @@ -121,26 +122,21 @@
# suggested.

A = np.random.rand(5, 5)
plt.figure(1)
plt.imshow(A, interpolation='nearest')
plt.grid(True)

plt.figure(2)
plt.imshow(A, interpolation='bilinear')
plt.grid(True)

plt.figure(3)
plt.imshow(A, interpolation='bicubic')
plt.grid(True)
fig, axs = plt.subplots(1, 3, figsize=(10, 3))
for ax, interp in zip(axs, ['nearest', 'bilinear', 'bicubic']):
ax.imshow(A, interpolation=interp)
ax.set_title(interp.capitalize())
ax.grid(True)

plt.show()


###############################################################################
# You can specify whether images should be plotted with the array origin
# x[0,0] in the upper left or upper right by using the origin parameter.
# You can also control the default be setting image.origin in your
# matplotlibrc file; see http://matplotlib.org/matplotlibrc
# x[0,0] in the upper left or lower right by using the origin parameter.
# You can also control the default setting image.origin in your
# :ref:`matplotlibrc file <customizing-with-matplotlibrc-files>`

x = np.arange(120).reshape((10, 12))

Expand All @@ -166,11 +162,13 @@

path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]])
patch = PathPatch(path, facecolor='none')
plt.gca().add_patch(patch)

im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
origin='lower', extent=[-3, 3, -3, 3],
clip_path=patch, clip_on=True)
fig, ax = plt.subplots()
ax.add_patch(patch)

im = ax.imshow(Z, interpolation='bilinear', cmap=cm.gray,
origin='lower', extent=[-3, 3, -3, 3],
clip_path=patch, clip_on=True)
im.set_clip_path(patch)

plt.show()
10 changes: 6 additions & 4 deletions examples/pylab_examples/ellipse_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

"""
import matplotlib.pyplot as plt
import numpy.random as rnd
import numpy as np
from matplotlib.patches import Ellipse

NUM = 250

ells = [Ellipse(xy=rnd.rand(2)*10, width=rnd.rand(), height=rnd.rand(), angle=rnd.rand()*360)
ells = [Ellipse(xy=np.random.rand(2)*10,
width=np.random.rand(), height=np.random.rand(),
angle=np.random.rand()*360)
for i in range(NUM)]

fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
for e in ells:
ax.add_artist(e)
e.set_clip_box(ax.bbox)
e.set_alpha(rnd.rand())
e.set_facecolor(rnd.rand(3))
e.set_alpha(np.random.rand())
e.set_facecolor(np.random.rand(3))

ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
Expand Down
6 changes: 3 additions & 3 deletions examples/pylab_examples/fancybox_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test2(ax):
#p_fancy.set_boxstyle("round", pad=0.1, rounding_size=0.2)

ax.text(0.1, 0.8,
' boxstyle="round,pad=0.1\n rounding\\_size=0.2"',
' boxstyle="round,pad=0.1\n rounding_size=0.2"',
size=10, transform=ax.transAxes)

# draws control points for the fancy box.
Expand All @@ -118,7 +118,7 @@ def test3(ax):
ax.add_patch(p_fancy)

ax.text(0.1, 0.8,
' boxstyle="round,pad=0.1"\n mutation\\_scale=2',
' boxstyle="round,pad=0.1"\n mutation_scale=2',
size=10, transform=ax.transAxes)

# draws control points for the fancy box.
Expand Down Expand Up @@ -152,7 +152,7 @@ def test4(ax):
ax.add_patch(p_fancy)

ax.text(0.1, 0.8,
' boxstyle="round,pad=0.3"\n mutation\\_aspect=.5',
' boxstyle="round,pad=0.3"\n mutation_aspect=.5',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we don't have to worry about these getting rendered as math now? Or is that intended behavior now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if wrapped in $, or maybe a usetex=True got removed at some point? They're definitely not needed right now though.

size=10, transform=ax.transAxes)

draw_bbox(ax, bb)
Expand Down
5 changes: 2 additions & 3 deletions examples/pylab_examples/line_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
segs = np.ma.masked_where((segs > 50) & (segs < 60), segs)

# We need to set the plot limits.
ax = plt.axes()
fig, ax = plt.subplots()
ax.set_xlim(x.min(), x.max())
ax.set_ylim(ys.min(), ys.max())

Expand Down Expand Up @@ -60,7 +60,7 @@
ys = [x + i for i in x]

# We need to set the plot limits, they will not autoscale
ax = plt.axes()
fig, ax = plt.subplots()
ax.set_xlim(np.min(x), np.max(x))
ax.set_ylim(np.min(ys), np.max(ys))

Expand All @@ -77,7 +77,6 @@
linestyles='solid')
line_segments.set_array(x)
ax.add_collection(line_segments)
fig = plt.gcf()
axcb = fig.colorbar(line_segments)
axcb.set_label('Line Number')
ax.set_title('Line Collection with mapped colors')
Expand Down
10 changes: 5 additions & 5 deletions examples/pylab_examples/major_minor_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
s = np.sin(0.1*np.pi*t)*np.exp(-t*0.01)

fig, ax = plt.subplots()
plt.plot(t, s)
ax.plot(t, s)

ax.xaxis.set_major_locator(majorLocator)
ax.xaxis.set_major_formatter(majorFormatter)
Expand Down Expand Up @@ -76,12 +76,12 @@
s = np.sin(2*np.pi*t)*np.exp(-t*0.01)

fig, ax = plt.subplots()
plt.plot(t, s)
ax.plot(t, s)

ax.xaxis.set_minor_locator(minorLocator)

plt.tick_params(which='both', width=2)
plt.tick_params(which='major', length=7)
plt.tick_params(which='minor', length=4, color='r')
ax.tick_params(which='both', width=2)
ax.tick_params(which='major', length=7)
ax.tick_params(which='minor', length=4, color='r')

plt.show()
7 changes: 3 additions & 4 deletions examples/pylab_examples/mri_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import matplotlib.cm as cm
import numpy as np

fig, ax = plt.subplots(num="MRI_demo")

# Data are 256x256 16 bit integers
dfile = cbook.get_sample_data('s1045.ima.gz')
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
dfile.close()
with cbook.get_sample_data('s1045.ima.gz') as dfile:
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))

fig, ax = plt.subplots(num="MRI_demo")
ax.imshow(im, cmap=cm.gray)
ax.axis('off')

Expand Down
10 changes: 4 additions & 6 deletions examples/pylab_examples/mri_with_eeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
fig = plt.figure("MRI_with_EEG")

# Load the MRI data (256x256 16 bit integers)
dfile = cbook.get_sample_data('s1045.ima.gz')
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
dfile.close()
with cbook.get_sample_data('s1045.ima.gz') as dfile:
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))

# Plot the MRI image
ax0 = fig.add_subplot(2, 2, 1)
Expand All @@ -43,9 +42,8 @@

# Load the EEG data
numSamples, numRows = 800, 4
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False)
print('Loading EEG %s' % eegfile)
data = np.fromfile(eegfile, dtype=float)
with cbook.get_sample_data('eeg.dat') as eegfile:
data = np.fromfile(eegfile, dtype=float)
data.shape = (numSamples, numRows)
t = 10.0 * np.arange(numSamples) / numSamples

Expand Down
Loading