From fd83646e31abadc2809514ab9ece92b647725472 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 29 Nov 2021 10:13:03 +0100 Subject: [PATCH 1/3] DOC: some minor fixes to the usage rewrite --- tutorials/introductory/usage.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tutorials/introductory/usage.py b/tutorials/introductory/usage.py index cb2d1d5b9519..9395f256207f 100644 --- a/tutorials/introductory/usage.py +++ b/tutorials/introductory/usage.py @@ -130,7 +130,7 @@ # # Coding styles # ============= - +# # The object-oriented and the pyplot interfaces # --------------------------------------------- # @@ -172,8 +172,8 @@ ############################################################################### # (In addition, there is a third approach, for the case when embedding # Matplotlib in a GUI application, which completely drops pyplot, even for -# figure creation. See the corresponding section in the gallery for more info -# (:ref:`user_interfaces`).) +# figure creation. See the corresponding section in the gallery for more info: +# :ref:`user_interfaces`.) # # Matplotlib's documentation and examples use both the OO and the pyplot # styles. In general, we suggest using the OO style, particularly for @@ -211,8 +211,6 @@ def my_plotter(ax, data1, data2, param_dict): my_plotter(ax2, data3, data4, {'marker': 'o'}) ############################################################################### -# These examples provide convenience for more complex graphs. -# # Note that if you want to install these as a python package, or any other # customizations you could use use one of the many templates on the web; # Matplotlib has one at `mpl-cookiecutter @@ -260,8 +258,8 @@ def my_plotter(ax, data1, data2, param_dict): # Marker size depends on the method being used. `~.Axes.plot` specifies # markersize in points, and is generally the "diameter" or width of the # marker. `~.Axes.scatter` specifies markersize as approximately -# proportional to the visual area of the marker. There are also an array of -# markerstyles available as string codes (see :mod:`~.matplotlib.markers`) or +# proportional to the visual area of the marker. There is an array of +# markerstyles available as string codes (see :mod:`~.matplotlib.markers`), or # users can define their own `~.MarkerStyle` (see # :doc:`/gallery/lines_bars_and_markers/marker_reference`): @@ -280,7 +278,7 @@ def my_plotter(ax, data1, data2, param_dict): # Axes labels and text # -------------------- # -# `~.Axes.set_xlabel`, `~.Axes.set_ylabel` and `~.Axes.set_title` are used to +# `~.Axes.set_xlabel`, `~.Axes.set_ylabel`, and `~.Axes.set_title` are used to # add text in the indicated locations (see :doc:`/tutorials/text/text_intro` for # more discussion). Text can also be directly added to plots using # `~.Axes.text`: @@ -421,7 +419,7 @@ def my_plotter(ax, data1, data2, param_dict): # Plotting dates and strings # -------------------------- # -# Matplotlib can handle plotting arrays of dates and arrays of strings as +# Matplotlib can handle plotting arrays of dates and arrays of strings, as # well as floating point numbers. These get special locators and formatters # as appropriate. For dates: @@ -446,8 +444,8 @@ def my_plotter(ax, data1, data2, param_dict): ############################################################################## # One caveat about categorical plotting is that some methods of parsing # text files return a list of strings, even if the strings all represent -# numbers or dates. If you pass 1000 strings Matplotlib will think you -# meant 1000 categories and will add 1000 ticks to your plot. +# numbers or dates. If you pass 1000 strings, Matplotlib will think you +# meant 1000 categories and will add 1000 ticks to your plot! # # Color mapped data # ================= @@ -507,7 +505,9 @@ def my_plotter(ax, data1, data2, param_dict): # :doc:`/gallery/subplots_axes_and_figures/colorbar_placement` for # details. You can also change the appearance of colorbars with the # *extend* keyword to add arrows to the ends, and *shrink* and *aspect* to -# control the size. +# control the size. Finally, the colorbar will have default Locators +# and Formatters appropriate to the Norm. These can be changed as for +# other axis objects. # # # Working with multiple figures and axes From 94d2fd502d9b6d8ace7944286a0515bd7b3bc26b Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 29 Nov 2021 10:17:32 +0100 Subject: [PATCH 2/3] DOC: add show to each example --- tutorials/introductory/usage.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tutorials/introductory/usage.py b/tutorials/introductory/usage.py index 9395f256207f..05f9d79708e4 100644 --- a/tutorials/introductory/usage.py +++ b/tutorials/introductory/usage.py @@ -26,6 +26,7 @@ fig, ax = plt.subplots() # Create a figure containing a single axes. ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the axes. +plt.show() ############################################################################### # .. _figure_parts: @@ -124,6 +125,7 @@ ax.scatter('a', 'b', c='c', s='d', data=data) ax.set_xlabel('entry a') ax.set_ylabel('entry b') +plt.show() ############################################################################## # .. _coding_styles: @@ -154,6 +156,7 @@ ax.set_ylabel('y label') # Add a y-label to the axes. ax.set_title("Simple Plot") # Add a title to the axes. ax.legend() # Add a legend. +plt.show() ############################################################################### # or the pyplot-style: @@ -168,6 +171,7 @@ plt.ylabel('y label') plt.title("Simple Plot") plt.legend() +plt.show() ############################################################################### # (In addition, there is a third approach, for the case when embedding @@ -209,6 +213,7 @@ def my_plotter(ax, data1, data2, param_dict): fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5, 2.7)) my_plotter(ax1, data1, data2, {'marker': 'x'}) my_plotter(ax2, data3, data4, {'marker': 'o'}) +plt.show() ############################################################################### # Note that if you want to install these as a python package, or any other @@ -231,6 +236,7 @@ def my_plotter(ax, data1, data2, param_dict): ax.plot(x, np.cumsum(data1), color='blue', linewidth=3, linestyle='--') l, = ax.plot(x, np.cumsum(data2), color='orange', linewidth=2) l.set_linestyle(':') +plt.show() ############################################################################### # Colors @@ -245,6 +251,7 @@ def my_plotter(ax, data1, data2, param_dict): fig, ax = plt.subplots(figsize=(5, 2.7)) x = np.arange(len(data1)) ax.scatter(data1, data2, s=50, facecolor='C0', edgecolor='k') +plt.show() ############################################################################### # Linewidths, linestyles, and markersizes @@ -269,6 +276,7 @@ def my_plotter(ax, data1, data2, param_dict): ax.plot(data3, 'v', label='data3') ax.plot(data4, 's', label='data4') ax.legend() +plt.show() ################################################################################ # @@ -340,6 +348,7 @@ def my_plotter(ax, data1, data2, param_dict): arrowprops=dict(facecolor='black', shrink=0.05)) ax.set_ylim(-2, 2) +plt.show() ############################################################################### # In this basic example, both *xy* and *xytext* are in data coordinates. @@ -358,6 +367,7 @@ def my_plotter(ax, data1, data2, param_dict): ax.plot(np.arange(len(data2)), data2, label='data2') ax.plot(np.arange(len(data3)), data3, 'd', label='data3') ax.legend() +plt.show() ############################################################################## # Legends in Matplotlib are quite flexible in layout, placement, and what @@ -387,6 +397,7 @@ def my_plotter(ax, data1, data2, param_dict): axs[1].set_yscale('log') axs[1].plot(xdata, data) +plt.show() ############################################################################## # The scale sets the mapping from data values to spacing along the Axis. This @@ -408,6 +419,7 @@ def my_plotter(ax, data1, data2, param_dict): axs[1].set_xticks(np.arange(0, 100, 30), ['zero', '30', 'sixty', '90']) axs[1].set_yticks([-1.5, 0, 1.5]) # note that we don't need to specify labels axs[1].set_title('Manual ticks') +plt.show() ############################################################################## # Different scales can have different locators and formatters; for instance @@ -428,6 +440,7 @@ def my_plotter(ax, data1, data2, param_dict): np.timedelta64(1, 'h')) data = np.cumsum(np.random.randn(len(dates))) ax.plot(dates, data) +plt.show() ############################################################################## # For more information see the date examples @@ -440,6 +453,7 @@ def my_plotter(ax, data1, data2, param_dict): categories = ['turnips', 'rutabega', 'cucumber', 'pumpkins'] ax.bar(categories, np.random.rand(len(categories))) +plt.show() ############################################################################## # One caveat about categorical plotting is that some methods of parsing @@ -473,6 +487,7 @@ def my_plotter(ax, data1, data2, param_dict): pc = axs[1, 1].scatter(data1, data2, c=data3, cmap='RdBu_r') fig.colorbar(pc, ax=axs[1, 1], extend='both') axs[1, 1].set_title('scatter()') +plt.show() ############################################################################## # Colormaps From 26fc6a1bb9eb52bf112cc08e6d6fb8fe1fa49469 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 29 Nov 2021 15:08:30 +0100 Subject: [PATCH 3/3] DOC: suppress extra prints --- .flake8 | 2 +- tutorials/introductory/usage.py | 70 +++++++++++++-------------------- 2 files changed, 28 insertions(+), 44 deletions(-) diff --git a/.flake8 b/.flake8 index a33c5e93afac..ca1d8f2d4bdc 100644 --- a/.flake8 +++ b/.flake8 @@ -100,7 +100,7 @@ per-file-ignores = tutorials/introductory/images.py: E402, E501 tutorials/introductory/pyplot.py: E402, E501 tutorials/introductory/sample_plots.py: E501 - tutorials/introductory/usage.py: E501 + tutorials/introductory/usage.py: E703 tutorials/text/annotations.py: E402, E501 tutorials/text/text_intro.py: E402 tutorials/text/text_props.py: E501 diff --git a/tutorials/introductory/usage.py b/tutorials/introductory/usage.py index 05f9d79708e4..5f9f386a01ff 100644 --- a/tutorials/introductory/usage.py +++ b/tutorials/introductory/usage.py @@ -25,8 +25,7 @@ # `.Axes.plot` to draw some data on the axes: fig, ax = plt.subplots() # Create a figure containing a single axes. -ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the axes. -plt.show() +ax.plot([1, 2, 3, 4], [1, 4, 2, 3]); # Plot some data on the axes. ############################################################################### # .. _figure_parts: @@ -124,8 +123,7 @@ fig, ax = plt.subplots(figsize=(5, 2.7), constrained_layout=True) ax.scatter('a', 'b', c='c', s='d', data=data) ax.set_xlabel('entry a') -ax.set_ylabel('entry b') -plt.show() +ax.set_ylabel('entry b'); ############################################################################## # .. _coding_styles: @@ -155,8 +153,7 @@ ax.set_xlabel('x label') # Add an x-label to the axes. ax.set_ylabel('y label') # Add a y-label to the axes. ax.set_title("Simple Plot") # Add a title to the axes. -ax.legend() # Add a legend. -plt.show() +ax.legend(); # Add a legend. ############################################################################### # or the pyplot-style: @@ -170,8 +167,7 @@ plt.xlabel('x label') plt.ylabel('y label') plt.title("Simple Plot") -plt.legend() -plt.show() +plt.legend(); ############################################################################### # (In addition, there is a third approach, for the case when embedding @@ -182,8 +178,8 @@ # Matplotlib's documentation and examples use both the OO and the pyplot # styles. In general, we suggest using the OO style, particularly for # complicated plots, and functions and scripts that are intended to be reused -# as part of a larger project. However, the pyplot style can be very conveneient -# for quick interactive work. +# as part of a larger project. However, the pyplot style can be very +# conveneient for quick interactive work. # # .. note:: # @@ -193,9 +189,9 @@ # Making a helper functions # ------------------------- # -# If you need to make the same plots over and over again with different data sets, -# or want to easily wrap Matplotlib methods, use the recommended signature function -# below. +# If you need to make the same plots over and over again with different data +# sets, or want to easily wrap Matplotlib methods, use the recommended +# signature function below. def my_plotter(ax, data1, data2, param_dict): @@ -212,8 +208,7 @@ def my_plotter(ax, data1, data2, param_dict): xdata = np.arange(len(data1)) # make an ordinal for this fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5, 2.7)) my_plotter(ax1, data1, data2, {'marker': 'x'}) -my_plotter(ax2, data3, data4, {'marker': 'o'}) -plt.show() +my_plotter(ax2, data3, data4, {'marker': 'o'}); ############################################################################### # Note that if you want to install these as a python package, or any other @@ -235,8 +230,7 @@ def my_plotter(ax, data1, data2, param_dict): x = np.arange(len(data1)) ax.plot(x, np.cumsum(data1), color='blue', linewidth=3, linestyle='--') l, = ax.plot(x, np.cumsum(data2), color='orange', linewidth=2) -l.set_linestyle(':') -plt.show() +l.set_linestyle(':'); ############################################################################### # Colors @@ -250,8 +244,7 @@ def my_plotter(ax, data1, data2, param_dict): fig, ax = plt.subplots(figsize=(5, 2.7)) x = np.arange(len(data1)) -ax.scatter(data1, data2, s=50, facecolor='C0', edgecolor='k') -plt.show() +ax.scatter(data1, data2, s=50, facecolor='C0', edgecolor='k'); ############################################################################### # Linewidths, linestyles, and markersizes @@ -275,10 +268,9 @@ def my_plotter(ax, data1, data2, param_dict): ax.plot(data2, 'd', label='data2') ax.plot(data3, 'v', label='data3') ax.plot(data4, 's', label='data4') -ax.legend() -plt.show() +ax.legend(); -################################################################################ +############################################################################### # # Labelling plots # =============== @@ -287,8 +279,8 @@ def my_plotter(ax, data1, data2, param_dict): # -------------------- # # `~.Axes.set_xlabel`, `~.Axes.set_ylabel`, and `~.Axes.set_title` are used to -# add text in the indicated locations (see :doc:`/tutorials/text/text_intro` for -# more discussion). Text can also be directly added to plots using +# add text in the indicated locations (see :doc:`/tutorials/text/text_intro` +# for more discussion). Text can also be directly added to plots using # `~.Axes.text`: mu, sigma = 115, 15 @@ -302,8 +294,7 @@ def my_plotter(ax, data1, data2, param_dict): ax.set_title('Aardvark lengths\n (not really)') ax.text(75, .025, r'$\mu=115,\ \sigma=15$') ax.axis([55, 175, 0, 0.03]) -ax.grid(True) -plt.show() +ax.grid(True); ############################################################################### # All of the `~.Axes.text` functions return a `matplotlib.text.Text` @@ -347,8 +338,7 @@ def my_plotter(ax, data1, data2, param_dict): ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05)) -ax.set_ylim(-2, 2) -plt.show() +ax.set_ylim(-2, 2); ############################################################################### # In this basic example, both *xy* and *xytext* are in data coordinates. @@ -366,8 +356,7 @@ def my_plotter(ax, data1, data2, param_dict): ax.plot(np.arange(len(data1)), data1, label='data1') ax.plot(np.arange(len(data2)), data2, label='data2') ax.plot(np.arange(len(data3)), data3, 'd', label='data3') -ax.legend() -plt.show() +ax.legend(); ############################################################################## # Legends in Matplotlib are quite flexible in layout, placement, and what @@ -377,9 +366,9 @@ def my_plotter(ax, data1, data2, param_dict): # Axis scales and ticks # ===================== # -# Each Axes has two (or three) `~.axis.Axis` objects represnting the x- and y-axis. -# These control the *scale* of the axis, the tick *Locators* and the tick -# *Formatters*. +# Each Axes has two (or three) `~.axis.Axis` objects represnting the x- and +# y-axis. These control the *scale* of the axis, the tick *Locators* and the +# tick *Formatters*. # # Scales # ------ @@ -396,8 +385,7 @@ def my_plotter(ax, data1, data2, param_dict): axs[0].plot(xdata, data) axs[1].set_yscale('log') -axs[1].plot(xdata, data) -plt.show() +axs[1].plot(xdata, data); ############################################################################## # The scale sets the mapping from data values to spacing along the Axis. This @@ -418,8 +406,7 @@ def my_plotter(ax, data1, data2, param_dict): axs[1].plot(xdata, data1) axs[1].set_xticks(np.arange(0, 100, 30), ['zero', '30', 'sixty', '90']) axs[1].set_yticks([-1.5, 0, 1.5]) # note that we don't need to specify labels -axs[1].set_title('Manual ticks') -plt.show() +axs[1].set_title('Manual ticks'); ############################################################################## # Different scales can have different locators and formatters; for instance @@ -439,8 +426,7 @@ def my_plotter(ax, data1, data2, param_dict): dates = np.arange(np.datetime64('2021-11-15'), np.datetime64('2021-12-25'), np.timedelta64(1, 'h')) data = np.cumsum(np.random.randn(len(dates))) -ax.plot(dates, data) -plt.show() +ax.plot(dates, data); ############################################################################## # For more information see the date examples @@ -452,8 +438,7 @@ def my_plotter(ax, data1, data2, param_dict): fig, ax = plt.subplots(figsize=(5, 2.7), constrained_layout=True) categories = ['turnips', 'rutabega', 'cucumber', 'pumpkins'] -ax.bar(categories, np.random.rand(len(categories))) -plt.show() +ax.bar(categories, np.random.rand(len(categories))); ############################################################################## # One caveat about categorical plotting is that some methods of parsing @@ -486,8 +471,7 @@ def my_plotter(ax, data1, data2, param_dict): pc = axs[1, 1].scatter(data1, data2, c=data3, cmap='RdBu_r') fig.colorbar(pc, ax=axs[1, 1], extend='both') -axs[1, 1].set_title('scatter()') -plt.show() +axs[1, 1].set_title('scatter()'); ############################################################################## # Colormaps