Skip to content

Replace use of pyplot with OO api in some examples #19359

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 2 commits into from
Jan 26, 2021
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
10 changes: 5 additions & 5 deletions examples/event_handling/trifinder_event_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def on_mouse_move(event):
else:
tri = trifinder(event.xdata, event.ydata)
update_polygon(tri)
plt.title('In triangle %i' % tri)
ax.set_title(f'In triangle {tri}')
event.canvas.draw()


Expand All @@ -52,10 +52,10 @@ def on_mouse_move(event):
trifinder = triang.get_trifinder()

# Setup plot and callbacks.
plt.subplot(aspect='equal')
plt.triplot(triang, 'bo-')
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
ax.triplot(triang, 'bo-')
polygon = Polygon([[0, 0], [0, 0]], facecolor='y') # dummy data for (xs, ys)
update_polygon(-1)
plt.gca().add_patch(polygon)
plt.gcf().canvas.mpl_connect('motion_notify_event', on_mouse_move)
ax.add_patch(polygon)
fig.canvas.mpl_connect('motion_notify_event', on_mouse_move)
plt.show()
13 changes: 6 additions & 7 deletions examples/lines_bars_and_markers/psd_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
cnse = cnse[:len(t)]
s = 0.1 * np.sin(2 * np.pi * t) + cnse

plt.subplot(211)
plt.plot(t, s)
plt.subplot(212)
plt.psd(s, 512, 1 / dt)
fig, (ax0, ax1) = plt.subplots(2, 1)
ax0.plot(t, s)
ax1.psd(s, 512, 1 / dt)

plt.show()

Expand Down Expand Up @@ -72,7 +71,7 @@
ax2.psd(y, NFFT=len(t), pad_to=len(t), Fs=fs)
ax2.psd(y, NFFT=len(t), pad_to=len(t) * 2, Fs=fs)
ax2.psd(y, NFFT=len(t), pad_to=len(t) * 4, Fs=fs)
plt.title('zero padding')
ax2.set_title('zero padding')

# Plot the PSD with different block sizes, Zero pad to the length of the
# original data sequence.
Expand All @@ -81,7 +80,7 @@
ax3.psd(y, NFFT=len(t) // 2, pad_to=len(t), Fs=fs)
ax3.psd(y, NFFT=len(t) // 4, pad_to=len(t), Fs=fs)
ax3.set_ylabel('')
plt.title('block size')
ax3.set_title('block size')

# Plot the PSD with different amounts of overlap between blocks
ax4 = fig.add_subplot(gs[1, 2], sharex=ax2, sharey=ax2)
Expand All @@ -91,7 +90,7 @@
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t),
noverlap=int(0.2 * len(t) / 2.), Fs=fs)
ax4.set_ylabel('')
plt.title('overlap')
ax4.set_title('overlap')

plt.show()

Expand Down
6 changes: 3 additions & 3 deletions examples/misc/custom_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ def _get_core_transform(self, resolution):
if __name__ == '__main__':
import matplotlib.pyplot as plt
# Now make a simple example using the custom projection.
plt.subplot(projection="custom_hammer")
p = plt.plot([-1, 1, 1], [-1, -1, 1], "o-")
plt.grid(True)
fig, ax = plt.subplots(subplot_kw={'projection': 'custom_hammer'})
ax.plot([-1, 1, 1], [-1, -1, 1], "o-")
ax.grid()

plt.show()
40 changes: 20 additions & 20 deletions examples/scales/symlog_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

