From a2765d5610ad8751479cf20917c1f84dbcbdfb38 Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Wed, 6 May 2020 13:55:40 +0300 Subject: [PATCH 1/9] convert some sample plots to use plt.subplots() instead of other methods --- .../plot_streamplot.py | 15 +++++++++------ examples/pie_and_polar_charts/polar_demo.py | 2 +- examples/showcase/xkcd.py | 8 ++++---- examples/subplots_axes_and_figures/subplot.py | 18 +++++++++--------- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/examples/images_contours_and_fields/plot_streamplot.py b/examples/images_contours_and_fields/plot_streamplot.py index 2051764bb383..02e2cc591240 100644 --- a/examples/images_contours_and_fields/plot_streamplot.py +++ b/examples/images_contours_and_fields/plot_streamplot.py @@ -22,22 +22,22 @@ V = 1 + X - Y**2 speed = np.sqrt(U**2 + V**2) -fig = plt.figure(figsize=(7, 9)) -gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) + +gs = {'height_ratios': [1, 1, 2]} +fig, ((ax0, ax1), (ax2, ax3), (ax4, ax5)) = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9)) + # Varying density along a streamline -ax0 = fig.add_subplot(gs[0, 0]) ax0.streamplot(X, Y, U, V, density=[0.5, 1]) ax0.set_title('Varying Density') # Varying color along a streamline -ax1 = fig.add_subplot(gs[0, 1]) +plt.sca(ax1) strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn') fig.colorbar(strm.lines) ax1.set_title('Varying Color') # Varying line width along a streamline -ax2 = fig.add_subplot(gs[1, 0]) lw = 5*speed / speed.max() ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw) ax2.set_title('Varying Line Width') @@ -45,7 +45,7 @@ # Controlling the starting points of the streamlines seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) -ax3 = fig.add_subplot(gs[1, 1]) +plt.sca(ax3) strm = ax3.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn', start_points=seed_points.T) fig.colorbar(strm.lines) @@ -61,6 +61,9 @@ U[:20, :20] = np.nan U = np.ma.array(U, mask=mask) +gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) +ax4.remove() +ax5.remove() ax4 = fig.add_subplot(gs[2:, :]) ax4.streamplot(X, Y, U, V, color='r') ax4.set_title('Streamplot with Masking') diff --git a/examples/pie_and_polar_charts/polar_demo.py b/examples/pie_and_polar_charts/polar_demo.py index 1ba897a9fa48..83d12771f09e 100644 --- a/examples/pie_and_polar_charts/polar_demo.py +++ b/examples/pie_and_polar_charts/polar_demo.py @@ -12,7 +12,7 @@ r = np.arange(0, 2, 0.01) theta = 2 * np.pi * r -ax = plt.subplot(111, projection='polar') +fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r) ax.set_rmax(2) ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks diff --git a/examples/showcase/xkcd.py b/examples/showcase/xkcd.py index 22fec6cfdc87..9a6e60b8d565 100644 --- a/examples/showcase/xkcd.py +++ b/examples/showcase/xkcd.py @@ -14,8 +14,8 @@ # Based on "Stove Ownership" from XKCD by Randall Munroe # https://xkcd.com/418/ - fig = plt.figure() - ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) + fig, ax = plt.subplots() + ax.set_position((0.1, 0.2, 0.8, 0.7)) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.set_xticks([]) @@ -44,8 +44,8 @@ # Based on "The Data So Far" from XKCD by Randall Munroe # https://xkcd.com/373/ - fig = plt.figure() - ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) + fig, ax = plt.subplots() + ax.set_position((0.1, 0.2, 0.8, 0.7)) ax.bar([0, 1], [0, 100], 0.25) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') diff --git a/examples/subplots_axes_and_figures/subplot.py b/examples/subplots_axes_and_figures/subplot.py index 8457fba09926..9418aea8c506 100644 --- a/examples/subplots_axes_and_figures/subplot.py +++ b/examples/subplots_axes_and_figures/subplot.py @@ -15,14 +15,14 @@ y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) y2 = np.cos(2 * np.pi * x2) -plt.subplot(2, 1, 1) -plt.plot(x1, y1, 'o-') -plt.title('A tale of 2 subplots') -plt.ylabel('Damped oscillation') - -plt.subplot(2, 1, 2) -plt.plot(x2, y2, '.-') -plt.xlabel('time (s)') -plt.ylabel('Undamped') +fig, (ax1, ax2) = plt.subplots(2, 1) +fig.suptitle('A tale of 2 subplots') + +ax1.plot(x1, y1, 'o-') +ax1.set_ylabel('Damped oscillation') + +ax2.plot(x2, y2, '.-') +ax2.set_xlabel('time (s)') +ax2.set_ylabel('Undamped') plt.show() From 3c4faca82df7ad7cada6ccc4b29bcb5ffc579a0c Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Wed, 6 May 2020 14:16:27 +0300 Subject: [PATCH 2/9] fixing line length --- examples/images_contours_and_fields/plot_streamplot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/images_contours_and_fields/plot_streamplot.py b/examples/images_contours_and_fields/plot_streamplot.py index 02e2cc591240..2ce8b5f21cc4 100644 --- a/examples/images_contours_and_fields/plot_streamplot.py +++ b/examples/images_contours_and_fields/plot_streamplot.py @@ -24,8 +24,8 @@ gs = {'height_ratios': [1, 1, 2]} -fig, ((ax0, ax1), (ax2, ax3), (ax4, ax5)) = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9)) - +fig, axes = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9)) +((ax0, ax1), (ax2, ax3), (ax4, ax5)) = axes # Varying density along a streamline ax0.streamplot(X, Y, U, V, density=[0.5, 1]) From cd25cea15f557b9f0a892913a98a5cf6950b3ab2 Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Thu, 7 May 2020 09:19:43 +0300 Subject: [PATCH 3/9] changing the use of gridspec() to avoid repeatation, removing unpacking and changing varibale name --- .../plot_streamplot.py | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/examples/images_contours_and_fields/plot_streamplot.py b/examples/images_contours_and_fields/plot_streamplot.py index 2ce8b5f21cc4..d583967c0cb4 100644 --- a/examples/images_contours_and_fields/plot_streamplot.py +++ b/examples/images_contours_and_fields/plot_streamplot.py @@ -24,36 +24,32 @@ gs = {'height_ratios': [1, 1, 2]} -fig, axes = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9)) -((ax0, ax1), (ax2, ax3), (ax4, ax5)) = axes +fig, axs = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9)) # Varying density along a streamline -ax0.streamplot(X, Y, U, V, density=[0.5, 1]) -ax0.set_title('Varying Density') +axs[0, 0].streamplot(X, Y, U, V, density=[0.5, 1]) +axs[0, 0].set_title('Varying Density') # Varying color along a streamline -plt.sca(ax1) -strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn') -fig.colorbar(strm.lines) -ax1.set_title('Varying Color') +strm = axs[0, 1].streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn') +fig.colorbar(strm.lines, ax=axs[0, 1]) +axs[0, 1].set_title('Varying Color') # Varying line width along a streamline lw = 5*speed / speed.max() -ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw) -ax2.set_title('Varying Line Width') +axs[1, 0].streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw) +axs[1, 0].set_title('Varying Line Width') # Controlling the starting points of the streamlines seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) -plt.sca(ax3) -strm = ax3.streamplot(X, Y, U, V, color=U, linewidth=2, - cmap='autumn', start_points=seed_points.T) -fig.colorbar(strm.lines) -ax3.set_title('Controlling Starting Points') +strm = axs[1, 1].streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn', start_points=seed_points.T) +fig.colorbar(strm.lines, ax=axs[1, 1]) +axs[1, 1].set_title('Controlling Starting Points') # Displaying the starting points with blue symbols. -ax3.plot(seed_points[0], seed_points[1], 'bo') -ax3.set(xlim=(-w, w), ylim=(-w, w)) +axs[1, 1].plot(seed_points[0], seed_points[1], 'bo') +axs[1, 1].set(xlim=(-w, w), ylim=(-w, w)) # Create a mask mask = np.zeros(U.shape, dtype=bool) @@ -61,9 +57,9 @@ U[:20, :20] = np.nan U = np.ma.array(U, mask=mask) -gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) -ax4.remove() -ax5.remove() +gs = axs[0, 0].get_subplotspec().get_gridspec() +axs[2, 0].remove() +axs[2, 1].remove() ax4 = fig.add_subplot(gs[2:, :]) ax4.streamplot(X, Y, U, V, color='r') ax4.set_title('Streamplot with Masking') From 2ae3dd91651cee77cd89644a0f6806e3fdcc9750 Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Thu, 7 May 2020 09:28:52 +0300 Subject: [PATCH 4/9] fix exeeded line while linting test --- examples/images_contours_and_fields/plot_streamplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/images_contours_and_fields/plot_streamplot.py b/examples/images_contours_and_fields/plot_streamplot.py index d583967c0cb4..7ed854316ee3 100644 --- a/examples/images_contours_and_fields/plot_streamplot.py +++ b/examples/images_contours_and_fields/plot_streamplot.py @@ -43,7 +43,8 @@ # Controlling the starting points of the streamlines seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) -strm = axs[1, 1].streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn', start_points=seed_points.T) +strm = axs[1, 1].streamplot(X, Y, U, V, color=U, linewidth=2, + cmap='autumn', start_points=seed_points.T) fig.colorbar(strm.lines, ax=axs[1, 1]) axs[1, 1].set_title('Controlling Starting Points') From 1e58203324044d3f063ead53597af0d0305b47fb Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Mon, 11 May 2020 11:10:41 +0300 Subject: [PATCH 5/9] revert plot_streamplot.py to original version --- .../plot_streamplot.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/images_contours_and_fields/plot_streamplot.py b/examples/images_contours_and_fields/plot_streamplot.py index 7ed854316ee3..2051764bb383 100644 --- a/examples/images_contours_and_fields/plot_streamplot.py +++ b/examples/images_contours_and_fields/plot_streamplot.py @@ -22,35 +22,38 @@ V = 1 + X - Y**2 speed = np.sqrt(U**2 + V**2) - -gs = {'height_ratios': [1, 1, 2]} -fig, axs = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9)) +fig = plt.figure(figsize=(7, 9)) +gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) # Varying density along a streamline -axs[0, 0].streamplot(X, Y, U, V, density=[0.5, 1]) -axs[0, 0].set_title('Varying Density') +ax0 = fig.add_subplot(gs[0, 0]) +ax0.streamplot(X, Y, U, V, density=[0.5, 1]) +ax0.set_title('Varying Density') # Varying color along a streamline -strm = axs[0, 1].streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn') -fig.colorbar(strm.lines, ax=axs[0, 1]) -axs[0, 1].set_title('Varying Color') +ax1 = fig.add_subplot(gs[0, 1]) +strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn') +fig.colorbar(strm.lines) +ax1.set_title('Varying Color') # Varying line width along a streamline +ax2 = fig.add_subplot(gs[1, 0]) lw = 5*speed / speed.max() -axs[1, 0].streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw) -axs[1, 0].set_title('Varying Line Width') +ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw) +ax2.set_title('Varying Line Width') # Controlling the starting points of the streamlines seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) -strm = axs[1, 1].streamplot(X, Y, U, V, color=U, linewidth=2, - cmap='autumn', start_points=seed_points.T) -fig.colorbar(strm.lines, ax=axs[1, 1]) -axs[1, 1].set_title('Controlling Starting Points') +ax3 = fig.add_subplot(gs[1, 1]) +strm = ax3.streamplot(X, Y, U, V, color=U, linewidth=2, + cmap='autumn', start_points=seed_points.T) +fig.colorbar(strm.lines) +ax3.set_title('Controlling Starting Points') # Displaying the starting points with blue symbols. -axs[1, 1].plot(seed_points[0], seed_points[1], 'bo') -axs[1, 1].set(xlim=(-w, w), ylim=(-w, w)) +ax3.plot(seed_points[0], seed_points[1], 'bo') +ax3.set(xlim=(-w, w), ylim=(-w, w)) # Create a mask mask = np.zeros(U.shape, dtype=bool) @@ -58,9 +61,6 @@ U[:20, :20] = np.nan U = np.ma.array(U, mask=mask) -gs = axs[0, 0].get_subplotspec().get_gridspec() -axs[2, 0].remove() -axs[2, 1].remove() ax4 = fig.add_subplot(gs[2:, :]) ax4.streamplot(X, Y, U, V, color='r') ax4.set_title('Streamplot with Masking') From a664c8af6b148790a625dccbc2e8719efe30c534 Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Sun, 17 May 2020 09:46:59 +0300 Subject: [PATCH 6/9] adding both methods how to generate multiple plots --- examples/subplots_axes_and_figures/subplot.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/examples/subplots_axes_and_figures/subplot.py b/examples/subplots_axes_and_figures/subplot.py index 9418aea8c506..a5ca09f944d3 100644 --- a/examples/subplots_axes_and_figures/subplot.py +++ b/examples/subplots_axes_and_figures/subplot.py @@ -26,3 +26,34 @@ ax2.set_ylabel('Undamped') plt.show() + +############################################################################# +# +# +# Alternative Method For Creating Multiple Plots +# """""""""" +# +# Subplots can also be generated using `subplot()` as in the following example: +# + +import numpy as np +import matplotlib.pyplot as plt + + +x1 = np.linspace(0.0, 5.0) +x2 = np.linspace(0.0, 2.0) + +y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) +y2 = np.cos(2 * np.pi * x2) + +plt.subplot(2, 1, 1) +plt.plot(x1, y1, 'o-') +plt.title('A tale of 2 subplots') +plt.ylabel('Damped oscillation') + +plt.subplot(2, 1, 2) +plt.plot(x2, y2, '.-') +plt.xlabel('time (s)') +plt.ylabel('Undamped') + +plt.show() \ No newline at end of file From a9d580e414d5bf933fdfc062770f63a38de31273 Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Sun, 17 May 2020 10:08:41 +0300 Subject: [PATCH 7/9] fix linting --- examples/subplots_axes_and_figures/subplot.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/subplots_axes_and_figures/subplot.py b/examples/subplots_axes_and_figures/subplot.py index a5ca09f944d3..e5371c5e5d88 100644 --- a/examples/subplots_axes_and_figures/subplot.py +++ b/examples/subplots_axes_and_figures/subplot.py @@ -8,6 +8,7 @@ import numpy as np import matplotlib.pyplot as plt +############################################################################### x1 = np.linspace(0.0, 5.0) x2 = np.linspace(0.0, 2.0) @@ -36,9 +37,6 @@ # Subplots can also be generated using `subplot()` as in the following example: # -import numpy as np -import matplotlib.pyplot as plt - x1 = np.linspace(0.0, 5.0) x2 = np.linspace(0.0, 2.0) @@ -56,4 +54,4 @@ plt.xlabel('time (s)') plt.ylabel('Undamped') -plt.show() \ No newline at end of file +plt.show() From bc444fa3a5980c5cccf47bbb1897e248c71c9638 Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Tue, 19 May 2020 09:11:27 +0300 Subject: [PATCH 8/9] revert xkcd.py --- examples/showcase/xkcd.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/showcase/xkcd.py b/examples/showcase/xkcd.py index 9a6e60b8d565..22fec6cfdc87 100644 --- a/examples/showcase/xkcd.py +++ b/examples/showcase/xkcd.py @@ -14,8 +14,8 @@ # Based on "Stove Ownership" from XKCD by Randall Munroe # https://xkcd.com/418/ - fig, ax = plt.subplots() - ax.set_position((0.1, 0.2, 0.8, 0.7)) + fig = plt.figure() + ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.set_xticks([]) @@ -44,8 +44,8 @@ # Based on "The Data So Far" from XKCD by Randall Munroe # https://xkcd.com/373/ - fig, ax = plt.subplots() - ax.set_position((0.1, 0.2, 0.8, 0.7)) + fig = plt.figure() + ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) ax.bar([0, 1], [0, 100], 0.25) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') From 820bbdf4e7e9d197739b21808fc446f7e0cd19ca Mon Sep 17 00:00:00 2001 From: Asaf Maman Date: Mon, 25 May 2020 10:27:36 +0300 Subject: [PATCH 9/9] modify heading in subplot.py --- examples/subplots_axes_and_figures/subplot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/subplots_axes_and_figures/subplot.py b/examples/subplots_axes_and_figures/subplot.py index e5371c5e5d88..5502d7c23f2b 100644 --- a/examples/subplots_axes_and_figures/subplot.py +++ b/examples/subplots_axes_and_figures/subplot.py @@ -32,9 +32,10 @@ # # # Alternative Method For Creating Multiple Plots -# """""""""" +# """""""""""""""""""""""""""""""""""""""""""""" # -# Subplots can also be generated using `subplot()` as in the following example: +# Subplots can also be generated using `~.pyplot.subplot()` \ +# as in the following example: #