diff --git a/examples/event_handling/trifinder_event_demo.py b/examples/event_handling/trifinder_event_demo.py index 039365af8200..6678003ab189 100644 --- a/examples/event_handling/trifinder_event_demo.py +++ b/examples/event_handling/trifinder_event_demo.py @@ -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() @@ -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() diff --git a/examples/lines_bars_and_markers/psd_demo.py b/examples/lines_bars_and_markers/psd_demo.py index 9c3cb78c0c2d..43fcc9bd47b4 100644 --- a/examples/lines_bars_and_markers/psd_demo.py +++ b/examples/lines_bars_and_markers/psd_demo.py @@ -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() @@ -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. @@ -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) @@ -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() diff --git a/examples/misc/custom_projection.py b/examples/misc/custom_projection.py index bd22ea7cd65c..7e5a4b0d405c 100644 --- a/examples/misc/custom_projection.py +++ b/examples/misc/custom_projection.py @@ -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() diff --git a/examples/scales/symlog_demo.py b/examples/scales/symlog_demo.py index 69266be29065..e1c433b22b88 100644 --- a/examples/scales/symlog_demo.py +++ b/examples/scales/symlog_demo.py @@ -1,3 +1,4 @@ + """ =========== Symlog Demo @@ -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() diff --git a/examples/shapes_and_collections/ellipse_demo.py b/examples/shapes_and_collections/ellipse_demo.py index 03b6c9b0ad7d..8b03e845bdbb 100644 --- a/examples/shapes_and_collections/ellipse_demo.py +++ b/examples/shapes_and_collections/ellipse_demo.py @@ -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() diff --git a/examples/text_labels_and_annotations/multiline.py b/examples/text_labels_and_annotations/multiline.py index ce2cb158af85..d0ca38b95232 100644 --- a/examples/text_labels_and_annotations/multiline.py +++ b/examples/text_labels_and_annotations/multiline.py @@ -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() diff --git a/examples/userdemo/simple_legend01.py b/examples/userdemo/simple_legend01.py index 9e178af9be5b..c80488d1ad2d 100644 --- a/examples/userdemo/simple_legend01.py +++ b/examples/userdemo/simple_legend01.py @@ -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()