"""
===========
Symlog Demo
Expand All @@ -12,24 +13,23 @@
x = np.arange(-50.0, 50.0, dt)
y = np.arange(0, 100.0, dt)

plt.subplot(311)
plt.plot(x, y)
plt.xscale('symlog')
plt.ylabel('symlogx')
plt.grid(True)
plt.gca().xaxis.grid(True, which='minor') # minor grid on too

plt.subplot(312)
plt.plot(y, x)
plt.yscale('symlog')
plt.ylabel('symlogy')

plt.subplot(313)
plt.plot(x, np.sin(x / 3.0))
plt.xscale('symlog')
plt.yscale('symlog', linthresh=0.015)
plt.grid(True)
plt.ylabel('symlog both')

plt.tight_layout()
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3)

ax0.plot(x, y)
ax0.set_xscale('symlog')
ax0.set_ylabel('symlogx')
ax0.grid()
ax0.xaxis.grid(which='minor') # minor grid on too

ax1.plot(y, x)
ax1.set_yscale('symlog')
ax1.set_ylabel('symlogy')

ax2.plot(x, np.sin(x / 3.0))
ax2.set_xscale('symlog')
ax2.set_yscale('symlog', linthresh=0.015)
ax2.grid()
ax2.set_ylabel('symlog both')

fig.tight_layout()
plt.show()
6 changes: 3 additions & 3 deletions examples/shapes_and_collections/ellipse_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
angle_step = 45 # degrees
angles = np.arange(0, 180, angle_step)

ax = plt.subplot(aspect='equal')
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})

for angle in angles:
ellipse = Ellipse((0, 0), 4, 2, angle=angle, alpha=0.1)
ax.add_artist(ellipse)

plt.xlim(-2.2, 2.2)
plt.ylim(-2.2, 2.2)
ax.set_xlim(-2.2, 2.2)
ax.set_ylim(-2.2, 2.2)

plt.show()

Expand Down
34 changes: 17 additions & 17 deletions examples/text_labels_and_annotations/multiline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@
import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(7, 4))
ax = plt.subplot(121)
ax.set_aspect(1)
plt.plot(np.arange(10))
plt.xlabel('this is a xlabel\n(with newlines!)')
plt.ylabel('this is vertical\ntest', multialignment='center')
plt.text(2, 7, 'this is\nyet another test',
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(7, 4))

ax0.set_aspect(1)
ax0.plot(np.arange(10))
ax0.set_xlabel('this is a xlabel\n(with newlines!)')
ax0.set_ylabel('this is vertical\ntest', multialignment='center')
ax0.text(2, 7, 'this is\nyet another test',
rotation=45,
horizontalalignment='center',
verticalalignment='top',
multialignment='center')

plt.grid(True)
ax0.grid()

plt.subplot(122)

plt.text(0.29, 0.4, "Mat\nTTp\n123", size=18,
ax1.text(0.29, 0.4, "Mat\nTTp\n123", size=18,
va="baseline", ha="right", multialignment="left",
bbox=dict(fc="none"))

plt.text(0.34, 0.4, "Mag\nTTT\n123", size=18,
ax1.text(0.34, 0.4, "Mag\nTTT\n123", size=18,
va="baseline", ha="left", multialignment="left",
bbox=dict(fc="none"))

plt.text(0.95, 0.4, "Mag\nTTT$^{A^A}$\n123", size=18,
ax1.text(0.95, 0.4, "Mag\nTTT$^{A^A}$\n123", size=18,
va="baseline", ha="right", multialignment="left",
bbox=dict(fc="none"))

plt.xticks([0.2, 0.4, 0.6, 0.8, 1.],
["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009", "May\n2009"])
ax1.set_xticks([0.2, 0.4, 0.6, 0.8, 1.])
ax1.set_xticklabels(["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009",
"May\n2009"])

plt.axhline(0.4)
plt.title("test line spacing for multiline text")
ax1.axhline(0.4)
ax1.set_title("test line spacing for multiline text")

plt.subplots_adjust(bottom=0.25, top=0.75)
fig.subplots_adjust(bottom=0.25, top=0.75)
plt.show()
18 changes: 10 additions & 8 deletions examples/userdemo/simple_legend01.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
import matplotlib.pyplot as plt


plt.subplot(211)
plt.plot([1, 2, 3], label="test1")
plt.plot([3, 2, 1], label="test2")
fig = plt.figure()

ax = fig.add_subplot(211)
ax.plot([1, 2, 3], label="test1")
ax.plot([3, 2, 1], label="test2")
# Place a legend above this subplot, expanding itself to
# fully use the given bounding box.
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
ncol=2, mode="expand", borderaxespad=0.)

plt.subplot(223)
plt.plot([1, 2, 3], label="test1")
plt.plot([3, 2, 1], label="test2")
ax = fig.add_subplot(223)
ax.plot([1, 2, 3], label="test1")
ax.plot([3, 2, 1], label="test2")
# Place a legend to the right of this smaller subplot.
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)

plt.show()