From bf2b43b5aad4d9a9486587834792bfcd6e4e509e Mon Sep 17 00:00:00 2001 From: patniharshit Date: Sun, 19 Mar 2017 16:13:30 +0530 Subject: [PATCH 1/4] Merge all figure in streamplot examples in one plot --- .../streamplot_demo.py | 79 +++++++++++++++++++ .../streamplot_features.py | 33 -------- .../streamplot_masking.py | 29 ------- .../streamplot_start_points.py | 28 ------- 4 files changed, 79 insertions(+), 90 deletions(-) create mode 100644 examples/images_contours_and_fields/streamplot_demo.py delete mode 100644 examples/images_contours_and_fields/streamplot_features.py delete mode 100644 examples/images_contours_and_fields/streamplot_masking.py delete mode 100644 examples/images_contours_and_fields/streamplot_start_points.py diff --git a/examples/images_contours_and_fields/streamplot_demo.py b/examples/images_contours_and_fields/streamplot_demo.py new file mode 100644 index 000000000000..48369376b22b --- /dev/null +++ b/examples/images_contours_and_fields/streamplot_demo.py @@ -0,0 +1,79 @@ +""" +========== +Streamplot +========== +A streamplot, or streamline plot, is used to display 2D vector fields. This +example shows a few features of the stream plot function: + + * Varying the color along a streamline. + * Varying the density of streamlines. + * Varying the line width along a stream line. + * Controlling the start points of streamlines. + * Streamlines skipping masked regions and NaN values. +""" +import numpy as np +import matplotlib.pyplot as plt + +w = 3 +Y, X = np.mgrid[-w:w:100j, -w:w:100j] +U = -1 - X**2 + Y +V = 1 + X - Y**2 +speed = np.sqrt(U*U + V*V) + +fig = plt.figure() + +# Varying density along a streamline +ax0 = fig.add_subplot(321) +ax0.streamplot(X, Y, U, V, density=[0.5, 1]) +ax0.set_title('Varying Density') + +# Varying color along a streamline +ax1 = fig.add_subplot(322) +strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn) +fig.colorbar(strm.lines) +ax1.set_title('Varying color') + +# Varying line width along a streamline +ax2 = fig.add_subplot(323) +lw = 5*speed / speed.max() +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 +X, Y = (np.linspace(-3, 3, 100), + np.linspace(-3, 3, 100)) +U, V = np.mgrid[-3:3:100j, 0:0:100j] +seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) + +ax3 = fig.add_subplot(324) +strm = ax3.streamplot(X, Y, U, V, color=U, linewidth=2, + cmap=plt.cm.autumn, start_points=seed_points.T) +fig.colorbar(strm.lines) +ax3.set_title('Controlling Starting Points') + +# Displaying the starting points with red symbols. +ax3.plot(seed_points[0], seed_points[1], 'bo') + +ax3.axis((-3, 3, -3, 3)) + +# Create a mask +w = 3 +Y, X = np.mgrid[-w:w:100j, -w:w:100j] +U = -1 - X**2 + Y +V = 1 + X - Y**2 +speed = np.sqrt(U*U + V*V) + +mask = np.zeros(U.shape, dtype=bool) +mask[40:60, 40:60] = True +U[:20, :20] = np.nan +U = np.ma.array(U, mask=mask) + +ax4 = fig.add_subplot(325) +ax4.streamplot(X, Y, U, V, color='r') +ax4.set_title('Streamline with Masking') + +ax4.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5, + interpolation='nearest', cmap=plt.cm.gray, aspect='auto') + +plt.tight_layout() +plt.show() diff --git a/examples/images_contours_and_fields/streamplot_features.py b/examples/images_contours_and_fields/streamplot_features.py deleted file mode 100644 index c9e520689fa1..000000000000 --- a/examples/images_contours_and_fields/streamplot_features.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -========== -Streamplot -========== - -Demo of the `streamplot` function. - -A streamplot, or streamline plot, is used to display 2D vector fields. This -example shows a few features of the stream plot function: - - * Varying the color along a streamline. - * Varying the density of streamlines. - * Varying the line width along a stream line. -""" -import numpy as np -import matplotlib.pyplot as plt - -Y, X = np.mgrid[-3:3:100j, -3:3:100j] -U = -1 - X**2 + Y -V = 1 + X - Y**2 -speed = np.sqrt(U*U + V*V) - -fig0, ax0 = plt.subplots() -strm = ax0.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn) -fig0.colorbar(strm.lines) - -fig1, (ax1, ax2) = plt.subplots(ncols=2) -ax1.streamplot(X, Y, U, V, density=[0.5, 1]) - -lw = 5*speed / speed.max() -ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw) - -plt.show() diff --git a/examples/images_contours_and_fields/streamplot_masking.py b/examples/images_contours_and_fields/streamplot_masking.py deleted file mode 100644 index d1354fe7c9c3..000000000000 --- a/examples/images_contours_and_fields/streamplot_masking.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -================================ -Streamplot function with masking -================================ - -This example shows how streamlines created by the streamplot function skips -masked regions and NaN values. -""" -import numpy as np -import matplotlib.pyplot as plt - -w = 3 -Y, X = np.mgrid[-w:w:100j, -w:w:100j] -U = -1 - X**2 + Y -V = 1 + X - Y**2 -speed = np.sqrt(U*U + V*V) - -mask = np.zeros(U.shape, dtype=bool) -mask[40:60, 40:60] = True -U[:20, :20] = np.nan -U = np.ma.array(U, mask=mask) - -fig, ax = plt.subplots() -ax.streamplot(X, Y, U, V, color='r') - -ax.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5, - interpolation='nearest', cmap=plt.cm.gray) - -plt.show() diff --git a/examples/images_contours_and_fields/streamplot_start_points.py b/examples/images_contours_and_fields/streamplot_start_points.py deleted file mode 100644 index 22f5bdba82db..000000000000 --- a/examples/images_contours_and_fields/streamplot_start_points.py +++ /dev/null @@ -1,28 +0,0 @@ -""" -======================================== -Streamplot function with starting points -======================================== - -This example shows how to fix the streamlines that are plotted, by passing -an array of seed points to the `start_points` keyword argument. -""" -import numpy as np -import matplotlib.pyplot as plt - -Y, X = np.mgrid[-3:3:100j, -3:3:100j] -U = -1 - X**2 + Y -V = 1 + X - Y**2 - -# 5 points along the first diagonal and a point in the left upper quadrant -seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) - -fig, ax = plt.subplots() -strm = ax.streamplot(X, Y, U, V, color=U, linewidth=2, - cmap=plt.cm.autumn, start_points=seed_points.T) -fig.colorbar(strm.lines) - -ax.plot(seed_points[0], seed_points[1], 'bo') - -ax.axis((-3, 3, -3, 3)) - -plt.show() From 92b11961dd979e811497bb5d69afad4d8beeb570 Mon Sep 17 00:00:00 2001 From: patniharshit Date: Thu, 23 Mar 2017 20:23:29 +0530 Subject: [PATCH 2/4] Add gridspec to have equal aspect ratio and remove references of deleted files --- doc/users/prev_whats_new/whats_new_1.2.rst | 2 +- doc/users/screenshots.rst | 2 +- .../streamplot_demo.py | 39 ++++++++++--------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/doc/users/prev_whats_new/whats_new_1.2.rst b/doc/users/prev_whats_new/whats_new_1.2.rst index f6dc7d2eef35..3dae7b43bb5c 100644 --- a/doc/users/prev_whats_new/whats_new_1.2.rst +++ b/doc/users/prev_whats_new/whats_new_1.2.rst @@ -153,7 +153,7 @@ In addition to simply plotting the streamlines of the vector field, line widths of the streamlines to a separate parameter, such as the speed or local intensity of the vector field. -.. plot:: gallery/images_contours_and_fields/streamplot_features.py +.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo.py New hist functionality diff --git a/doc/users/screenshots.rst b/doc/users/screenshots.rst index 884dc18b7992..7e1e9f3b8df9 100644 --- a/doc/users/screenshots.rst +++ b/doc/users/screenshots.rst @@ -71,7 +71,7 @@ a vector field. In addition to simply plotting the streamlines, it allows you to map the colors and/or line widths of streamlines to a separate parameter, such as the speed or local intensity of the vector field. -.. plot:: gallery/images_contours_and_fields/streamplot_features.py +.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo.py This feature complements the :meth:`~matplotlib.pyplot.quiver` function for plotting vector fields. Thanks to Tom Flannaghan and Tony Yu for adding the diff --git a/examples/images_contours_and_fields/streamplot_demo.py b/examples/images_contours_and_fields/streamplot_demo.py index 48369376b22b..50058ac65076 100644 --- a/examples/images_contours_and_fields/streamplot_demo.py +++ b/examples/images_contours_and_fields/streamplot_demo.py @@ -2,17 +2,19 @@ ========== Streamplot ========== -A streamplot, or streamline plot, is used to display 2D vector fields. This + +A stream plot, or stream line plot, is used to display 2D vector fields. This example shows a few features of the stream plot function: - * Varying the color along a streamline. - * Varying the density of streamlines. + * Varying the color along a stream line. + * Varying the density of stream lines. * Varying the line width along a stream line. - * Controlling the start points of streamlines. - * Streamlines skipping masked regions and NaN values. + * Controlling the starting points of stream lines. + * Stream lines skipping masked regions and NaN values. """ import numpy as np import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec w = 3 Y, X = np.mgrid[-w:w:100j, -w:w:100j] @@ -20,21 +22,22 @@ V = 1 + X - Y**2 speed = np.sqrt(U*U + V*V) -fig = plt.figure() +fig = plt.figure(figsize=(7, 9)) +gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) # Varying density along a streamline -ax0 = fig.add_subplot(321) +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(322) -strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn) +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(323) +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,16 +48,15 @@ U, V = np.mgrid[-3:3:100j, 0:0:100j] seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) -ax3 = fig.add_subplot(324) +ax3 = fig.add_subplot(gs[1, 1]) strm = ax3.streamplot(X, Y, U, V, color=U, linewidth=2, - cmap=plt.cm.autumn, start_points=seed_points.T) + cmap='autumn', start_points=seed_points.T) fig.colorbar(strm.lines) ax3.set_title('Controlling Starting Points') -# Displaying the starting points with red symbols. +# Displaying the starting points with blue symbols. ax3.plot(seed_points[0], seed_points[1], 'bo') - -ax3.axis((-3, 3, -3, 3)) +ax3.axis((-w, w, -w, w)) # Create a mask w = 3 @@ -68,12 +70,13 @@ U[:20, :20] = np.nan U = np.ma.array(U, mask=mask) -ax4 = fig.add_subplot(325) +ax4 = fig.add_subplot(gs[2:, :]) ax4.streamplot(X, Y, U, V, color='r') -ax4.set_title('Streamline with Masking') +ax4.set_title('Streamplot with Masking') ax4.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5, - interpolation='nearest', cmap=plt.cm.gray, aspect='auto') + interpolation='nearest', cmap='gray', aspect='auto') +ax4.set_aspect('equal') plt.tight_layout() plt.show() From 913fdb7331c0b5d6ddc0ea8f07e1a19ba7585b74 Mon Sep 17 00:00:00 2001 From: patniharshit Date: Tue, 28 Mar 2017 18:25:33 +0530 Subject: [PATCH 3/4] Remove data regeneration --- ...{streamplot_demo.py => plot_streamplot.py} | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) rename examples/images_contours_and_fields/{streamplot_demo.py => plot_streamplot.py} (73%) diff --git a/examples/images_contours_and_fields/streamplot_demo.py b/examples/images_contours_and_fields/plot_streamplot.py similarity index 73% rename from examples/images_contours_and_fields/streamplot_demo.py rename to examples/images_contours_and_fields/plot_streamplot.py index 50058ac65076..bf57db9a0e33 100644 --- a/examples/images_contours_and_fields/streamplot_demo.py +++ b/examples/images_contours_and_fields/plot_streamplot.py @@ -3,14 +3,14 @@ Streamplot ========== -A stream plot, or stream line plot, is used to display 2D vector fields. This -example shows a few features of the stream plot function: +A stream plot, or streamline plot, is used to display 2D vector fields. This +example shows a few features of the streamplot function: - * Varying the color along a stream line. - * Varying the density of stream lines. - * Varying the line width along a stream line. - * Controlling the starting points of stream lines. - * Stream lines skipping masked regions and NaN values. + * Varying the color along a streamline. + * Varying the density of streamlines. + * Varying the line width along a streamline. + * Controlling the starting points of streamlines. + * Streamlines skipping masked regions and NaN values. """ import numpy as np import matplotlib.pyplot as plt @@ -34,7 +34,7 @@ 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') +ax1.set_title('Varying Color') # Varying line width along a streamline ax2 = fig.add_subplot(gs[1, 0]) @@ -43,9 +43,6 @@ ax2.set_title('Varying Line Width') # Controlling the starting points of the streamlines -X, Y = (np.linspace(-3, 3, 100), - np.linspace(-3, 3, 100)) -U, V = np.mgrid[-3:3:100j, 0:0:100j] seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]]) ax3 = fig.add_subplot(gs[1, 1]) @@ -59,12 +56,6 @@ ax3.axis((-w, w, -w, w)) # Create a mask -w = 3 -Y, X = np.mgrid[-w:w:100j, -w:w:100j] -U = -1 - X**2 + Y -V = 1 + X - Y**2 -speed = np.sqrt(U*U + V*V) - mask = np.zeros(U.shape, dtype=bool) mask[40:60, 40:60] = True U[:20, :20] = np.nan From d56b5cee6050c8b42ba28ffd8024d04e48c42313 Mon Sep 17 00:00:00 2001 From: patniharshit Date: Mon, 10 Apr 2017 22:36:22 +0530 Subject: [PATCH 4/4] Update example name in docs --- doc/users/prev_whats_new/whats_new_1.2.rst | 2 +- doc/users/screenshots.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/users/prev_whats_new/whats_new_1.2.rst b/doc/users/prev_whats_new/whats_new_1.2.rst index 3dae7b43bb5c..ca46ae10f508 100644 --- a/doc/users/prev_whats_new/whats_new_1.2.rst +++ b/doc/users/prev_whats_new/whats_new_1.2.rst @@ -153,7 +153,7 @@ In addition to simply plotting the streamlines of the vector field, line widths of the streamlines to a separate parameter, such as the speed or local intensity of the vector field. -.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo.py +.. plot:: mpl_examples/images_contours_and_fields/plot_streamplot.py New hist functionality diff --git a/doc/users/screenshots.rst b/doc/users/screenshots.rst index 7e1e9f3b8df9..635d8cf66ae5 100644 --- a/doc/users/screenshots.rst +++ b/doc/users/screenshots.rst @@ -71,7 +71,7 @@ a vector field. In addition to simply plotting the streamlines, it allows you to map the colors and/or line widths of streamlines to a separate parameter, such as the speed or local intensity of the vector field. -.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo.py +.. plot:: mpl_examples/images_contours_and_fields/plot_streamplot.py This feature complements the :meth:`~matplotlib.pyplot.quiver` function for plotting vector fields. Thanks to Tom Flannaghan and Tony Yu for adding the