From f7972682baa9afdbdba247da5a31852beac34726 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Thu, 29 Aug 2024 10:36:15 +0200 Subject: [PATCH 01/50] Change first argument to add_axes from list to tuple --- doc/project/history.rst | 4 ++-- doc/users/prev_whats_new/whats_new_3.5.0.rst | 6 +++--- galleries/examples/animation/rain.py | 2 +- .../make_room_for_ylabel_using_axesgrid.py | 8 ++++---- .../examples/axisartist/demo_parasite_axes.py | 2 +- .../images_contours_and_fields/barcode_demo.py | 2 +- .../image_antialiasing.py | 2 +- .../image_exact_placement.py | 8 ++++---- galleries/examples/misc/svg_filter_line.py | 2 +- galleries/examples/misc/svg_filter_pie.py | 2 +- galleries/examples/showcase/anatomy.py | 2 +- galleries/examples/showcase/firefox.py | 2 +- galleries/examples/showcase/mandelbrot.py | 2 +- .../subplots_axes_and_figures/axes_demo.py | 4 ++-- .../mathtext_examples.py | 2 +- .../ticks/fig_axes_customize_simple.py | 2 +- galleries/examples/widgets/buttons.py | 4 ++-- galleries/examples/widgets/range_slider.py | 2 +- galleries/examples/widgets/slider_demo.py | 6 +++--- galleries/examples/widgets/slider_snap_demo.py | 6 +++--- galleries/examples/widgets/textbox.py | 2 +- galleries/tutorials/artists.py | 8 ++++---- galleries/users_explain/axes/arranging_axes.py | 4 ++-- galleries/users_explain/axes/axes_intro.rst | 4 ++-- galleries/users_explain/colors/colors.py | 2 +- galleries/users_explain/text/text_props.py | 2 +- .../users_explain/toolkits/axisartist.rst | 2 +- lib/matplotlib/_enums.py | 2 +- lib/matplotlib/figure.py | 4 ++-- lib/matplotlib/image.py | 2 +- lib/matplotlib/tests/test_artist.py | 4 ++-- lib/matplotlib/tests/test_axes.py | 10 +++++----- lib/matplotlib/tests/test_colorbar.py | 12 ++++++------ lib/matplotlib/tests/test_colors.py | 6 +++--- lib/matplotlib/tests/test_constrainedlayout.py | 4 ++-- lib/matplotlib/tests/test_figure.py | 18 +++++++++--------- lib/matplotlib/tests/test_image.py | 4 ++-- lib/matplotlib/tests/test_marker.py | 4 ++-- lib/matplotlib/tests/test_offsetbox.py | 2 +- lib/matplotlib/tests/test_polar.py | 12 ++++++------ lib/matplotlib/tests/test_usetex.py | 2 +- lib/matplotlib/widgets.py | 2 +- .../axes_grid1/tests/test_axes_grid1.py | 2 +- tools/make_icons.py | 2 +- 44 files changed, 93 insertions(+), 93 deletions(-) diff --git a/doc/project/history.rst b/doc/project/history.rst index 966b7a3caa38..7f148902898b 100644 --- a/doc/project/history.rst +++ b/doc/project/history.rst @@ -157,7 +157,7 @@ Matplotlib logo (2008 - 2015). def add_math_background(): - ax = fig.add_axes([0., 0., 1., 1.]) + ax = fig.add_axes((0., 0., 1., 1.)) text = [] text.append( @@ -187,7 +187,7 @@ Matplotlib logo (2008 - 2015). def add_polar_bar(): - ax = fig.add_axes([0.025, 0.075, 0.2, 0.85], projection='polar') + ax = fig.add_axes((0.025, 0.075, 0.2, 0.85), projection='polar') ax.patch.set_alpha(axalpha) ax.set_axisbelow(True) diff --git a/doc/users/prev_whats_new/whats_new_3.5.0.rst b/doc/users/prev_whats_new/whats_new_3.5.0.rst index e67573702218..54687e9a487e 100644 --- a/doc/users/prev_whats_new/whats_new_3.5.0.rst +++ b/doc/users/prev_whats_new/whats_new_3.5.0.rst @@ -274,9 +274,9 @@ of the text inside the Axes of the `.TextBox` widget. fig = plt.figure(figsize=(4, 3)) for i, alignment in enumerate(['left', 'center', 'right']): - box_input = fig.add_axes([0.1, 0.7 - i*0.3, 0.8, 0.2]) - text_box = TextBox(ax=box_input, initial=f'{alignment} alignment', - label='', textalignment=alignment) + box_input = fig.add_axes((0.1, 0.7 - i*0.3, 0.8, 0.2)) + text_box = TextBox(ax=box_input, initial=f'{alignment} alignment', + label='', textalignment=alignment) Simplifying the font setting for usetex mode -------------------------------------------- diff --git a/galleries/examples/animation/rain.py b/galleries/examples/animation/rain.py index 4303e567d074..2a354aaa7c94 100644 --- a/galleries/examples/animation/rain.py +++ b/galleries/examples/animation/rain.py @@ -22,7 +22,7 @@ # Create new Figure and an Axes which fills it. fig = plt.figure(figsize=(7, 7)) -ax = fig.add_axes([0, 0, 1, 1], frameon=False) +ax = fig.add_axes((0, 0, 1, 1), frameon=False) ax.set_xlim(0, 1), ax.set_xticks([]) ax.set_ylim(0, 1), ax.set_yticks([]) diff --git a/galleries/examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py b/galleries/examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py index f130ef4a6de2..7e914ff76b6b 100644 --- a/galleries/examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py +++ b/galleries/examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py @@ -10,7 +10,7 @@ from mpl_toolkits.axes_grid1.axes_divider import make_axes_area_auto_adjustable fig = plt.figure() -ax = fig.add_axes([0, 0, 1, 1]) +ax = fig.add_axes((0, 0, 1, 1)) ax.set_yticks([0.5], labels=["very long label"]) @@ -19,8 +19,8 @@ # %% fig = plt.figure() -ax1 = fig.add_axes([0, 0, 1, 0.5]) -ax2 = fig.add_axes([0, 0.5, 1, 0.5]) +ax1 = fig.add_axes((0, 0, 1, 0.5)) +ax2 = fig.add_axes((0, 0.5, 1, 0.5)) ax1.set_yticks([0.5], labels=["very long label"]) ax1.set_ylabel("Y label") @@ -33,7 +33,7 @@ # %% fig = plt.figure() -ax1 = fig.add_axes([0, 0, 1, 1]) +ax1 = fig.add_axes((0, 0, 1, 1)) divider = make_axes_locatable(ax1) ax2 = divider.append_axes("right", "100%", pad=0.3, sharey=ax1) diff --git a/galleries/examples/axisartist/demo_parasite_axes.py b/galleries/examples/axisartist/demo_parasite_axes.py index 8565ef455c7e..800b9be32ac8 100644 --- a/galleries/examples/axisartist/demo_parasite_axes.py +++ b/galleries/examples/axisartist/demo_parasite_axes.py @@ -24,7 +24,7 @@ fig = plt.figure() -host = fig.add_axes([0.15, 0.1, 0.65, 0.8], axes_class=HostAxes) +host = fig.add_axes((0.15, 0.1, 0.65, 0.8), axes_class=HostAxes) par1 = host.get_aux_axes(viewlim_mode=None, sharex=host) par2 = host.get_aux_axes(viewlim_mode=None, sharex=host) diff --git a/galleries/examples/images_contours_and_fields/barcode_demo.py b/galleries/examples/images_contours_and_fields/barcode_demo.py index bdf48ca22531..5df58535650d 100644 --- a/galleries/examples/images_contours_and_fields/barcode_demo.py +++ b/galleries/examples/images_contours_and_fields/barcode_demo.py @@ -30,7 +30,7 @@ dpi = 100 fig = plt.figure(figsize=(len(code) * pixel_per_bar / dpi, 2), dpi=dpi) -ax = fig.add_axes([0, 0, 1, 1]) # span the whole figure +ax = fig.add_axes((0, 0, 1, 1)) # span the whole figure ax.set_axis_off() ax.imshow(code.reshape(1, -1), cmap='binary', aspect='auto', interpolation='nearest') diff --git a/galleries/examples/images_contours_and_fields/image_antialiasing.py b/galleries/examples/images_contours_and_fields/image_antialiasing.py index 7f223f6998f2..10f563875767 100644 --- a/galleries/examples/images_contours_and_fields/image_antialiasing.py +++ b/galleries/examples/images_contours_and_fields/image_antialiasing.py @@ -245,7 +245,7 @@ # may serve a 100x100 version of the image, which will be downsampled.) fig = plt.figure(figsize=(2, 2)) -ax = fig.add_axes([0, 0, 1, 1]) +ax = fig.add_axes((0, 0, 1, 1)) ax.imshow(aa[:400, :400], cmap='RdBu_r', interpolation='nearest') plt.show() # %% diff --git a/galleries/examples/images_contours_and_fields/image_exact_placement.py b/galleries/examples/images_contours_and_fields/image_exact_placement.py index a3b314a7c7c3..7c667dfed1af 100644 --- a/galleries/examples/images_contours_and_fields/image_exact_placement.py +++ b/galleries/examples/images_contours_and_fields/image_exact_placement.py @@ -134,13 +134,13 @@ def annotate_rect(ax): fig = plt.figure(figsize=(fig_width / dpi, fig_height / dpi), facecolor='aliceblue') # the position posA must be normalized by the figure width and height: -ax = fig.add_axes([posA[0] / fig_width, posA[1] / fig_height, - posA[2] / fig_width, posA[3] / fig_height]) +ax = fig.add_axes((posA[0] / fig_width, posA[1] / fig_height, + posA[2] / fig_width, posA[3] / fig_height)) ax.imshow(A, vmin=-1, vmax=1) annotate_rect(ax) -ax = fig.add_axes([posB[0] / fig_width, posB[1] / fig_height, - posB[2] / fig_width, posB[3] / fig_height]) +ax = fig.add_axes((posB[0] / fig_width, posB[1] / fig_height, + posB[2] / fig_width, posB[3] / fig_height)) ax.imshow(B, vmin=-1, vmax=1) plt.show() # %% diff --git a/galleries/examples/misc/svg_filter_line.py b/galleries/examples/misc/svg_filter_line.py index c6adec093bee..dd97dc975eda 100644 --- a/galleries/examples/misc/svg_filter_line.py +++ b/galleries/examples/misc/svg_filter_line.py @@ -17,7 +17,7 @@ import matplotlib.transforms as mtransforms fig1 = plt.figure() -ax = fig1.add_axes([0.1, 0.1, 0.8, 0.8]) +ax = fig1.add_axes((0.1, 0.1, 0.8, 0.8)) # draw lines l1, = ax.plot([0.1, 0.5, 0.9], [0.1, 0.9, 0.5], "bo-", diff --git a/galleries/examples/misc/svg_filter_pie.py b/galleries/examples/misc/svg_filter_pie.py index b823cc9670c9..b19867be9a2f 100644 --- a/galleries/examples/misc/svg_filter_pie.py +++ b/galleries/examples/misc/svg_filter_pie.py @@ -19,7 +19,7 @@ # make a square figure and Axes fig = plt.figure(figsize=(6, 6)) -ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) +ax = fig.add_axes((0.1, 0.1, 0.8, 0.8)) labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' fracs = [15, 30, 45, 10] diff --git a/galleries/examples/showcase/anatomy.py b/galleries/examples/showcase/anatomy.py index b1fbde9c8d7b..798e4204cad3 100644 --- a/galleries/examples/showcase/anatomy.py +++ b/galleries/examples/showcase/anatomy.py @@ -27,7 +27,7 @@ Y3 = np.random.uniform(Y1, Y2, len(X)) fig = plt.figure(figsize=(7.5, 7.5)) -ax = fig.add_axes([0.2, 0.17, 0.68, 0.7], aspect=1) +ax = fig.add_axes((0.2, 0.17, 0.68, 0.7), aspect=1) ax.xaxis.set_major_locator(MultipleLocator(1.000)) ax.xaxis.set_minor_locator(AutoMinorLocator(4)) diff --git a/galleries/examples/showcase/firefox.py b/galleries/examples/showcase/firefox.py index 65682ccd7429..2026d253f6b6 100644 --- a/galleries/examples/showcase/firefox.py +++ b/galleries/examples/showcase/firefox.py @@ -48,7 +48,7 @@ def svg_parse(path): xmax, ymax = verts.max(axis=0) + 1 fig = plt.figure(figsize=(5, 5), facecolor="0.75") # gray background -ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1, +ax = fig.add_axes((0, 0, 1, 1), frameon=False, aspect=1, xlim=(xmin, xmax), # centering ylim=(ymax, ymin), # centering, upside down xticks=[], yticks=[]) # no ticks diff --git a/galleries/examples/showcase/mandelbrot.py b/galleries/examples/showcase/mandelbrot.py index ab40a061dc03..d8b7faf4c7b8 100644 --- a/galleries/examples/showcase/mandelbrot.py +++ b/galleries/examples/showcase/mandelbrot.py @@ -55,7 +55,7 @@ def mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0): width = 10 height = 10*yn/xn fig = plt.figure(figsize=(width, height), dpi=dpi) - ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1) + ax = fig.add_axes((0, 0, 1, 1), frameon=False, aspect=1) # Shaded rendering light = colors.LightSource(azdeg=315, altdeg=10) diff --git a/galleries/examples/subplots_axes_and_figures/axes_demo.py b/galleries/examples/subplots_axes_and_figures/axes_demo.py index 07f3ca2070c2..16db465449a4 100644 --- a/galleries/examples/subplots_axes_and_figures/axes_demo.py +++ b/galleries/examples/subplots_axes_and_figures/axes_demo.py @@ -33,12 +33,12 @@ main_ax.set_title('Gaussian colored noise') # this is an inset Axes over the main Axes -right_inset_ax = fig.add_axes([.65, .6, .2, .2], facecolor='k') +right_inset_ax = fig.add_axes((.65, .6, .2, .2), facecolor='k') right_inset_ax.hist(s, 400, density=True) right_inset_ax.set(title='Probability', xticks=[], yticks=[]) # this is another inset Axes over the main Axes -left_inset_ax = fig.add_axes([.2, .6, .2, .2], facecolor='k') +left_inset_ax = fig.add_axes((.2, .6, .2, .2), facecolor='k') left_inset_ax.plot(t[:len(r)], r) left_inset_ax.set(title='Impulse response', xlim=(0, .2), xticks=[], yticks=[]) diff --git a/galleries/examples/text_labels_and_annotations/mathtext_examples.py b/galleries/examples/text_labels_and_annotations/mathtext_examples.py index f9f8e628e08b..cf395f0daf0e 100644 --- a/galleries/examples/text_labels_and_annotations/mathtext_examples.py +++ b/galleries/examples/text_labels_and_annotations/mathtext_examples.py @@ -61,7 +61,7 @@ def doall(): # Creating figure and axis. fig = plt.figure(figsize=(7, 7)) - ax = fig.add_axes([0.01, 0.01, 0.98, 0.90], + ax = fig.add_axes((0.01, 0.01, 0.98, 0.90), facecolor="white", frameon=True) ax.set_xlim(0, 1) ax.set_ylim(0, 1) diff --git a/galleries/examples/ticks/fig_axes_customize_simple.py b/galleries/examples/ticks/fig_axes_customize_simple.py index 0dd85ec4bd93..07a569e3d31d 100644 --- a/galleries/examples/ticks/fig_axes_customize_simple.py +++ b/galleries/examples/ticks/fig_axes_customize_simple.py @@ -13,7 +13,7 @@ fig = plt.figure() fig.patch.set_facecolor('lightgoldenrodyellow') -ax1 = fig.add_axes([0.1, 0.3, 0.4, 0.4]) +ax1 = fig.add_axes((0.1, 0.3, 0.4, 0.4)) ax1.patch.set_facecolor('lightslategray') ax1.tick_params(axis='x', labelcolor='tab:red', labelrotation=45, labelsize=16) diff --git a/galleries/examples/widgets/buttons.py b/galleries/examples/widgets/buttons.py index 61249522c72c..2aef798399f4 100644 --- a/galleries/examples/widgets/buttons.py +++ b/galleries/examples/widgets/buttons.py @@ -41,8 +41,8 @@ def prev(self, event): plt.draw() callback = Index() -axprev = fig.add_axes([0.7, 0.05, 0.1, 0.075]) -axnext = fig.add_axes([0.81, 0.05, 0.1, 0.075]) +axprev = fig.add_axes((0.7, 0.05, 0.1, 0.075)) +axnext = fig.add_axes((0.81, 0.05, 0.1, 0.075)) bnext = Button(axnext, 'Next') bnext.on_clicked(callback.next) bprev = Button(axprev, 'Previous') diff --git a/galleries/examples/widgets/range_slider.py b/galleries/examples/widgets/range_slider.py index f1bed7431e39..d2f2d1554246 100644 --- a/galleries/examples/widgets/range_slider.py +++ b/galleries/examples/widgets/range_slider.py @@ -34,7 +34,7 @@ axs[1].set_title('Histogram of pixel intensities') # Create the RangeSlider -slider_ax = fig.add_axes([0.20, 0.1, 0.60, 0.03]) +slider_ax = fig.add_axes((0.20, 0.1, 0.60, 0.03)) slider = RangeSlider(slider_ax, "Threshold", img.min(), img.max()) # Create the Vertical lines on the histogram diff --git a/galleries/examples/widgets/slider_demo.py b/galleries/examples/widgets/slider_demo.py index 7dc47b9c7b6f..e56390c182a0 100644 --- a/galleries/examples/widgets/slider_demo.py +++ b/galleries/examples/widgets/slider_demo.py @@ -38,7 +38,7 @@ def f(t, amplitude, frequency): fig.subplots_adjust(left=0.25, bottom=0.25) # Make a horizontal slider to control the frequency. -axfreq = fig.add_axes([0.25, 0.1, 0.65, 0.03]) +axfreq = fig.add_axes((0.25, 0.1, 0.65, 0.03)) freq_slider = Slider( ax=axfreq, label='Frequency [Hz]', @@ -48,7 +48,7 @@ def f(t, amplitude, frequency): ) # Make a vertically oriented slider to control the amplitude -axamp = fig.add_axes([0.1, 0.25, 0.0225, 0.63]) +axamp = fig.add_axes((0.1, 0.25, 0.0225, 0.63)) amp_slider = Slider( ax=axamp, label="Amplitude", @@ -70,7 +70,7 @@ def update(val): amp_slider.on_changed(update) # Create a `matplotlib.widgets.Button` to reset the sliders to initial values. -resetax = fig.add_axes([0.8, 0.025, 0.1, 0.04]) +resetax = fig.add_axes((0.8, 0.025, 0.1, 0.04)) button = Button(resetax, 'Reset', hovercolor='0.975') diff --git a/galleries/examples/widgets/slider_snap_demo.py b/galleries/examples/widgets/slider_snap_demo.py index 953ffaf63672..5826be32fa07 100644 --- a/galleries/examples/widgets/slider_snap_demo.py +++ b/galleries/examples/widgets/slider_snap_demo.py @@ -30,8 +30,8 @@ fig.subplots_adjust(bottom=0.25) l, = ax.plot(t, s, lw=2) -ax_freq = fig.add_axes([0.25, 0.1, 0.65, 0.03]) -ax_amp = fig.add_axes([0.25, 0.15, 0.65, 0.03]) +ax_freq = fig.add_axes((0.25, 0.1, 0.65, 0.03)) +ax_amp = fig.add_axes((0.25, 0.15, 0.65, 0.03)) # define the values to use for snapping allowed_amplitudes = np.concatenate([np.linspace(.1, 5, 100), [6, 7, 8, 9]]) @@ -60,7 +60,7 @@ def update(val): sfreq.on_changed(update) samp.on_changed(update) -ax_reset = fig.add_axes([0.8, 0.025, 0.1, 0.04]) +ax_reset = fig.add_axes((0.8, 0.025, 0.1, 0.04)) button = Button(ax_reset, 'Reset', hovercolor='0.975') diff --git a/galleries/examples/widgets/textbox.py b/galleries/examples/widgets/textbox.py index d5f02b82a30b..2121ce8594ce 100644 --- a/galleries/examples/widgets/textbox.py +++ b/galleries/examples/widgets/textbox.py @@ -39,7 +39,7 @@ def submit(expression): plt.draw() -axbox = fig.add_axes([0.1, 0.05, 0.8, 0.075]) +axbox = fig.add_axes((0.1, 0.05, 0.8, 0.075)) text_box = TextBox(axbox, "Evaluate", textalignment="center") text_box.on_submit(submit) text_box.set_val("t ** 2") # Trigger `submit` with the initial string. diff --git a/galleries/tutorials/artists.py b/galleries/tutorials/artists.py index a258eb71d447..4f93f7c71a6e 100644 --- a/galleries/tutorials/artists.py +++ b/galleries/tutorials/artists.py @@ -70,7 +70,7 @@ class in the Matplotlib API, and the one you will be working with most coordinates:: fig2 = plt.figure() - ax2 = fig2.add_axes([0.15, 0.1, 0.7, 0.3]) + ax2 = fig2.add_axes((0.15, 0.1, 0.7, 0.3)) Continuing with our example:: @@ -134,7 +134,7 @@ class in the Matplotlib API, and the one you will be working with most # Fixing random state for reproducibility np.random.seed(19680801) -ax2 = fig.add_axes([0.15, 0.1, 0.7, 0.3]) +ax2 = fig.add_axes((0.15, 0.1, 0.7, 0.3)) n, bins, patches = ax2.hist(np.random.randn(1000), 50, facecolor='yellow', edgecolor='yellow') ax2.set_xlabel('Time [s]') @@ -295,7 +295,7 @@ class in the Matplotlib API, and the one you will be working with most # # In [157]: ax1 = fig.add_subplot(211) # -# In [158]: ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.3]) +# In [158]: ax2 = fig.add_axes((0.1, 0.1, 0.7, 0.3)) # # In [159]: ax1 # Out[159]: @@ -669,7 +669,7 @@ class in the Matplotlib API, and the one you will be working with most rect = fig.patch # a rectangle instance rect.set_facecolor('lightgoldenrodyellow') -ax1 = fig.add_axes([0.1, 0.3, 0.4, 0.4]) +ax1 = fig.add_axes((0.1, 0.3, 0.4, 0.4)) rect = ax1.patch rect.set_facecolor('lightslategray') diff --git a/galleries/users_explain/axes/arranging_axes.py b/galleries/users_explain/axes/arranging_axes.py index bc537e15c12c..64879d4a696d 100644 --- a/galleries/users_explain/axes/arranging_axes.py +++ b/galleries/users_explain/axes/arranging_axes.py @@ -103,8 +103,8 @@ w, h = 4, 3 margin = 0.5 fig = plt.figure(figsize=(w, h), facecolor='lightblue') -ax = fig.add_axes([margin / w, margin / h, (w - 2 * margin) / w, - (h - 2 * margin) / h]) +ax = fig.add_axes((margin / w, margin / h, + (w - 2 * margin) / w, (h - 2 * margin) / h)) # %% diff --git a/galleries/users_explain/axes/axes_intro.rst b/galleries/users_explain/axes/axes_intro.rst index 16738d929056..0ffbfd4ffdb4 100644 --- a/galleries/users_explain/axes/axes_intro.rst +++ b/galleries/users_explain/axes/axes_intro.rst @@ -52,8 +52,8 @@ Axes are added using methods on `~.Figure` objects, or via the `~.pyplot` interf There are a number of other methods for adding Axes to a Figure: -* `.Figure.add_axes`: manually position an Axes. ``fig.add_axes([0, 0, 1, - 1])`` makes an Axes that fills the whole figure. +* `.Figure.add_axes`: manually position an Axes. ``fig.add_axes((0, 0, 1, 1))`` makes an + Axes that fills the whole figure. * `.pyplot.subplots` and `.Figure.subplots`: add a grid of Axes as in the example above. The pyplot version returns both the Figure object and an array of Axes. Note that ``fig, ax = plt.subplots()`` adds a single Axes to a Figure. diff --git a/galleries/users_explain/colors/colors.py b/galleries/users_explain/colors/colors.py index c91a5fcb0dbe..97a281bf1977 100644 --- a/galleries/users_explain/colors/colors.py +++ b/galleries/users_explain/colors/colors.py @@ -197,7 +197,7 @@ def demo(sty): if f'xkcd:{name}' in mcolors.XKCD_COLORS} fig = plt.figure(figsize=[9, 5]) -ax = fig.add_axes([0, 0, 1, 1]) +ax = fig.add_axes((0, 0, 1, 1)) n_groups = 3 n_rows = len(overlap) // n_groups + 1 diff --git a/galleries/users_explain/text/text_props.py b/galleries/users_explain/text/text_props.py index c5ae22c02d38..fb67421fd880 100644 --- a/galleries/users_explain/text/text_props.py +++ b/galleries/users_explain/text/text_props.py @@ -75,7 +75,7 @@ top = bottom + height fig = plt.figure() -ax = fig.add_axes([0, 0, 1, 1]) +ax = fig.add_axes((0, 0, 1, 1)) # axes coordinates: (0, 0) is bottom left and (1, 1) is upper right p = patches.Rectangle( diff --git a/galleries/users_explain/toolkits/axisartist.rst b/galleries/users_explain/toolkits/axisartist.rst index eff2b575a63f..7ff0897f23d8 100644 --- a/galleries/users_explain/toolkits/axisartist.rst +++ b/galleries/users_explain/toolkits/axisartist.rst @@ -50,7 +50,7 @@ To create an Axes, :: import mpl_toolkits.axisartist as AA fig = plt.figure() - fig.add_axes([0.1, 0.1, 0.8, 0.8], axes_class=AA.Axes) + fig.add_axes((0.1, 0.1, 0.8, 0.8), axes_class=AA.Axes) or to create a subplot :: diff --git a/lib/matplotlib/_enums.py b/lib/matplotlib/_enums.py index 75a09b7b5d8c..d85c5c5f03db 100644 --- a/lib/matplotlib/_enums.py +++ b/lib/matplotlib/_enums.py @@ -151,7 +151,7 @@ def demo(): import matplotlib.pyplot as plt fig = plt.figure(figsize=(4, 1.2)) - ax = fig.add_axes([0, 0, 1, 0.8]) + ax = fig.add_axes((0, 0, 1, 0.8)) ax.set_title('Cap style') for x, style in enumerate(['butt', 'round', 'projecting']): diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index ec11e379db60..01ddd6c2bac5 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -3678,7 +3678,7 @@ def figaspect(arg): w, h = figaspect(2.) fig = Figure(figsize=(w, h)) - ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) + ax = fig.add_axes((0.1, 0.1, 0.8, 0.8)) ax.imshow(A, **kwargs) Make a figure with the proper aspect for an array:: @@ -3686,7 +3686,7 @@ def figaspect(arg): A = rand(5, 3) w, h = figaspect(A) fig = Figure(figsize=(w, h)) - ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) + ax = fig.add_axes((0.1, 0.1, 0.8, 0.8)) ax.imshow(A, **kwargs) """ diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 760235b6284d..e48c32bb8cf9 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -1772,7 +1772,7 @@ def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear', fig = Figure(figsize=(width, height), dpi=dpi) FigureCanvasBase(fig) - ax = fig.add_axes([0, 0, 1, 1], aspect='auto', + ax = fig.add_axes((0, 0, 1, 1), aspect='auto', frameon=False, xticks=[], yticks=[]) ax.imshow(im, aspect='auto', resample=True, interpolation=interpolation) fig.savefig(thumbfile, dpi=dpi) diff --git a/lib/matplotlib/tests/test_artist.py b/lib/matplotlib/tests/test_artist.py index 5c8141e40741..d6a1e2b7bd38 100644 --- a/lib/matplotlib/tests/test_artist.py +++ b/lib/matplotlib/tests/test_artist.py @@ -128,7 +128,7 @@ def test_clipping(): def test_clipping_zoom(fig_test, fig_ref): # This test places the Axes and sets its limits such that the clip path is # outside the figure entirely. This should not break the clip path. - ax_test = fig_test.add_axes([0, 0, 1, 1]) + ax_test = fig_test.add_axes((0, 0, 1, 1)) l, = ax_test.plot([-3, 3], [-3, 3]) # Explicit Path instead of a Rectangle uses clip path processing, instead # of a clip box optimization. @@ -136,7 +136,7 @@ def test_clipping_zoom(fig_test, fig_ref): p = mpatches.PathPatch(p, transform=ax_test.transData) l.set_clip_path(p) - ax_ref = fig_ref.add_axes([0, 0, 1, 1]) + ax_ref = fig_ref.add_axes((0, 0, 1, 1)) ax_ref.plot([-3, 3], [-3, 3]) ax_ref.set(xlim=(0.5, 0.75), ylim=(0.5, 0.75)) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index c1758d2ec3e0..8dea18f23368 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7126,7 +7126,7 @@ def shared_axes_generator(request): ax = ax_lst[0][0] elif request.param == 'add_axes': fig = plt.figure() - ax = fig.add_axes([.1, .1, .8, .8]) + ax = fig.add_axes((.1, .1, .8, .8)) return fig, ax @@ -7460,7 +7460,7 @@ def test_title_no_move_off_page(): # make sure that the automatic title repositioning does not get done. mpl.rcParams['axes.titley'] = None fig = plt.figure() - ax = fig.add_axes([0.1, -0.5, 0.8, 0.2]) + ax = fig.add_axes((0.1, -0.5, 0.8, 0.2)) ax.tick_params(axis="x", bottom=True, top=True, labelbottom=True, labeltop=True) tt = ax.set_title('Boo') @@ -8399,7 +8399,7 @@ def test_aspect_nonlinear_adjustable_box(): def test_aspect_nonlinear_adjustable_datalim(): fig = plt.figure(figsize=(10, 10)) # Square. - ax = fig.add_axes([.1, .1, .8, .8]) # Square. + ax = fig.add_axes((.1, .1, .8, .8)) # Square. ax.plot([.4, .6], [.4, .6]) # Set minpos to keep logit happy. ax.set(xscale="log", xlim=(1, 100), yscale="logit", ylim=(1 / 101, 1 / 11), @@ -8623,7 +8623,7 @@ def test_multiplot_autoscale(): def test_sharing_does_not_link_positions(): fig = plt.figure() ax0 = fig.add_subplot(221) - ax1 = fig.add_axes([.6, .6, .3, .3], sharex=ax0) + ax1 = fig.add_axes((.6, .6, .3, .3), sharex=ax0) init_pos = ax1.get_position() fig.subplots_adjust(left=0) assert (ax1.get_position().get_points() == init_pos.get_points()).all() @@ -9722,7 +9722,7 @@ def test_axes_set_position_external_bbox_unchanged(fig_test, fig_ref): ax_test = fig_test.add_axes(bbox) ax_test.set_position([0.25, 0.25, 0.5, 0.5]) assert (bbox.x0, bbox.y0, bbox.width, bbox.height) == (0.0, 0.0, 1.0, 1.0) - ax_ref = fig_ref.add_axes([0.25, 0.25, 0.5, 0.5]) + ax_ref = fig_ref.add_axes((0.25, 0.25, 0.5, 0.5)) def test_bar_shape_mismatch(): diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index f95f131e3bf6..b77fdb0c8ed8 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -332,11 +332,11 @@ def test_colorbar_closed_patch(): plt.rcParams['pcolormesh.snap'] = False fig = plt.figure(figsize=(8, 6)) - ax1 = fig.add_axes([0.05, 0.85, 0.9, 0.1]) - ax2 = fig.add_axes([0.1, 0.65, 0.75, 0.1]) - ax3 = fig.add_axes([0.05, 0.45, 0.9, 0.1]) - ax4 = fig.add_axes([0.05, 0.25, 0.9, 0.1]) - ax5 = fig.add_axes([0.05, 0.05, 0.9, 0.1]) + ax1 = fig.add_axes((0.05, 0.85, 0.9, 0.1)) + ax2 = fig.add_axes((0.1, 0.65, 0.75, 0.1)) + ax3 = fig.add_axes((0.05, 0.45, 0.9, 0.1)) + ax4 = fig.add_axes((0.05, 0.25, 0.9, 0.1)) + ax5 = fig.add_axes((0.05, 0.05, 0.9, 0.1)) cmap = mpl.colormaps["RdBu"].resampled(5) @@ -854,7 +854,7 @@ def test_axes_handles_same_functions(fig_ref, fig_test): for nn, fig in enumerate([fig_ref, fig_test]): ax = fig.add_subplot() pc = ax.pcolormesh(np.ones(300).reshape(10, 30)) - cax = fig.add_axes([0.9, 0.1, 0.03, 0.8]) + cax = fig.add_axes((0.9, 0.1, 0.03, 0.8)) cb = fig.colorbar(pc, cax=cax) if nn == 0: caxx = cax diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 8d0f3467f045..04e9b82c6e6e 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -857,9 +857,9 @@ def test_boundarynorm_and_colorbarbase(): # Make a figure and axes with dimensions as desired. fig = plt.figure() - ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15]) - ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15]) - ax3 = fig.add_axes([0.05, 0.15, 0.9, 0.15]) + ax1 = fig.add_axes((0.05, 0.80, 0.9, 0.15)) + ax2 = fig.add_axes((0.05, 0.475, 0.9, 0.15)) + ax3 = fig.add_axes((0.05, 0.15, 0.9, 0.15)) # Set the colormap and bounds bounds = [-1, 2, 5, 7, 12, 15] diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 7c7dd43a3115..05f4723cb7a4 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -309,7 +309,7 @@ def test_constrained_layout16(): """Test ax.set_position.""" fig, ax = plt.subplots(layout="constrained") example_plot(ax, fontsize=12) - ax2 = fig.add_axes([0.2, 0.2, 0.4, 0.4]) + ax2 = fig.add_axes((0.2, 0.2, 0.4, 0.4)) @image_comparison(['constrained_layout17.png'], style='mpl20') @@ -357,7 +357,7 @@ def test_constrained_layout20(): img = np.hypot(gx, gx[:, None]) fig = plt.figure() - ax = fig.add_axes([0, 0, 1, 1]) + ax = fig.add_axes((0, 0, 1, 1)) mesh = ax.pcolormesh(gx, gx, img[:-1, :-1]) fig.colorbar(mesh) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 014eb2cf23d0..5668c770c361 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -241,7 +241,7 @@ def test_gca(): fig = plt.figure() # test that gca() picks up Axes created via add_axes() - ax0 = fig.add_axes([0, 0, 1, 1]) + ax0 = fig.add_axes((0, 0, 1, 1)) assert fig.gca() is ax0 # test that gca() picks up Axes created via add_subplot() @@ -546,7 +546,7 @@ def test_invalid_figure_add_axes(): fig.add_axes((.1, .1, .5, np.nan)) with pytest.raises(TypeError, match="multiple values for argument 'rect'"): - fig.add_axes([0, 0, 1, 1], rect=[0, 0, 1, 1]) + fig.add_axes((0, 0, 1, 1), rect=[0, 0, 1, 1]) fig2, ax = plt.subplots() with pytest.raises(ValueError, @@ -559,7 +559,7 @@ def test_invalid_figure_add_axes(): fig2.add_axes(ax, "extra positional argument") with pytest.raises(TypeError, match=r"add_axes\(\) takes 1 positional arguments"): - fig.add_axes([0, 0, 1, 1], "extra positional argument") + fig.add_axes((0, 0, 1, 1), "extra positional argument") def test_subplots_shareax_loglabels(): @@ -1583,22 +1583,22 @@ def test_add_subplot_kwargs(): def test_add_axes_kwargs(): # fig.add_axes() always creates new axes, even if axes kwargs differ. fig = plt.figure() - ax = fig.add_axes([0, 0, 1, 1]) - ax1 = fig.add_axes([0, 0, 1, 1]) + ax = fig.add_axes((0, 0, 1, 1)) + ax1 = fig.add_axes((0, 0, 1, 1)) assert ax is not None assert ax1 is not ax plt.close() fig = plt.figure() - ax = fig.add_axes([0, 0, 1, 1], projection='polar') - ax1 = fig.add_axes([0, 0, 1, 1], projection='polar') + ax = fig.add_axes((0, 0, 1, 1), projection='polar') + ax1 = fig.add_axes((0, 0, 1, 1), projection='polar') assert ax is not None assert ax1 is not ax plt.close() fig = plt.figure() - ax = fig.add_axes([0, 0, 1, 1], projection='polar') - ax1 = fig.add_axes([0, 0, 1, 1]) + ax = fig.add_axes((0, 0, 1, 1), projection='polar') + ax1 = fig.add_axes((0, 0, 1, 1)) assert ax is not None assert ax1.name == 'rectilinear' assert ax1 is not ax diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 93124141487f..5401dd73e5ef 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -504,7 +504,7 @@ def test_image_shift(): def test_image_edges(): fig = plt.figure(figsize=[1, 1]) - ax = fig.add_axes([0, 0, 1, 1], frameon=False) + ax = fig.add_axes((0, 0, 1, 1), frameon=False) data = np.tile(np.arange(12), 15).reshape(20, 9) @@ -1192,7 +1192,7 @@ def test_exact_vmin(): # make the image exactly 190 pixels wide fig = plt.figure(figsize=(1.9, 0.1), dpi=100) - ax = fig.add_axes([0, 0, 1, 1]) + ax = fig.add_axes((0, 0, 1, 1)) data = np.array( [[-1, -1, -1, 0, 0, 0, 0, 43, 79, 95, 66, 1, -1, -1, -1, 0, 0, 0, 34]], diff --git a/lib/matplotlib/tests/test_marker.py b/lib/matplotlib/tests/test_marker.py index f6e20c148897..a1e71f1f6533 100644 --- a/lib/matplotlib/tests/test_marker.py +++ b/lib/matplotlib/tests/test_marker.py @@ -181,9 +181,9 @@ def test_marker_clipping(fig_ref, fig_test): width = 2 * marker_size * ncol height = 2 * marker_size * nrow * 2 fig_ref.set_size_inches((width / fig_ref.dpi, height / fig_ref.dpi)) - ax_ref = fig_ref.add_axes([0, 0, 1, 1]) + ax_ref = fig_ref.add_axes((0, 0, 1, 1)) fig_test.set_size_inches((width / fig_test.dpi, height / fig_ref.dpi)) - ax_test = fig_test.add_axes([0, 0, 1, 1]) + ax_test = fig_test.add_axes((0, 0, 1, 1)) for i, marker in enumerate(markers.MarkerStyle.markers): x = i % ncol diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index f18fa7c777d1..81fe062d83b2 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -455,7 +455,7 @@ def test_remove_draggable(): def test_draggable_in_subfigure(): fig = plt.figure() # Put annotation at lower left corner to make it easily pickable below. - ann = fig.subfigures().add_axes([0, 0, 1, 1]).annotate("foo", (0, 0)) + ann = fig.subfigures().add_axes((0, 0, 1, 1)).annotate("foo", (0, 0)) ann.draggable(True) fig.canvas.draw() # Texts are non-pickable until the first draw. MouseEvent("button_press_event", fig.canvas, 1, 1)._process() diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index a0969df5de90..31e8cdd89a21 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -150,7 +150,7 @@ def test_polar_rmin(): theta = 2*np.pi*r fig = plt.figure() - ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True) + ax = fig.add_axes((0.1, 0.1, 0.8, 0.8), polar=True) ax.plot(theta, r) ax.set_rmax(2.0) ax.set_rmin(0.5) @@ -162,7 +162,7 @@ def test_polar_negative_rmin(): theta = 2*np.pi*r fig = plt.figure() - ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True) + ax = fig.add_axes((0.1, 0.1, 0.8, 0.8), polar=True) ax.plot(theta, r) ax.set_rmax(0.0) ax.set_rmin(-3.0) @@ -174,7 +174,7 @@ def test_polar_rorigin(): theta = 2*np.pi*r fig = plt.figure() - ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True) + ax = fig.add_axes((0.1, 0.1, 0.8, 0.8), polar=True) ax.plot(theta, r) ax.set_rmax(2.0) ax.set_rmin(0.5) @@ -184,14 +184,14 @@ def test_polar_rorigin(): @image_comparison(['polar_invertedylim.png'], style='default') def test_polar_invertedylim(): fig = plt.figure() - ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True) + ax = fig.add_axes((0.1, 0.1, 0.8, 0.8), polar=True) ax.set_ylim(2, 0) @image_comparison(['polar_invertedylim_rorigin.png'], style='default') def test_polar_invertedylim_rorigin(): fig = plt.figure() - ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True) + ax = fig.add_axes((0.1, 0.1, 0.8, 0.8), polar=True) ax.yaxis.set_inverted(True) # Set the rlims to inverted (2, 0) without calling set_rlim, to check that # viewlims are correctly unstaled before draw()ing. @@ -206,7 +206,7 @@ def test_polar_theta_position(): theta = 2*np.pi*r fig = plt.figure() - ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True) + ax = fig.add_axes((0.1, 0.1, 0.8, 0.8), polar=True) ax.plot(theta, r) ax.set_theta_zero_location("NW", 30) ax.set_theta_direction('clockwise') diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index c7658c4f42ac..c92beed7f2c6 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -168,7 +168,7 @@ def test_rotation(): mpl.rcParams['text.usetex'] = True fig = plt.figure() - ax = fig.add_axes([0, 0, 1, 1]) + ax = fig.add_axes((0, 0, 1, 1)) ax.set(xlim=[-0.5, 5], xticks=[], ylim=[-0.5, 3], yticks=[], frame_on=False) text = {val: val[0] for val in ['top', 'center', 'bottom', 'left', 'right']} diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 6b196571814d..e62379470db1 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1841,7 +1841,7 @@ def __init__(self, targetfig, toolfig): self.sliderbottom.slidermax = self.slidertop self.slidertop.slidermin = self.sliderbottom - bax = toolfig.add_axes([0.8, 0.05, 0.15, 0.075]) + bax = toolfig.add_axes((0.8, 0.05, 0.15, 0.075)) self.buttonreset = Button(bax, 'Reset') self.buttonreset.on_clicked(self._on_reset) diff --git a/lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py b/lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py index 496ce74d72c0..6748c1d7e1c0 100644 --- a/lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py +++ b/lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py @@ -678,7 +678,7 @@ def test_mark_inset_unstales_viewlim(fig_test, fig_ref): def test_auto_adjustable(): fig = plt.figure() - ax = fig.add_axes([0, 0, 1, 1]) + ax = fig.add_axes((0, 0, 1, 1)) pad = 0.1 make_axes_area_auto_adjustable(ax, pad=pad) fig.canvas.draw() diff --git a/tools/make_icons.py b/tools/make_icons.py index f09d40e92256..b253c0517c43 100755 --- a/tools/make_icons.py +++ b/tools/make_icons.py @@ -64,7 +64,7 @@ def make_icon(font_path, ccode): def make_matplotlib_icon(): fig = plt.figure(figsize=(1, 1)) fig.patch.set_alpha(0.0) - ax = fig.add_axes([0.025, 0.025, 0.95, 0.95], projection='polar') + ax = fig.add_axes((0.025, 0.025, 0.95, 0.95), projection='polar') ax.set_axisbelow(True) N = 7 From db4e67cf385074bdee7be33ae349c58fb2cbb3f5 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Thu, 29 Aug 2024 10:42:28 +0200 Subject: [PATCH 02/50] Change first argument to set_[xyz]lim to two arguments --- .../prev_whats_new/dflt_style_changes.rst | 2 +- doc/users/prev_whats_new/whats_new_3.5.0.rst | 8 +-- .../examples/animation/simple_scatter.py | 2 +- .../examples/event_handling/poly_editor.py | 4 +- .../examples/event_handling/pong_sgskip.py | 4 +- .../eventcollection_demo.py | 4 +- .../lines_bars_and_markers/markevery_demo.py | 4 +- .../shapes_and_collections/hatch_demo.py | 4 +- galleries/examples/showcase/xkcd.py | 6 +-- .../examples/statistics/errorbar_limits.py | 2 +- .../user_interfaces/fourier_demo_wx_sgskip.py | 8 +-- .../artists/transforms_tutorial.py | 2 +- galleries/users_explain/axes/axes_intro.rst | 2 +- galleries/users_explain/axes/axes_scales.py | 2 +- lib/matplotlib/axes/_base.py | 4 +- lib/matplotlib/image.py | 4 +- lib/matplotlib/tests/test_arrow_patches.py | 4 +- lib/matplotlib/tests/test_artist.py | 8 +-- lib/matplotlib/tests/test_axes.py | 50 +++++++++---------- lib/matplotlib/tests/test_axis.py | 4 +- lib/matplotlib/tests/test_colorbar.py | 2 +- lib/matplotlib/tests/test_dates.py | 2 +- lib/matplotlib/tests/test_image.py | 22 ++++---- lib/matplotlib/tests/test_legend.py | 4 +- lib/matplotlib/tests/test_lines.py | 4 +- lib/matplotlib/tests/test_offsetbox.py | 4 +- lib/matplotlib/tests/test_path.py | 4 +- lib/matplotlib/tests/test_simplification.py | 10 ++-- lib/matplotlib/tests/test_text.py | 4 +- lib/matplotlib/tests/test_tightlayout.py | 4 +- lib/matplotlib/widgets.py | 4 +- .../axisartist/tests/test_axislines.py | 4 +- lib/mpl_toolkits/mplot3d/tests/test_axes3d.py | 6 +-- 33 files changed, 101 insertions(+), 101 deletions(-) diff --git a/doc/users/prev_whats_new/dflt_style_changes.rst b/doc/users/prev_whats_new/dflt_style_changes.rst index a833064b573b..808204383fb8 100644 --- a/doc/users/prev_whats_new/dflt_style_changes.rst +++ b/doc/users/prev_whats_new/dflt_style_changes.rst @@ -1005,7 +1005,7 @@ a cleaner separation between subplots. ax = fig.add_subplot(2, 2, j) ax.hist(np.random.beta(0.5, 0.5, 10000), 25, density=True) - ax.set_xlim([0, 1]) + ax.set_xlim(0, 1) ax.set_title(title) ax = fig.add_subplot(2, 2, j + 2) diff --git a/doc/users/prev_whats_new/whats_new_3.5.0.rst b/doc/users/prev_whats_new/whats_new_3.5.0.rst index 54687e9a487e..fb156d0c68e8 100644 --- a/doc/users/prev_whats_new/whats_new_3.5.0.rst +++ b/doc/users/prev_whats_new/whats_new_3.5.0.rst @@ -375,9 +375,9 @@ attribute. points = ax.scatter((3, 3), (1, 3), (1, 3), c='red', zorder=10, label='zorder=10') - ax.set_xlim((0, 5)) - ax.set_ylim((0, 5)) - ax.set_zlim((0, 2.5)) + ax.set_xlim(0, 5) + ax.set_ylim(0, 5) + ax.set_zlim(0, 2.5) plane = mpatches.Patch(facecolor='white', edgecolor='black', label='zorder=1') @@ -485,7 +485,7 @@ new styling parameters for the added handles. ax = ax_old valmin = 0 valinit = 0.5 - ax.set_xlim([0, 1]) + ax.set_xlim(0, 1) ax_old.axvspan(valmin, valinit, 0, 1) ax.axvline(valinit, 0, 1, color="r", lw=1) ax.set_xticks([]) diff --git a/galleries/examples/animation/simple_scatter.py b/galleries/examples/animation/simple_scatter.py index 3f8c285810a3..5afef75f6074 100644 --- a/galleries/examples/animation/simple_scatter.py +++ b/galleries/examples/animation/simple_scatter.py @@ -11,7 +11,7 @@ import matplotlib.animation as animation fig, ax = plt.subplots() -ax.set_xlim([0, 10]) +ax.set_xlim(0, 10) scat = ax.scatter(1, 0) x = np.linspace(0, 10) diff --git a/galleries/examples/event_handling/poly_editor.py b/galleries/examples/event_handling/poly_editor.py index f6efd8bb8446..9cc2e5373ae5 100644 --- a/galleries/examples/event_handling/poly_editor.py +++ b/galleries/examples/event_handling/poly_editor.py @@ -203,6 +203,6 @@ def on_mouse_move(self, event): p = PolygonInteractor(ax, poly) ax.set_title('Click and drag a point to move it') - ax.set_xlim((-2, 2)) - ax.set_ylim((-2, 2)) + ax.set_xlim(-2, 2) + ax.set_ylim(-2, 2) plt.show() diff --git a/galleries/examples/event_handling/pong_sgskip.py b/galleries/examples/event_handling/pong_sgskip.py index 583e51eacdc5..2c4c35a7cb35 100644 --- a/galleries/examples/event_handling/pong_sgskip.py +++ b/galleries/examples/event_handling/pong_sgskip.py @@ -134,9 +134,9 @@ def __init__(self, ax): # create the initial line self.ax = ax ax.xaxis.set_visible(False) - ax.set_xlim([0, 7]) + ax.set_xlim(0, 7) ax.yaxis.set_visible(False) - ax.set_ylim([-1, 1]) + ax.set_ylim(-1, 1) pad_a_x = 0 pad_b_x = .50 pad_a_y = pad_b_y = .30 diff --git a/galleries/examples/lines_bars_and_markers/eventcollection_demo.py b/galleries/examples/lines_bars_and_markers/eventcollection_demo.py index 1aa2fa622812..6854a13e0974 100644 --- a/galleries/examples/lines_bars_and_markers/eventcollection_demo.py +++ b/galleries/examples/lines_bars_and_markers/eventcollection_demo.py @@ -53,8 +53,8 @@ ax.add_collection(yevents2) # set the limits -ax.set_xlim([0, 1]) -ax.set_ylim([0, 1]) +ax.set_xlim(0, 1) +ax.set_ylim(0, 1) ax.set_title('line plot with data points') diff --git a/galleries/examples/lines_bars_and_markers/markevery_demo.py b/galleries/examples/lines_bars_and_markers/markevery_demo.py index 919e12cde952..da4da0ecf9f1 100644 --- a/galleries/examples/lines_bars_and_markers/markevery_demo.py +++ b/galleries/examples/lines_bars_and_markers/markevery_demo.py @@ -79,8 +79,8 @@ for ax, markevery in zip(axs.flat, cases): ax.set_title(f'markevery={markevery}') ax.plot(x, y, 'o', ls='-', ms=4, markevery=markevery) - ax.set_xlim((6, 6.7)) - ax.set_ylim((1.1, 1.7)) + ax.set_xlim(6, 6.7) + ax.set_ylim(1.1, 1.7) # %% # markevery on polar plots diff --git a/galleries/examples/shapes_and_collections/hatch_demo.py b/galleries/examples/shapes_and_collections/hatch_demo.py index f2ca490c4e37..8d44dba5489b 100644 --- a/galleries/examples/shapes_and_collections/hatch_demo.py +++ b/galleries/examples/shapes_and_collections/hatch_demo.py @@ -41,8 +41,8 @@ hatch='*', facecolor='y')) axs['patches'].add_patch(Polygon([(10, 20), (30, 50), (50, 10)], hatch='\\/...', facecolor='g')) -axs['patches'].set_xlim([0, 40]) -axs['patches'].set_ylim([10, 60]) +axs['patches'].set_xlim(0, 40) +axs['patches'].set_ylim(10, 60) axs['patches'].set_aspect(1) plt.show() diff --git a/galleries/examples/showcase/xkcd.py b/galleries/examples/showcase/xkcd.py index 3d6d5418a13f..9b4de0a90f5b 100644 --- a/galleries/examples/showcase/xkcd.py +++ b/galleries/examples/showcase/xkcd.py @@ -19,7 +19,7 @@ ax.spines[['top', 'right']].set_visible(False) ax.set_xticks([]) ax.set_yticks([]) - ax.set_ylim([-30, 10]) + ax.set_ylim(-30, 10) data = np.ones(100) data[70:] -= np.arange(30) @@ -50,9 +50,9 @@ ax.xaxis.set_ticks_position('bottom') ax.set_xticks([0, 1]) ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT']) - ax.set_xlim([-0.5, 1.5]) + ax.set_xlim(-0.5, 1.5) ax.set_yticks([]) - ax.set_ylim([0, 110]) + ax.set_ylim(0, 110) ax.set_title("CLAIMS OF SUPERNATURAL POWERS") diff --git a/galleries/examples/statistics/errorbar_limits.py b/galleries/examples/statistics/errorbar_limits.py index f1d26460d947..fde18327af83 100644 --- a/galleries/examples/statistics/errorbar_limits.py +++ b/galleries/examples/statistics/errorbar_limits.py @@ -71,7 +71,7 @@ linestyle='none') # tidy up the figure -ax.set_xlim((0, 5.5)) +ax.set_xlim(0, 5.5) ax.set_title('Errorbar upper and lower limits') plt.show() diff --git a/galleries/examples/user_interfaces/fourier_demo_wx_sgskip.py b/galleries/examples/user_interfaces/fourier_demo_wx_sgskip.py index f51917fda6b9..9e72b3745a40 100644 --- a/galleries/examples/user_interfaces/fourier_demo_wx_sgskip.py +++ b/galleries/examples/user_interfaces/fourier_demo_wx_sgskip.py @@ -194,10 +194,10 @@ def createPlots(self): self.subplot1.set_xlabel("frequency f", fontsize=8) self.subplot2.set_ylabel("Time Domain Waveform x(t)", fontsize=8) self.subplot2.set_xlabel("time t", fontsize=8) - self.subplot1.set_xlim([-6, 6]) - self.subplot1.set_ylim([0, 1]) - self.subplot2.set_xlim([-2, 2]) - self.subplot2.set_ylim([-2, 2]) + self.subplot1.set_xlim(-6, 6) + self.subplot1.set_ylim(0, 1) + self.subplot2.set_xlim(-2, 2) + self.subplot2.set_ylim(-2, 2) self.subplot1.text(0.05, .95, r'$X(f) = \mathcal{F}\{x(t)\}$', verticalalignment='top', diff --git a/galleries/users_explain/artists/transforms_tutorial.py b/galleries/users_explain/artists/transforms_tutorial.py index f8a3e98e8077..3920fe886c7f 100644 --- a/galleries/users_explain/artists/transforms_tutorial.py +++ b/galleries/users_explain/artists/transforms_tutorial.py @@ -401,7 +401,7 @@ fig, ax = plt.subplots() xdata, ydata = (0.2, 0.7), (0.5, 0.5) ax.plot(xdata, ydata, "o") -ax.set_xlim((0, 1)) +ax.set_xlim(0, 1) trans = (fig.dpi_scale_trans + transforms.ScaledTranslation(xdata[0], ydata[0], ax.transData)) diff --git a/galleries/users_explain/axes/axes_intro.rst b/galleries/users_explain/axes/axes_intro.rst index 0ffbfd4ffdb4..bb3094495026 100644 --- a/galleries/users_explain/axes/axes_intro.rst +++ b/galleries/users_explain/axes/axes_intro.rst @@ -143,7 +143,7 @@ Other important methods set the extent on the axes (`~.axes.Axes.set_xlim`, `~.a x = 2**np.cumsum(np.random.randn(200)) linesx = ax.plot(t, x) ax.set_yscale('log') - ax.set_xlim([20, 180]) + ax.set_xlim(20, 180) The Axes class also has helpers to deal with Axis ticks and their labels. Most straight-forward is `~.axes.Axes.set_xticks` and `~.axes.Axes.set_yticks` which manually set the tick locations and optionally their labels. Minor ticks can be toggled with `~.axes.Axes.minorticks_on` or `~.axes.Axes.minorticks_off`. diff --git a/galleries/users_explain/axes/axes_scales.py b/galleries/users_explain/axes/axes_scales.py index 6b163835070c..f901c012974a 100644 --- a/galleries/users_explain/axes/axes_scales.py +++ b/galleries/users_explain/axes/axes_scales.py @@ -171,7 +171,7 @@ def inverse(a): ax.set_yscale('function', functions=(forward, inverse)) ax.set_title('function: Mercator') ax.grid(True) -ax.set_xlim([0, 180]) +ax.set_xlim(0, 180) ax.yaxis.set_minor_formatter(NullFormatter()) ax.yaxis.set_major_locator(FixedLocator(np.arange(0, 90, 10))) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index c5c525b29a06..987ffc8defa5 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2179,9 +2179,9 @@ def axis(self, arg=None, /, *, emit=True, **kwargs): xlim = self.get_xlim() ylim = self.get_ylim() edge_size = max(np.diff(xlim), np.diff(ylim))[0] - self.set_xlim([xlim[0], xlim[0] + edge_size], + self.set_xlim(xlim[0], xlim[0] + edge_size, emit=emit, auto=False) - self.set_ylim([ylim[0], ylim[0] + edge_size], + self.set_ylim(ylim[0], ylim[0] + edge_size, emit=emit, auto=False) else: raise ValueError(f"Unrecognized string {arg!r} to axis; " diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index e48c32bb8cf9..afb75d042c6d 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -967,9 +967,9 @@ def set_extent(self, extent, **kwargs): self.sticky_edges.x[:] = [xmin, xmax] self.sticky_edges.y[:] = [ymin, ymax] if self.axes.get_autoscalex_on(): - self.axes.set_xlim((xmin, xmax), auto=None) + self.axes.set_xlim(xmin, xmax, auto=None) if self.axes.get_autoscaley_on(): - self.axes.set_ylim((ymin, ymax), auto=None) + self.axes.set_ylim(ymin, ymax, auto=None) self.stale = True def get_extent(self): diff --git a/lib/matplotlib/tests/test_arrow_patches.py b/lib/matplotlib/tests/test_arrow_patches.py index c2b6d4fa8086..e26c806c9ea4 100644 --- a/lib/matplotlib/tests/test_arrow_patches.py +++ b/lib/matplotlib/tests/test_arrow_patches.py @@ -59,8 +59,8 @@ def __prepare_fancyarrow_dpi_cor_test(): """ fig2 = plt.figure("fancyarrow_dpi_cor_test", figsize=(4, 3), dpi=50) ax = fig2.add_subplot() - ax.set_xlim([0, 1]) - ax.set_ylim([0, 1]) + ax.set_xlim(0, 1) + ax.set_ylim(0, 1) ax.add_patch(mpatches.FancyArrowPatch(posA=(0.3, 0.4), posB=(0.8, 0.6), lw=3, arrowstyle='->', mutation_scale=100)) diff --git a/lib/matplotlib/tests/test_artist.py b/lib/matplotlib/tests/test_artist.py index d6a1e2b7bd38..1367701ffe3e 100644 --- a/lib/matplotlib/tests/test_artist.py +++ b/lib/matplotlib/tests/test_artist.py @@ -120,8 +120,8 @@ def test_clipping(): patch.set_clip_path(clip_path, ax2.transData) ax2.add_patch(patch) - ax1.set_xlim([-3, 3]) - ax1.set_ylim([-3, 3]) + ax1.set_xlim(-3, 3) + ax1.set_ylim(-3, 3) @check_figures_equal() @@ -226,8 +226,8 @@ def test_default_edges(): np.arange(10) + 1, np.arange(10), 'o') ax2.bar(np.arange(10), np.arange(10), align='edge') ax3.text(0, 0, "BOX", size=24, bbox=dict(boxstyle='sawtooth')) - ax3.set_xlim((-1, 1)) - ax3.set_ylim((-1, 1)) + ax3.set_xlim(-1, 1) + ax3.set_ylim(-1, 1) pp1 = mpatches.PathPatch( mpath.Path([(0, 0), (1, 0), (1, 1), (0, 0)], [mpath.Path.MOVETO, mpath.Path.CURVE3, diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 8dea18f23368..50e214b15225 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -3262,16 +3262,16 @@ def test_stackplot(): y3 = 3.0 * x + 2 ax = fig.add_subplot(1, 1, 1) ax.stackplot(x, y1, y2, y3) - ax.set_xlim((0, 10)) - ax.set_ylim((0, 70)) + ax.set_xlim(0, 10) + ax.set_ylim(0, 70) # Reuse testcase from above for a test with labeled data and with colours # from the Axes property cycle. data = {"x": x, "y1": y1, "y2": y2, "y3": y3} fig, ax = plt.subplots() ax.stackplot("x", "y1", "y2", "y3", data=data, colors=["C0", "C1", "C2"]) - ax.set_xlim((0, 10)) - ax.set_ylim((0, 70)) + ax.set_xlim(0, 10) + ax.set_ylim(0, 70) @image_comparison(['stackplot_test_baseline.png'], remove_text=True) @@ -3308,16 +3308,16 @@ def test_stackplot_hatching(fig_ref, fig_test): # stackplot with different hatching styles (issue #27146) ax_test = fig_test.subplots() ax_test.stackplot(x, y1, y2, y3, hatch=["x", "//", "\\\\"], colors=["white"]) - ax_test.set_xlim((0, 10)) - ax_test.set_ylim((0, 70)) + ax_test.set_xlim(0, 10) + ax_test.set_ylim(0, 70) # compare with result from hatching each layer individually stack_baseline = np.zeros(len(x)) ax_ref = fig_ref.subplots() ax_ref.fill_between(x, stack_baseline, y1, hatch="x", facecolor="white") ax_ref.fill_between(x, y1, y1+y2, hatch="//", facecolor="white") ax_ref.fill_between(x, y1+y2, y1+y2+y3, hatch="\\\\", facecolor="white") - ax_ref.set_xlim((0, 10)) - ax_ref.set_ylim((0, 70)) + ax_ref.set_xlim(0, 10) + ax_ref.set_ylim(0, 70) def _bxp_test_helper( @@ -3594,13 +3594,13 @@ def test_boxplot(): fig, ax = plt.subplots() ax.boxplot([x, x], bootstrap=10000, notch=1) - ax.set_ylim((-30, 30)) + ax.set_ylim(-30, 30) # Reuse testcase from above for a labeled data test data = {"x": [x, x]} fig, ax = plt.subplots() ax.boxplot("x", bootstrap=10000, notch=1, data=data) - ax.set_ylim((-30, 30)) + ax.set_ylim(-30, 30) @check_figures_equal() @@ -3638,10 +3638,10 @@ def test_boxplot_sym2(): fig, [ax1, ax2] = plt.subplots(1, 2) ax1.boxplot([x, x], bootstrap=10000, sym='^') - ax1.set_ylim((-30, 30)) + ax1.set_ylim(-30, 30) ax2.boxplot([x, x], bootstrap=10000, sym='g') - ax2.set_ylim((-30, 30)) + ax2.set_ylim(-30, 30) @image_comparison(['boxplot_sym.png'], @@ -3654,7 +3654,7 @@ def test_boxplot_sym(): fig, ax = plt.subplots() ax.boxplot([x, x], sym='gs') - ax.set_ylim((-30, 30)) + ax.set_ylim(-30, 30) @image_comparison(['boxplot_autorange_false_whiskers.png', @@ -3669,11 +3669,11 @@ def test_boxplot_autorange_whiskers(): fig1, ax1 = plt.subplots() ax1.boxplot([x, x], bootstrap=10000, notch=1) - ax1.set_ylim((-5, 5)) + ax1.set_ylim(-5, 5) fig2, ax2 = plt.subplots() ax2.boxplot([x, x], bootstrap=10000, notch=1, autorange=True) - ax2.set_ylim((-5, 5)) + ax2.set_ylim(-5, 5) def _rc_test_bxp_helper(ax, rc_dict): @@ -3763,7 +3763,7 @@ def test_boxplot_with_CIarray(): # another with manual values ax.boxplot([x, x], bootstrap=10000, usermedians=[None, 1.0], conf_intervals=CIs, notch=1) - ax.set_ylim((-30, 30)) + ax.set_ylim(-30, 30) @image_comparison(['boxplot_no_inverted_whisker.png'], @@ -4352,7 +4352,7 @@ def test_errorbar_limits(): xlolims=xlolims, xuplims=xuplims, uplims=uplims, lolims=lolims, ls='none', mec='blue', capsize=0, color='cyan') - ax.set_xlim((0, 5.5)) + ax.set_xlim(0, 5.5) ax.set_title('Errorbar upper and lower limits') @@ -5282,8 +5282,8 @@ def test_vertex_markers(): fig, ax = plt.subplots() ax.plot(data, linestyle='', marker=marker_as_tuple, mfc='k') ax.plot(data[::-1], linestyle='', marker=marker_as_list, mfc='b') - ax.set_xlim([-1, 10]) - ax.set_ylim([-1, 10]) + ax.set_xlim(-1, 10) + ax.set_ylim(-1, 10) @image_comparison(['vline_hline_zorder.png', 'errorbar_zorder.png'], @@ -5552,8 +5552,8 @@ def test_step_linestyle(): ax.step(x, y, lw=5, linestyle=ls, where='pre') ax.step(x, y + 1, lw=5, linestyle=ls, where='mid') ax.step(x, y + 2, lw=5, linestyle=ls, where='post') - ax.set_xlim([-1, 5]) - ax.set_ylim([-1, 7]) + ax.set_xlim(-1, 5) + ax.set_ylim(-1, 7) # Reuse testcase from above for a labeled data test data = {"X": x, "Y0": y, "Y1": y+1, "Y2": y+2} @@ -5564,8 +5564,8 @@ def test_step_linestyle(): ax.step("X", "Y0", lw=5, linestyle=ls, where='pre', data=data) ax.step("X", "Y1", lw=5, linestyle=ls, where='mid', data=data) ax.step("X", "Y2", lw=5, linestyle=ls, where='post', data=data) - ax.set_xlim([-1, 5]) - ax.set_ylim([-1, 7]) + ax.set_xlim(-1, 5) + ax.set_ylim(-1, 7) @image_comparison(['mixed_collection'], remove_text=True) @@ -7802,8 +7802,8 @@ def test_zoom_inset(): axin1 = ax.inset_axes([0.7, 0.7, 0.35, 0.35]) # redraw the data in the inset axes... axin1.pcolormesh(x, y, z[:-1, :-1]) - axin1.set_xlim([1.5, 2.15]) - axin1.set_ylim([2, 2.5]) + axin1.set_xlim(1.5, 2.15) + axin1.set_ylim(2, 2.5) axin1.set_aspect(ax.get_aspect()) with pytest.warns(mpl.MatplotlibDeprecationWarning): diff --git a/lib/matplotlib/tests/test_axis.py b/lib/matplotlib/tests/test_axis.py index e33656ea9c17..5cb3ff4d3856 100644 --- a/lib/matplotlib/tests/test_axis.py +++ b/lib/matplotlib/tests/test_axis.py @@ -15,9 +15,9 @@ def test_axis_not_in_layout(): fig2, (ax2_left, ax2_right) = plt.subplots(ncols=2, layout='constrained') # 100 label overlapping the end of the axis - ax1_left.set_xlim([0, 100]) + ax1_left.set_xlim(0, 100) # 100 label not overlapping the end of the axis - ax2_left.set_xlim([0, 120]) + ax2_left.set_xlim(0, 120) for ax in ax1_left, ax2_left: ax.set_xticks([0, 100]) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index b77fdb0c8ed8..ba20f325f4d7 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -845,7 +845,7 @@ def test_colorbar_change_lim_scale(): pc = ax[1].pcolormesh(np.arange(100).reshape(10, 10)+1) cb = fig.colorbar(pc, ax=ax[1], extend='both') - cb.ax.set_ylim([20, 90]) + cb.ax.set_ylim(20, 90) @check_figures_equal() diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 73f10cec52aa..8ee12131fdbe 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -199,7 +199,7 @@ def test_too_many_date_ticks(caplog): tf = datetime.datetime(2000, 1, 20) fig, ax = plt.subplots() with pytest.warns(UserWarning) as rec: - ax.set_xlim((t0, tf), auto=True) + ax.set_xlim(t0, tf, auto=True) assert len(rec) == 1 assert ('Attempting to set identical low and high xlims' in str(rec[0].message)) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 5401dd73e5ef..271fe6755a15 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -114,12 +114,12 @@ def test_imshow_zoom(fig_test, fig_ref): fig.set_size_inches(2.9, 2.9) ax = fig_test.subplots() ax.imshow(A, interpolation='auto') - ax.set_xlim([10, 20]) - ax.set_ylim([10, 20]) + ax.set_xlim(10, 20) + ax.set_ylim(10, 20) ax = fig_ref.subplots() ax.imshow(A, interpolation='nearest') - ax.set_xlim([10, 20]) - ax.set_ylim([10, 20]) + ax.set_xlim(10, 20) + ax.set_ylim(10, 20) @check_figures_equal() @@ -512,8 +512,8 @@ def test_image_edges(): interpolation='none', cmap='gray') x = y = 2 - ax.set_xlim([-x, x]) - ax.set_ylim([-y, y]) + ax.set_xlim(-x, x) + ax.set_ylim(-y, y) ax.set_xticks([]) ax.set_yticks([]) @@ -538,7 +538,7 @@ def test_image_composite_background(): ax.imshow(arr, extent=[0, 2, 15, 0]) ax.imshow(arr, extent=[4, 6, 15, 0]) ax.set_facecolor((1, 0, 0, 0.5)) - ax.set_xlim([0, 12]) + ax.set_xlim(0, 12) @image_comparison(['image_composite_alpha'], remove_text=True, tol=0.07) @@ -564,8 +564,8 @@ def test_image_composite_alpha(): ax.imshow(arr2, extent=[0, 5, 2, 3], alpha=0.6) ax.imshow(arr2, extent=[0, 5, 3, 4], alpha=0.3) ax.set_facecolor((0, 0.5, 0, 1)) - ax.set_xlim([0, 5]) - ax.set_ylim([5, 0]) + ax.set_xlim(0, 5) + ax.set_ylim(5, 0) @check_figures_equal(extensions=["pdf"]) @@ -1469,8 +1469,8 @@ def test_rgba_antialias(): axs = axs.flatten() # zoom in axs[0].imshow(aa, interpolation='nearest', cmap=cmap, vmin=-1.2, vmax=1.2) - axs[0].set_xlim([N/2-25, N/2+25]) - axs[0].set_ylim([N/2+50, N/2-10]) + axs[0].set_xlim(N/2-25, N/2+25) + axs[0].set_ylim(N/2+50, N/2-10) # no anti-alias axs[1].imshow(aa, interpolation='nearest', cmap=cmap, vmin=-1.2, vmax=1.2) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 9c708598e27c..a073fa839d50 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -518,8 +518,8 @@ def test_legend_stackplot(): y2 = 2.0 * x + 1 y3 = 3.0 * x + 2 ax.stackplot(x, y1, y2, y3, labels=['y1', 'y2', 'y3']) - ax.set_xlim((0, 10)) - ax.set_ylim((0, 70)) + ax.set_xlim(0, 10) + ax.set_ylim(0, 70) ax.legend(loc='best') diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index 56bded0c6557..88f96d1b6555 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -219,8 +219,8 @@ def test_marker_fill_styles(): markeredgecolor=color, markeredgewidth=2) - ax.set_ylim([0, 7.5]) - ax.set_xlim([-5, 155]) + ax.set_ylim(0, 7.5) + ax.set_xlim(-5, 155) def test_markerfacecolor_fillstyle(): diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 81fe062d83b2..cd693c64ee3f 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -48,8 +48,8 @@ def test_offsetbox_clipping(): da.add_artist(bg) da.add_artist(line) ax.add_artist(anchored_box) - ax.set_xlim((0, 1)) - ax.set_ylim((0, 1)) + ax.set_xlim(0, 1) + ax.set_ylim(0, 1) def test_offsetbox_clip_children(): diff --git a/lib/matplotlib/tests/test_path.py b/lib/matplotlib/tests/test_path.py index 5424160dad93..88bb5840a97f 100644 --- a/lib/matplotlib/tests/test_path.py +++ b/lib/matplotlib/tests/test_path.py @@ -155,8 +155,8 @@ def test_nonlinear_containment(): def test_arrow_contains_point(): # fix bug (#8384) fig, ax = plt.subplots() - ax.set_xlim((0, 2)) - ax.set_ylim((0, 2)) + ax.set_xlim(0, 2) + ax.set_ylim(0, 2) # create an arrow with Curve style arrow = patches.FancyArrowPatch((0.5, 0.25), (1.5, 0.75), diff --git a/lib/matplotlib/tests/test_simplification.py b/lib/matplotlib/tests/test_simplification.py index bc9b46b14db2..41d01addd622 100644 --- a/lib/matplotlib/tests/test_simplification.py +++ b/lib/matplotlib/tests/test_simplification.py @@ -25,7 +25,7 @@ def test_clipping(): fig, ax = plt.subplots() ax.plot(t, s, linewidth=1.0) - ax.set_ylim((-0.20, -0.28)) + ax.set_ylim(-0.20, -0.28) @image_comparison(['overflow'], remove_text=True, @@ -244,8 +244,8 @@ def test_simplify_curve(): fig, ax = plt.subplots() ax.add_patch(pp1) - ax.set_xlim((0, 2)) - ax.set_ylim((0, 2)) + ax.set_xlim(0, 2) + ax.set_ylim(0, 2) @check_figures_equal(extensions=['png', 'pdf', 'svg']) @@ -401,8 +401,8 @@ def test_closed_path_clipping(fig_test, fig_ref): def test_hatch(): fig, ax = plt.subplots() ax.add_patch(plt.Rectangle((0, 0), 1, 1, fill=False, hatch="/")) - ax.set_xlim((0.45, 0.55)) - ax.set_ylim((0.45, 0.55)) + ax.set_xlim(0.45, 0.55) + ax.set_ylim(0.45, 0.55) @image_comparison(['fft_peaks'], remove_text=True) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 79a9e2d66c46..7327ed55ed04 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -144,8 +144,8 @@ def test_multiline2(): fig, ax = plt.subplots() - ax.set_xlim([0, 1.4]) - ax.set_ylim([0, 2]) + ax.set_xlim(0, 1.4) + ax.set_ylim(0, 2) ax.axhline(0.5, color='C2', linewidth=0.3) sts = ['Line', '2 Lineg\n 2 Lg', '$\\sum_i x $', 'hi $\\sum_i x $\ntest', 'test\n $\\sum_i x $', '$\\sum_i x $\n $\\sum_i x $'] diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index f6b6d8f644cc..98fd5e70cdb9 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -331,8 +331,8 @@ def test_collapsed(): # zero (i.e. margins add up to more than the available width) that a call # to tight_layout will not get applied: fig, ax = plt.subplots(tight_layout=True) - ax.set_xlim([0, 1]) - ax.set_ylim([0, 1]) + ax.set_xlim(0, 1) + ax.set_ylim(0, 1) ax.annotate('BIG LONG STRING', xy=(1.25, 2), xytext=(10.5, 1.75), annotation_clip=False) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index e62379470db1..9ded7c61ce2d 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -273,10 +273,10 @@ def __init__(self, ax, orientation, closedmin, closedmax, self.valfmt = valfmt if orientation == "vertical": - ax.set_ylim((valmin, valmax)) + ax.set_ylim(valmin, valmax) axis = ax.yaxis else: - ax.set_xlim((valmin, valmax)) + ax.set_xlim(valmin, valmax) axis = ax.xaxis self._fmt = axis.get_major_formatter() diff --git a/lib/mpl_toolkits/axisartist/tests/test_axislines.py b/lib/mpl_toolkits/axisartist/tests/test_axislines.py index 8bc3707421b6..a1485d4f436b 100644 --- a/lib/mpl_toolkits/axisartist/tests/test_axislines.py +++ b/lib/mpl_toolkits/axisartist/tests/test_axislines.py @@ -83,8 +83,8 @@ def test_ParasiteAxesAuxTrans(): getattr(ax2, name)(xx, yy, data[:-1, :-1]) else: getattr(ax2, name)(xx, yy, data) - ax1.set_xlim((0, 5)) - ax1.set_ylim((0, 5)) + ax1.set_xlim(0, 5) + ax1.set_ylim(0, 5) ax2.contour(xx, yy, data, colors='k') diff --git a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py index 79c7baba9bd1..cd45c8e33a6f 100644 --- a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py +++ b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py @@ -2227,9 +2227,9 @@ def test_computed_zorder(): # plot some points ax.scatter((3, 3), (1, 3), (1, 3), c='red', zorder=10) - ax.set_xlim((0, 5.0)) - ax.set_ylim((0, 5.0)) - ax.set_zlim((0, 2.5)) + ax.set_xlim(0, 5.0) + ax.set_ylim(0, 5.0) + ax.set_zlim(0, 2.5) ax3 = fig.add_subplot(223, projection='3d') ax4 = fig.add_subplot(224, projection='3d') From edc8b20f3f05a1b16f2145ebc8e73ad9967ae4c1 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 16 Apr 2025 19:47:54 -0400 Subject: [PATCH 03/50] Ensure tuples are passed to Axes.set([xyz]lim=...) --- galleries/examples/mplot3d/box3d.py | 2 +- .../examples/specialty_plots/leftventricle_bullseye.py | 2 +- galleries/tutorials/lifecycle.py | 8 ++++---- galleries/users_explain/animations/animations.py | 2 +- lib/matplotlib/tests/test_usetex.py | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/galleries/examples/mplot3d/box3d.py b/galleries/examples/mplot3d/box3d.py index 807e3d496ec6..4d75c8bc2809 100644 --- a/galleries/examples/mplot3d/box3d.py +++ b/galleries/examples/mplot3d/box3d.py @@ -51,7 +51,7 @@ xmin, xmax = X.min(), X.max() ymin, ymax = Y.min(), Y.max() zmin, zmax = Z.min(), Z.max() -ax.set(xlim=[xmin, xmax], ylim=[ymin, ymax], zlim=[zmin, zmax]) +ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax), zlim=(zmin, zmax)) # Plot edges edges_kw = dict(color='0.4', linewidth=1, zorder=1e3) diff --git a/galleries/examples/specialty_plots/leftventricle_bullseye.py b/galleries/examples/specialty_plots/leftventricle_bullseye.py index 3ad02edbc630..285fcdaecc5e 100644 --- a/galleries/examples/specialty_plots/leftventricle_bullseye.py +++ b/galleries/examples/specialty_plots/leftventricle_bullseye.py @@ -55,7 +55,7 @@ def bullseye_plot(ax, data, seg_bold=None, cmap="viridis", norm=None): r = np.linspace(0.2, 1, 4) - ax.set(ylim=[0, 1], xticklabels=[], yticklabels=[]) + ax.set(ylim=(0, 1), xticklabels=[], yticklabels=[]) ax.grid(False) # Remove grid # Fill segments 1-6, 7-12, 13-16. diff --git a/galleries/tutorials/lifecycle.py b/galleries/tutorials/lifecycle.py index 4aae4d6c1dbc..4c009f802cf4 100644 --- a/galleries/tutorials/lifecycle.py +++ b/galleries/tutorials/lifecycle.py @@ -169,7 +169,7 @@ ax.barh(group_names, group_data) labels = ax.get_xticklabels() plt.setp(labels, rotation=45, horizontalalignment='right') -ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', +ax.set(xlim=(-10000, 140000), xlabel='Total Revenue', ylabel='Company', title='Company Revenue') # %% @@ -187,7 +187,7 @@ ax.barh(group_names, group_data) labels = ax.get_xticklabels() plt.setp(labels, rotation=45, horizontalalignment='right') -ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', +ax.set(xlim=(-10000, 140000), xlabel='Total Revenue', ylabel='Company', title='Company Revenue') # %% @@ -220,7 +220,7 @@ def currency(x, pos): labels = ax.get_xticklabels() plt.setp(labels, rotation=45, horizontalalignment='right') -ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', +ax.set(xlim=(-10000, 140000), xlabel='Total Revenue', ylabel='Company', title='Company Revenue') ax.xaxis.set_major_formatter(currency) @@ -248,7 +248,7 @@ def currency(x, pos): # Now we move our title up since it's getting a little cramped ax.title.set(y=1.05) -ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', +ax.set(xlim=(-10000, 140000), xlabel='Total Revenue', ylabel='Company', title='Company Revenue') ax.xaxis.set_major_formatter(currency) ax.set_xticks([0, 25e3, 50e3, 75e3, 100e3, 125e3]) diff --git a/galleries/users_explain/animations/animations.py b/galleries/users_explain/animations/animations.py index a0669956ab81..dca49fc5228e 100644 --- a/galleries/users_explain/animations/animations.py +++ b/galleries/users_explain/animations/animations.py @@ -111,7 +111,7 @@ scat = ax.scatter(t[0], z[0], c="b", s=5, label=f'v0 = {v0} m/s') line2 = ax.plot(t[0], z2[0], label=f'v0 = {v02} m/s')[0] -ax.set(xlim=[0, 3], ylim=[-4, 10], xlabel='Time [s]', ylabel='Z [m]') +ax.set(xlim=(0, 3), ylim=(-4, 10), xlabel='Time [s]', ylabel='Z [m]') ax.legend() diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index c92beed7f2c6..1d640b4adf7c 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -169,7 +169,7 @@ def test_rotation(): fig = plt.figure() ax = fig.add_axes((0, 0, 1, 1)) - ax.set(xlim=[-0.5, 5], xticks=[], ylim=[-0.5, 3], yticks=[], frame_on=False) + ax.set(xlim=(-0.5, 5), xticks=[], ylim=(-0.5, 3), yticks=[], frame_on=False) text = {val: val[0] for val in ['top', 'center', 'bottom', 'left', 'right']} text['baseline'] = 'B' From 31d92dc41c381eb331bdddc18df94e89f21a9c03 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 11 Jun 2025 14:10:40 +0200 Subject: [PATCH 04/50] Prepare to turn matplotlib.style into a plain module. Having matplotlib.style be a package with the entire implementation in matplotlib.style.core is a bit overkill, and also makes it slightly awkward that USER_LIBRARY_PATHS is effectively a public API (clearly intended as so, per the comment, even though we may do it differently nowadays...) but only available in matplotlib.style.core, whereas everything else is re-exported by matplotlib.style. Prepare to flatten the implementation by deprecating matplotlib.style.core and reexporting USER_LIBRARY_PATHS in matplotlib.style. Once the deprecation elapses, we'll be able to move the implementation into a plain matplotlib/style.py module. --- .../deprecations/30163-AL.rst | 9 + lib/matplotlib/style/__init__.py | 252 +++++++++++++++++- lib/matplotlib/style/__init__.pyi | 20 ++ lib/matplotlib/style/core.py | 232 +--------------- lib/matplotlib/style/core.pyi | 7 +- lib/matplotlib/style/meson.build | 1 + lib/matplotlib/tests/test_style.py | 7 +- 7 files changed, 300 insertions(+), 228 deletions(-) create mode 100644 doc/api/next_api_changes/deprecations/30163-AL.rst create mode 100644 lib/matplotlib/style/__init__.pyi diff --git a/doc/api/next_api_changes/deprecations/30163-AL.rst b/doc/api/next_api_changes/deprecations/30163-AL.rst new file mode 100644 index 000000000000..15d0077375f2 --- /dev/null +++ b/doc/api/next_api_changes/deprecations/30163-AL.rst @@ -0,0 +1,9 @@ +``matplotlib.style.core`` +~~~~~~~~~~~~~~~~~~~~~~~~~ +The ``matplotlib.style.core`` module is deprecated. All APIs intended for +public use are now available in `matplotlib.style` directly (including +``USER_LIBRARY_PATHS``, which was previously not reexported). + +The following APIs of ``matplotlib.style.core`` have been deprecated with no +replacement: ``BASE_LIBRARY_PATH``, ``STYLE_EXTENSION``, ``STYLE_BLACKLIST``, +``update_user_library``, ``read_style_directory``, ``update_nested_dict``. diff --git a/lib/matplotlib/style/__init__.py b/lib/matplotlib/style/__init__.py index 488c6d6ae1ec..a202cfe08b20 100644 --- a/lib/matplotlib/style/__init__.py +++ b/lib/matplotlib/style/__init__.py @@ -1,4 +1,252 @@ -from .core import available, context, library, reload_library, use +""" +Core functions and attributes for the matplotlib style library: +``use`` + Select style sheet to override the current matplotlib settings. +``context`` + Context manager to use a style sheet temporarily. +``available`` + List available style sheets. +``library`` + A dictionary of style names and matplotlib settings. +""" -__all__ = ["available", "context", "library", "reload_library", "use"] +import contextlib +import importlib.resources +import logging +import os +from pathlib import Path +import warnings + +import matplotlib as mpl +from matplotlib import _api, _docstring, rc_params_from_file, rcParamsDefault + +_log = logging.getLogger(__name__) + +__all__ = ['use', 'context', 'available', 'library', 'reload_library'] + + +_BASE_LIBRARY_PATH = os.path.join(mpl.get_data_path(), 'stylelib') +# Users may want multiple library paths, so store a list of paths. +USER_LIBRARY_PATHS = [os.path.join(mpl.get_configdir(), 'stylelib')] +_STYLE_EXTENSION = 'mplstyle' +# A list of rcParams that should not be applied from styles +_STYLE_BLACKLIST = { + 'interactive', 'backend', 'webagg.port', 'webagg.address', + 'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback', + 'toolbar', 'timezone', 'figure.max_open_warning', + 'figure.raise_window', 'savefig.directory', 'tk.window_focus', + 'docstring.hardcopy', 'date.epoch'} + + +@_docstring.Substitution( + "\n".join(map("- {}".format, sorted(_STYLE_BLACKLIST, key=str.lower))) +) +def use(style): + """ + Use Matplotlib style settings from a style specification. + + The style name of 'default' is reserved for reverting back to + the default style settings. + + .. note:: + + This updates the `.rcParams` with the settings from the style. + `.rcParams` not defined in the style are kept. + + Parameters + ---------- + style : str, dict, Path or list + + A style specification. Valid options are: + + str + - One of the style names in `.style.available` (a builtin style or + a style installed in the user library path). + + - A dotted name of the form "package.style_name"; in that case, + "package" should be an importable Python package name, e.g. at + ``/path/to/package/__init__.py``; the loaded style file is + ``/path/to/package/style_name.mplstyle``. (Style files in + subpackages are likewise supported.) + + - The path or URL to a style file, which gets loaded by + `.rc_params_from_file`. + + dict + A mapping of key/value pairs for `matplotlib.rcParams`. + + Path + The path to a style file, which gets loaded by + `.rc_params_from_file`. + + list + A list of style specifiers (str, Path or dict), which are applied + from first to last in the list. + + Notes + ----- + The following `.rcParams` are not related to style and will be ignored if + found in a style specification: + + %s + """ + if isinstance(style, (str, Path)) or hasattr(style, 'keys'): + # If name is a single str, Path or dict, make it a single element list. + styles = [style] + else: + styles = style + + style_alias = {'mpl20': 'default', 'mpl15': 'classic'} + + for style in styles: + if isinstance(style, str): + style = style_alias.get(style, style) + if style == "default": + # Deprecation warnings were already handled when creating + # rcParamsDefault, no need to reemit them here. + with _api.suppress_matplotlib_deprecation_warning(): + # don't trigger RcParams.__getitem__('backend') + style = {k: rcParamsDefault[k] for k in rcParamsDefault + if k not in _STYLE_BLACKLIST} + elif style in library: + style = library[style] + elif "." in style: + pkg, _, name = style.rpartition(".") + try: + path = importlib.resources.files(pkg) / f"{name}.{_STYLE_EXTENSION}" + style = rc_params_from_file(path, use_default_template=False) + except (ModuleNotFoundError, OSError, TypeError) as exc: + # There is an ambiguity whether a dotted name refers to a + # package.style_name or to a dotted file path. Currently, + # we silently try the first form and then the second one; + # in the future, we may consider forcing file paths to + # either use Path objects or be prepended with "./" and use + # the slash as marker for file paths. + pass + if isinstance(style, (str, Path)): + try: + style = rc_params_from_file(style, use_default_template=False) + except OSError as err: + raise OSError( + f"{style!r} is not a valid package style, path of style " + f"file, URL of style file, or library style name (library " + f"styles are listed in `style.available`)") from err + filtered = {} + for k in style: # don't trigger RcParams.__getitem__('backend') + if k in _STYLE_BLACKLIST: + _api.warn_external( + f"Style includes a parameter, {k!r}, that is not " + f"related to style. Ignoring this parameter.") + else: + filtered[k] = style[k] + mpl.rcParams.update(filtered) + + +@contextlib.contextmanager +def context(style, after_reset=False): + """ + Context manager for using style settings temporarily. + + Parameters + ---------- + style : str, dict, Path or list + A style specification. Valid options are: + + str + - One of the style names in `.style.available` (a builtin style or + a style installed in the user library path). + + - A dotted name of the form "package.style_name"; in that case, + "package" should be an importable Python package name, e.g. at + ``/path/to/package/__init__.py``; the loaded style file is + ``/path/to/package/style_name.mplstyle``. (Style files in + subpackages are likewise supported.) + + - The path or URL to a style file, which gets loaded by + `.rc_params_from_file`. + dict + A mapping of key/value pairs for `matplotlib.rcParams`. + + Path + The path to a style file, which gets loaded by + `.rc_params_from_file`. + + list + A list of style specifiers (str, Path or dict), which are applied + from first to last in the list. + + after_reset : bool + If True, apply style after resetting settings to their defaults; + otherwise, apply style on top of the current settings. + """ + with mpl.rc_context(): + if after_reset: + mpl.rcdefaults() + use(style) + yield + + +def _update_user_library(library): + """Update style library with user-defined rc files.""" + for stylelib_path in map(os.path.expanduser, USER_LIBRARY_PATHS): + styles = _read_style_directory(stylelib_path) + _update_nested_dict(library, styles) + return library + + +@_api.deprecated("3.11") +def update_user_library(library): + return _update_user_library(library) + + +def _read_style_directory(style_dir): + """Return dictionary of styles defined in *style_dir*.""" + styles = dict() + for path in Path(style_dir).glob(f"*.{_STYLE_EXTENSION}"): + with warnings.catch_warnings(record=True) as warns: + styles[path.stem] = rc_params_from_file(path, use_default_template=False) + for w in warns: + _log.warning('In %s: %s', path, w.message) + return styles + + +@_api.deprecated("3.11") +def read_style_directory(style_dir): + return _read_style_directory(style_dir) + + +def _update_nested_dict(main_dict, new_dict): + """ + Update nested dict (only level of nesting) with new values. + + Unlike `dict.update`, this assumes that the values of the parent dict are + dicts (or dict-like), so you shouldn't replace the nested dict if it + already exists. Instead you should update the sub-dict. + """ + # update named styles specified by user + for name, rc_dict in new_dict.items(): + main_dict.setdefault(name, {}).update(rc_dict) + return main_dict + + +@_api.deprecated("3.11") +def update_nested_dict(main_dict, new_dict): + return _update_nested_dict(main_dict, new_dict) + + +# Load style library +# ================== +_base_library = _read_style_directory(_BASE_LIBRARY_PATH) +library = {} +available = [] + + +def reload_library(): + """Reload the style library.""" + library.clear() + library.update(_update_user_library(_base_library)) + available[:] = sorted(library.keys()) + + +reload_library() diff --git a/lib/matplotlib/style/__init__.pyi b/lib/matplotlib/style/__init__.pyi new file mode 100644 index 000000000000..c93b504fe6bd --- /dev/null +++ b/lib/matplotlib/style/__init__.pyi @@ -0,0 +1,20 @@ +from collections.abc import Generator +import contextlib + +from matplotlib import RcParams +from matplotlib.typing import RcStyleType + +USER_LIBRARY_PATHS: list[str] = ... + +def use(style: RcStyleType) -> None: ... +@contextlib.contextmanager +def context( + style: RcStyleType, after_reset: bool = ... +) -> Generator[None, None, None]: ... + +library: dict[str, RcParams] +available: list[str] + +def reload_library() -> None: ... + +__all__ = ['use', 'context', 'available', 'library', 'reload_library'] diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index e36c3c37a882..c377bc64077a 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -11,227 +11,17 @@ A dictionary of style names and matplotlib settings. """ -import contextlib -import importlib.resources -import logging -import os -from pathlib import Path -import warnings - -import matplotlib as mpl -from matplotlib import _api, _docstring, _rc_params_in_file, rcParamsDefault - -_log = logging.getLogger(__name__) - -__all__ = ['use', 'context', 'available', 'library', 'reload_library'] - - -BASE_LIBRARY_PATH = os.path.join(mpl.get_data_path(), 'stylelib') -# Users may want multiple library paths, so store a list of paths. -USER_LIBRARY_PATHS = [os.path.join(mpl.get_configdir(), 'stylelib')] -STYLE_EXTENSION = 'mplstyle' -# A list of rcParams that should not be applied from styles -STYLE_BLACKLIST = { - 'interactive', 'backend', 'webagg.port', 'webagg.address', - 'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback', - 'toolbar', 'timezone', 'figure.max_open_warning', - 'figure.raise_window', 'savefig.directory', 'tk.window_focus', - 'docstring.hardcopy', 'date.epoch'} - - -@_docstring.Substitution( - "\n".join(map("- {}".format, sorted(STYLE_BLACKLIST, key=str.lower))) +from .. import _api +from . import ( + use, context, available, library, reload_library, USER_LIBRARY_PATHS, + _BASE_LIBRARY_PATH as BASE_LIBRARY_PATH, + _STYLE_EXTENSION as STYLE_EXTENSION, + _STYLE_BLACKLIST as STYLE_BLACKLIST, ) -def use(style): - """ - Use Matplotlib style settings from a style specification. - - The style name of 'default' is reserved for reverting back to - the default style settings. - - .. note:: - - This updates the `.rcParams` with the settings from the style. - `.rcParams` not defined in the style are kept. - - Parameters - ---------- - style : str, dict, Path or list - - A style specification. Valid options are: - - str - - One of the style names in `.style.available` (a builtin style or - a style installed in the user library path). - - - A dotted name of the form "package.style_name"; in that case, - "package" should be an importable Python package name, e.g. at - ``/path/to/package/__init__.py``; the loaded style file is - ``/path/to/package/style_name.mplstyle``. (Style files in - subpackages are likewise supported.) - - - The path or URL to a style file, which gets loaded by - `.rc_params_from_file`. - - dict - A mapping of key/value pairs for `matplotlib.rcParams`. - - Path - The path to a style file, which gets loaded by - `.rc_params_from_file`. - - list - A list of style specifiers (str, Path or dict), which are applied - from first to last in the list. - - Notes - ----- - The following `.rcParams` are not related to style and will be ignored if - found in a style specification: - - %s - """ - if isinstance(style, (str, Path)) or hasattr(style, 'keys'): - # If name is a single str, Path or dict, make it a single element list. - styles = [style] - else: - styles = style - - style_alias = {'mpl20': 'default', 'mpl15': 'classic'} - - for style in styles: - if isinstance(style, str): - style = style_alias.get(style, style) - if style == "default": - # Deprecation warnings were already handled when creating - # rcParamsDefault, no need to reemit them here. - with _api.suppress_matplotlib_deprecation_warning(): - # don't trigger RcParams.__getitem__('backend') - style = {k: rcParamsDefault[k] for k in rcParamsDefault - if k not in STYLE_BLACKLIST} - elif style in library: - style = library[style] - elif "." in style: - pkg, _, name = style.rpartition(".") - try: - path = importlib.resources.files(pkg) / f"{name}.{STYLE_EXTENSION}" - style = _rc_params_in_file(path) - except (ModuleNotFoundError, OSError, TypeError) as exc: - # There is an ambiguity whether a dotted name refers to a - # package.style_name or to a dotted file path. Currently, - # we silently try the first form and then the second one; - # in the future, we may consider forcing file paths to - # either use Path objects or be prepended with "./" and use - # the slash as marker for file paths. - pass - if isinstance(style, (str, Path)): - try: - style = _rc_params_in_file(style) - except OSError as err: - raise OSError( - f"{style!r} is not a valid package style, path of style " - f"file, URL of style file, or library style name (library " - f"styles are listed in `style.available`)") from err - filtered = {} - for k in style: # don't trigger RcParams.__getitem__('backend') - if k in STYLE_BLACKLIST: - _api.warn_external( - f"Style includes a parameter, {k!r}, that is not " - f"related to style. Ignoring this parameter.") - else: - filtered[k] = style[k] - mpl.rcParams.update(filtered) - - -@contextlib.contextmanager -def context(style, after_reset=False): - """ - Context manager for using style settings temporarily. - - Parameters - ---------- - style : str, dict, Path or list - A style specification. Valid options are: - - str - - One of the style names in `.style.available` (a builtin style or - a style installed in the user library path). - - - A dotted name of the form "package.style_name"; in that case, - "package" should be an importable Python package name, e.g. at - ``/path/to/package/__init__.py``; the loaded style file is - ``/path/to/package/style_name.mplstyle``. (Style files in - subpackages are likewise supported.) - - - The path or URL to a style file, which gets loaded by - `.rc_params_from_file`. - dict - A mapping of key/value pairs for `matplotlib.rcParams`. - - Path - The path to a style file, which gets loaded by - `.rc_params_from_file`. - - list - A list of style specifiers (str, Path or dict), which are applied - from first to last in the list. - - after_reset : bool - If True, apply style after resetting settings to their defaults; - otherwise, apply style on top of the current settings. - """ - with mpl.rc_context(): - if after_reset: - mpl.rcdefaults() - use(style) - yield - - -def update_user_library(library): - """Update style library with user-defined rc files.""" - for stylelib_path in map(os.path.expanduser, USER_LIBRARY_PATHS): - styles = read_style_directory(stylelib_path) - update_nested_dict(library, styles) - return library - - -def read_style_directory(style_dir): - """Return dictionary of styles defined in *style_dir*.""" - styles = dict() - for path in Path(style_dir).glob(f"*.{STYLE_EXTENSION}"): - with warnings.catch_warnings(record=True) as warns: - styles[path.stem] = _rc_params_in_file(path) - for w in warns: - _log.warning('In %s: %s', path, w.message) - return styles - - -def update_nested_dict(main_dict, new_dict): - """ - Update nested dict (only level of nesting) with new values. - - Unlike `dict.update`, this assumes that the values of the parent dict are - dicts (or dict-like), so you shouldn't replace the nested dict if it - already exists. Instead you should update the sub-dict. - """ - # update named styles specified by user - for name, rc_dict in new_dict.items(): - main_dict.setdefault(name, {}).update(rc_dict) - return main_dict - - -# Load style library -# ================== -_base_library = read_style_directory(BASE_LIBRARY_PATH) -library = {} -available = [] - - -def reload_library(): - """Reload the style library.""" - library.clear() - library.update(update_user_library(_base_library)) - available[:] = sorted(library.keys()) +__all__ = [ + "use", "context", "available", "library", "reload_library", + "USER_LIBRARY_PATHS", "BASE_LIBRARY_PATH", "STYLE_EXTENSION", "STYLE_BLACKLIST", +] -reload_library() +_api.warn_deprecated("3.11", name=__name__, obj_type="module") diff --git a/lib/matplotlib/style/core.pyi b/lib/matplotlib/style/core.pyi index 5734b017f7c4..ee21d2f41ef5 100644 --- a/lib/matplotlib/style/core.pyi +++ b/lib/matplotlib/style/core.pyi @@ -5,7 +5,9 @@ from matplotlib import RcParams from matplotlib.typing import RcStyleType USER_LIBRARY_PATHS: list[str] = ... +BASE_LIBRARY_PATH: str = ... STYLE_EXTENSION: str = ... +STYLE_BLACKLIST: set[str] = ... def use(style: RcStyleType) -> None: ... @contextlib.contextmanager @@ -18,4 +20,7 @@ available: list[str] def reload_library() -> None: ... -__all__ = ['use', 'context', 'available', 'library', 'reload_library'] +__all__ = [ + "use", "context", "available", "library", "reload_library", + "USER_LIBRARY_PATHS", "BASE_LIBRARY_PATH", "STYLE_EXTENSION", "STYLE_BLACKLIST", +] diff --git a/lib/matplotlib/style/meson.build b/lib/matplotlib/style/meson.build index 03e7972132bb..e7a183c8581c 100644 --- a/lib/matplotlib/style/meson.build +++ b/lib/matplotlib/style/meson.build @@ -4,6 +4,7 @@ python_sources = [ ] typing_sources = [ + '__init__.pyi', 'core.pyi', ] diff --git a/lib/matplotlib/tests/test_style.py b/lib/matplotlib/tests/test_style.py index be038965e33d..4d76a4ecfa8b 100644 --- a/lib/matplotlib/tests/test_style.py +++ b/lib/matplotlib/tests/test_style.py @@ -8,7 +8,6 @@ import matplotlib as mpl from matplotlib import pyplot as plt, style -from matplotlib.style.core import USER_LIBRARY_PATHS, STYLE_EXTENSION PARAM = 'image.cmap' @@ -21,7 +20,7 @@ def temp_style(style_name, settings=None): """Context manager to create a style sheet in a temporary directory.""" if not settings: settings = DUMMY_SETTINGS - temp_file = f'{style_name}.{STYLE_EXTENSION}' + temp_file = f'{style_name}.mplstyle' try: with TemporaryDirectory() as tmpdir: # Write style settings to file in the tmpdir. @@ -29,7 +28,7 @@ def temp_style(style_name, settings=None): "\n".join(f"{k}: {v}" for k, v in settings.items()), encoding="utf-8") # Add tmpdir to style path and reload so we can access this style. - USER_LIBRARY_PATHS.append(tmpdir) + style.USER_LIBRARY_PATHS.append(tmpdir) style.reload_library() yield finally: @@ -71,7 +70,7 @@ def test_use_url(tmp_path): def test_single_path(tmp_path): mpl.rcParams[PARAM] = 'gray' - path = tmp_path / f'text.{STYLE_EXTENSION}' + path = tmp_path / 'text.mplstyle' path.write_text(f'{PARAM} : {VALUE}', encoding='utf-8') with style.context(path): assert mpl.rcParams[PARAM] == VALUE From f5851dad829961c392cf22901412a6eeccc9a45f Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 19 Jun 2025 20:09:47 -0400 Subject: [PATCH 05/50] Simplify RendererAgg::draw_markers buffers Instead of a fixed-size buffer, a dynamic buffer, and all the tracking for them, just use a standard C++ container, which will clean up for itself automatically. --- src/_backend_agg.h | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/src/_backend_agg.h b/src/_backend_agg.h index 6ecbcba1df18..6eb54e485e86 100644 --- a/src/_backend_agg.h +++ b/src/_backend_agg.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "agg_alpha_mask_u8.h" #include "agg_conv_curve.h" @@ -102,8 +103,6 @@ class BufferRegion int stride; }; -#define MARKER_CACHE_SIZE 512 - // the renderer class RendererAgg { @@ -539,22 +538,14 @@ inline void RendererAgg::draw_markers(GCAgg &gc, rendererBase.reset_clipping(true); agg::rect_i marker_size(0x7FFFFFFF, 0x7FFFFFFF, -0x7FFFFFFF, -0x7FFFFFFF); - agg::int8u staticFillCache[MARKER_CACHE_SIZE]; - agg::int8u staticStrokeCache[MARKER_CACHE_SIZE]; - agg::int8u *fillCache = staticFillCache; - agg::int8u *strokeCache = staticStrokeCache; - try { - unsigned fillSize = 0; + std::vector fillBuffer; if (face.first) { theRasterizer.add_path(marker_path_curve); agg::render_scanlines(theRasterizer, slineP8, scanlines); - fillSize = scanlines.byte_size(); - if (fillSize >= MARKER_CACHE_SIZE) { - fillCache = new agg::int8u[fillSize]; - } - scanlines.serialize(fillCache); + fillBuffer.resize(scanlines.byte_size()); + scanlines.serialize(fillBuffer.data()); marker_size = agg::rect_i(scanlines.min_x(), scanlines.min_y(), scanlines.max_x(), @@ -569,11 +560,8 @@ inline void RendererAgg::draw_markers(GCAgg &gc, theRasterizer.reset(); theRasterizer.add_path(stroke); agg::render_scanlines(theRasterizer, slineP8, scanlines); - unsigned strokeSize = scanlines.byte_size(); - if (strokeSize >= MARKER_CACHE_SIZE) { - strokeCache = new agg::int8u[strokeSize]; - } - scanlines.serialize(strokeCache); + std::vector strokeBuffer(scanlines.byte_size()); + scanlines.serialize(strokeBuffer.data()); marker_size = agg::rect_i(std::min(marker_size.x1, scanlines.min_x()), std::min(marker_size.y1, scanlines.min_y()), std::max(marker_size.x2, scanlines.max_x()), @@ -619,11 +607,11 @@ inline void RendererAgg::draw_markers(GCAgg &gc, if (face.first) { ren.color(face.second); - sa.init(fillCache, fillSize, x, y); + sa.init(fillBuffer.data(), fillBuffer.size(), x, y); agg::render_scanlines(sa, sl, ren); } ren.color(gc.color); - sa.init(strokeCache, strokeSize, x, y); + sa.init(strokeBuffer.data(), strokeBuffer.size(), x, y); agg::render_scanlines(sa, sl, ren); } } else { @@ -647,32 +635,23 @@ inline void RendererAgg::draw_markers(GCAgg &gc, if (face.first) { rendererAA.color(face.second); - sa.init(fillCache, fillSize, x, y); + sa.init(fillBuffer.data(), fillBuffer.size(), x, y); agg::render_scanlines(sa, sl, rendererAA); } rendererAA.color(gc.color); - sa.init(strokeCache, strokeSize, x, y); + sa.init(strokeBuffer.data(), strokeBuffer.size(), x, y); agg::render_scanlines(sa, sl, rendererAA); } } } catch (...) { - if (fillCache != staticFillCache) - delete[] fillCache; - if (strokeCache != staticStrokeCache) - delete[] strokeCache; theRasterizer.reset_clipping(); rendererBase.reset_clipping(true); throw; } - if (fillCache != staticFillCache) - delete[] fillCache; - if (strokeCache != staticStrokeCache) - delete[] strokeCache; - theRasterizer.reset_clipping(); rendererBase.reset_clipping(true); } From b154237b4b3bbc5712f01807d58e3b8011eb9cb9 Mon Sep 17 00:00:00 2001 From: "Albert Y. Shih" Date: Thu, 19 Jun 2025 14:13:10 -0400 Subject: [PATCH 06/50] Clean up image files that may be symbolic links to files --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2a48276707ce..f599e55ab03e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -346,7 +346,7 @@ jobs: run: | function remove_files() { local extension=$1 - find ./result_images -type f -name "*-expected*.$extension" | while read file; do + find ./result_images -name "*-expected*.$extension" | while read file; do if [[ $file == *"-expected_pdf"* ]]; then base=${file%-expected_pdf.$extension}_pdf elif [[ $file == *"-expected_eps"* ]]; then From 8c75238a3172e2260d58f72e7b26a06263d2b90d Mon Sep 17 00:00:00 2001 From: "Albert Y. Shih" Date: Thu, 19 Jun 2025 14:55:47 -0400 Subject: [PATCH 07/50] Clean up GIF image files from passing tests --- .github/workflows/tests.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f599e55ab03e..85ace93445b6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -353,6 +353,8 @@ jobs: base=${file%-expected_eps.$extension}_eps elif [[ $file == *"-expected_svg"* ]]; then base=${file%-expected_svg.$extension}_svg + elif [[ $file == *"-expected_gif"* ]]; then + base=${file%-expected_gif.$extension}_gif else base=${file%-expected.$extension} fi @@ -369,7 +371,7 @@ jobs: done } - remove_files "png"; remove_files "svg"; remove_files "pdf"; remove_files "eps"; + remove_files "png"; remove_files "svg"; remove_files "pdf"; remove_files "eps"; remove_files "gif"; if [ "$(find ./result_images -mindepth 1 -type d)" ]; then find ./result_images/* -type d -empty -delete From fc99473e8da7c30ed9c6cedaa704772563257dcd Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 20 Jun 2025 22:57:27 -0400 Subject: [PATCH 08/50] DOC: Remove last userdemo example Nested gridspecs are explained in `galleries/examples/subplots_axes_and_figures/gridspec_nested.py`, and layout is more generally explained in `galleries/users_explain/axes/arranging_axes.py` and `galleries/users_explain/axes/constrainedlayout_guide.py` Closes #25800 --- .../gridspec_nested.py | 2 + galleries/examples/userdemo/README.txt | 4 -- .../examples/userdemo/demo_gridspec06.py | 38 ------------------- galleries/users_explain/axes/legend_guide.py | 4 +- pyproject.toml | 1 - 5 files changed, 4 insertions(+), 45 deletions(-) delete mode 100644 galleries/examples/userdemo/README.txt delete mode 100644 galleries/examples/userdemo/demo_gridspec06.py diff --git a/galleries/examples/subplots_axes_and_figures/gridspec_nested.py b/galleries/examples/subplots_axes_and_figures/gridspec_nested.py index 025bdb1185a7..789cc0ae6b5b 100644 --- a/galleries/examples/subplots_axes_and_figures/gridspec_nested.py +++ b/galleries/examples/subplots_axes_and_figures/gridspec_nested.py @@ -1,4 +1,6 @@ """ +.. redirect-from:: /gallery/userdemo/demo_gridspec06 + ================ Nested Gridspecs ================ diff --git a/galleries/examples/userdemo/README.txt b/galleries/examples/userdemo/README.txt deleted file mode 100644 index 7be351dc70dd..000000000000 --- a/galleries/examples/userdemo/README.txt +++ /dev/null @@ -1,4 +0,0 @@ -.. _userdemo: - -Userdemo -======== diff --git a/galleries/examples/userdemo/demo_gridspec06.py b/galleries/examples/userdemo/demo_gridspec06.py deleted file mode 100644 index c42224ce1e7b..000000000000 --- a/galleries/examples/userdemo/demo_gridspec06.py +++ /dev/null @@ -1,38 +0,0 @@ -r""" -================ -Nested GridSpecs -================ - -This example demonstrates the use of nested `.GridSpec`\s. -""" - -import matplotlib.pyplot as plt -import numpy as np - - -def squiggle_xy(a, b, c, d): - i = np.arange(0.0, 2*np.pi, 0.05) - return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d) - - -fig = plt.figure(figsize=(8, 8)) -outer_grid = fig.add_gridspec(4, 4, wspace=0, hspace=0) - -for a in range(4): - for b in range(4): - # gridspec inside gridspec - inner_grid = outer_grid[a, b].subgridspec(3, 3, wspace=0, hspace=0) - axs = inner_grid.subplots() # Create all subplots for the inner grid. - for (c, d), ax in np.ndenumerate(axs): - ax.plot(*squiggle_xy(a + 1, b + 1, c + 1, d + 1)) - ax.set(xticks=[], yticks=[]) - -# show only the outside spines -for ax in fig.get_axes(): - ss = ax.get_subplotspec() - ax.spines.top.set_visible(ss.is_first_row()) - ax.spines.bottom.set_visible(ss.is_last_row()) - ax.spines.left.set_visible(ss.is_first_col()) - ax.spines.right.set_visible(ss.is_last_col()) - -plt.show() diff --git a/galleries/users_explain/axes/legend_guide.py b/galleries/users_explain/axes/legend_guide.py index 5da3ceafe387..ec0468fe172d 100644 --- a/galleries/users_explain/axes/legend_guide.py +++ b/galleries/users_explain/axes/legend_guide.py @@ -1,7 +1,7 @@ """ .. redirect-from:: /tutorials/intermediate/legend_guide -.. redirect-from:: /galleries/examples/userdemo/simple_legend01 -.. redirect-from:: /galleries/examples/userdemo/simple_legend02 +.. redirect-from:: /gallery/userdemo/simple_legend01 +.. redirect-from:: /gallery/userdemo/simple_legend02 .. _legend_guide: diff --git a/pyproject.toml b/pyproject.toml index 70b078a73d27..b980e512769a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -196,7 +196,6 @@ convention = "numpy" "galleries/examples/user_interfaces/mpl_with_glade3_sgskip.py" = ["E402"] "galleries/examples/user_interfaces/pylab_with_gtk3_sgskip.py" = ["E402"] "galleries/examples/user_interfaces/pylab_with_gtk4_sgskip.py" = ["E402"] -"galleries/examples/userdemo/pgf_preamble_sgskip.py" = ["E402"] "lib/matplotlib/__init__.py" = ["F822"] "lib/matplotlib/_cm.py" = ["E202", "E203", "E302"] From bbff84c0df2bcfa59bacd8f38723e6be0de0fd84 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 20 Jun 2025 19:17:22 -0400 Subject: [PATCH 09/50] Replace facepair_t with std::optional This type seems to cover the intent more clearly than `std::pair`. --- src/_backend_agg.h | 47 ++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/_backend_agg.h b/src/_backend_agg.h index 6eb54e485e86..1ac3d4c06b13 100644 --- a/src/_backend_agg.h +++ b/src/_backend_agg.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "agg_alpha_mask_u8.h" @@ -123,9 +124,6 @@ class RendererAgg typedef agg::renderer_base renderer_base_alpha_mask_type; typedef agg::renderer_scanline_aa_solid renderer_alpha_mask_type; - /* TODO: Remove facepair_t */ - typedef std::pair facepair_t; - RendererAgg(unsigned int width, unsigned int height, double dpi); virtual ~RendererAgg(); @@ -248,7 +246,7 @@ class RendererAgg bool render_clippath(mpl::PathIterator &clippath, const agg::trans_affine &clippath_trans, e_snap_mode snap_mode); template - void _draw_path(PathIteratorType &path, bool has_clippath, const facepair_t &face, GCAgg &gc); + void _draw_path(PathIteratorType &path, bool has_clippath, const std::optional &face, GCAgg &gc); template inline void -RendererAgg::_draw_path(path_t &path, bool has_clippath, const facepair_t &face, GCAgg &gc) +RendererAgg::_draw_path(path_t &path, bool has_clippath, const std::optional &face, GCAgg &gc) { typedef agg::conv_stroke stroke_t; typedef agg::conv_dash dash_t; @@ -306,7 +304,7 @@ RendererAgg::_draw_path(path_t &path, bool has_clippath, const facepair_t &face, typedef agg::renderer_scanline_bin_solid amask_bin_renderer_type; // Render face - if (face.first) { + if (face) { theRasterizer.add_path(path); if (gc.isaa) { @@ -314,10 +312,10 @@ RendererAgg::_draw_path(path_t &path, bool has_clippath, const facepair_t &face, pixfmt_amask_type pfa(pixFmt, alphaMask); amask_ren_type r(pfa); amask_aa_renderer_type ren(r); - ren.color(face.second); + ren.color(*face); agg::render_scanlines(theRasterizer, scanlineAlphaMask, ren); } else { - rendererAA.color(face.second); + rendererAA.color(*face); agg::render_scanlines(theRasterizer, slineP8, rendererAA); } } else { @@ -325,10 +323,10 @@ RendererAgg::_draw_path(path_t &path, bool has_clippath, const facepair_t &face, pixfmt_amask_type pfa(pixFmt, alphaMask); amask_ren_type r(pfa); amask_bin_renderer_type ren(r); - ren.color(face.second); + ren.color(*face); agg::render_scanlines(theRasterizer, scanlineAlphaMask, ren); } else { - rendererBin.color(face.second); + rendererBin.color(*face); agg::render_scanlines(theRasterizer, slineP8, rendererBin); } } @@ -458,7 +456,10 @@ RendererAgg::draw_path(GCAgg &gc, PathIterator &path, agg::trans_affine &trans, typedef agg::conv_curve curve_t; typedef Sketch sketch_t; - facepair_t face(color.a != 0.0, color); + std::optional face; + if (color.a != 0.0) { + face = color; + } theRasterizer.reset_clipping(); rendererBase.reset_clipping(true); @@ -467,7 +468,7 @@ RendererAgg::draw_path(GCAgg &gc, PathIterator &path, agg::trans_affine &trans, trans *= agg::trans_affine_scaling(1.0, -1.0); trans *= agg::trans_affine_translation(0.0, (double)height); - bool clip = !face.first && !gc.has_hatchpath(); + bool clip = !face && !gc.has_hatchpath(); bool simplify = path.should_simplify() && clip; double snapping_linewidth = points_to_pixels(gc.linewidth); if (gc.color.a == 0.0) { @@ -529,7 +530,10 @@ inline void RendererAgg::draw_markers(GCAgg &gc, curve_t path_curve(path_snapped); path_curve.rewind(0); - facepair_t face(color.a != 0.0, color); + std::optional face; + if (color.a != 0.0) { + face = color; + } // maxim's suggestions for cached scanlines agg::scanline_storage_aa8 scanlines; @@ -541,7 +545,7 @@ inline void RendererAgg::draw_markers(GCAgg &gc, try { std::vector fillBuffer; - if (face.first) { + if (face) { theRasterizer.add_path(marker_path_curve); agg::render_scanlines(theRasterizer, slineP8, scanlines); fillBuffer.resize(scanlines.byte_size()); @@ -605,8 +609,8 @@ inline void RendererAgg::draw_markers(GCAgg &gc, amask_ren_type r(pfa); amask_aa_renderer_type ren(r); - if (face.first) { - ren.color(face.second); + if (face) { + ren.color(*face); sa.init(fillBuffer.data(), fillBuffer.size(), x, y); agg::render_scanlines(sa, sl, ren); } @@ -633,8 +637,8 @@ inline void RendererAgg::draw_markers(GCAgg &gc, continue; } - if (face.first) { - rendererAA.color(face.second); + if (face) { + rendererAA.color(*face); sa.init(fillBuffer.data(), fillBuffer.size(), x, y); agg::render_scanlines(sa, sl, rendererAA); } @@ -936,10 +940,9 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc, // Set some defaults, assuming no face or edge gc.linewidth = 0.0; - facepair_t face; - face.first = Nfacecolors != 0; + std::optional face; agg::trans_affine trans; - bool do_clip = !face.first && !gc.has_hatchpath(); + bool do_clip = Nfacecolors == 0 && !gc.has_hatchpath(); for (int i = 0; i < (int)N; ++i) { typename PathGenerator::path_iterator path = path_generator(i); @@ -970,7 +973,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc, if (Nfacecolors) { int ic = i % Nfacecolors; - face.second = agg::rgba(facecolors(ic, 0), facecolors(ic, 1), facecolors(ic, 2), facecolors(ic, 3)); + face.emplace(facecolors(ic, 0), facecolors(ic, 1), facecolors(ic, 2), facecolors(ic, 3)); } if (Nedgecolors) { From e77c17fef43714decfb8e4108916d524e30d97fa Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 22 Jun 2025 23:11:56 +0200 Subject: [PATCH 10/50] Use collections.deque to store animation cache data. deques are exactly suited for the task at hand, and should be (here, marginally) more efficient than repeatedly slicing off the start of the list. Also drop unneeded reference to old_frame_seq (the iterator already keeps that data alive). --- lib/matplotlib/animation.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index c6ff7702d992..8756cb0c1439 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -1,5 +1,6 @@ import abc import base64 +import collections import contextlib from io import BytesIO, TextIOWrapper import itertools @@ -1708,13 +1709,13 @@ def iter_frames(frames=frames): self._cache_frame_data = cache_frame_data # Needs to be initialized so the draw functions work without checking - self._save_seq = [] + self._save_seq = collections.deque([], self._save_count) super().__init__(fig, **kwargs) # Need to reset the saved seq, since right now it will contain data # for a single frame from init, which is not what we want. - self._save_seq = [] + self._save_seq.clear() def new_frame_seq(self): # Use the generating function to generate a new frame sequence @@ -1727,8 +1728,7 @@ def new_saved_frame_seq(self): if self._save_seq: # While iterating we are going to update _save_seq # so make a copy to safely iterate over - self._old_saved_seq = list(self._save_seq) - return iter(self._old_saved_seq) + return iter([*self._save_seq]) else: if self._save_count is None: frame_seq = self.new_frame_seq() @@ -1773,13 +1773,12 @@ def _init_draw(self): 'return a sequence of Artist objects.') for a in self._drawn_artists: a.set_animated(self._blit) - self._save_seq = [] + self._save_seq.clear() def _draw_frame(self, framedata): if self._cache_frame_data: # Save the data for potential saving of movies. self._save_seq.append(framedata) - self._save_seq = self._save_seq[-self._save_count:] # Call the func with framedata and args. If blitting is desired, # func needs to return a sequence of any artists that were modified. From 6e51bec39261675fcba9b697df4ac5b6c30ed796 Mon Sep 17 00:00:00 2001 From: Constantinos Menelaou <91343054+konmenel@users.noreply.github.com> Date: Tue, 24 Jun 2025 07:24:33 +0100 Subject: [PATCH 11/50] Support individual styling of major and minor grid through rcParams (#29481) * Possible fix for issue #13919 * fix flake warnings * Resolve suggested changes * Add new validators in stub file * make stub file arguments same as runtime * Add distinction for x and y axis in grid line options * fix flake8 * Add What's New note * Extend `_val_or_rc` to support multiply names `_val_or_rc` now accept multiple rc names and return val or the first non-None value in rcParams. Returns last rc name if all other are None. Also, simplified code in `Tick` for grid lines creatation * Fix linting for and _validate_linestyle_or_None to mypy allowlist * Remove validate linestyle functions from stubtest allow list * Revert change to just grid.major/minor distinction in rcParams * Update What's New note and reduced example * Add testing for `grid.major/minor.*` in rcParams * fix indentation and linting * Fix example description * Fix spelling * Fix type * Fix formatting Removed outer brackets * `validate_color_or_None` private and fix argument names * Fix validator name in stub file as well * correct validators for grid.*.color keys * Revert change in mypy-stubtest-allowlist.txt --------- Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- .../new_rcparams_grid_options.rst | 33 +++++++++++++++++++ lib/matplotlib/__init__.py | 13 ++++++-- lib/matplotlib/axis.py | 25 +++++++++++--- lib/matplotlib/mpl-data/matplotlibrc | 10 ++++++ lib/matplotlib/rcsetup.py | 23 +++++++++++++ lib/matplotlib/rcsetup.pyi | 2 ++ lib/matplotlib/tests/test_axis.py | 28 ++++++++++++++++ 7 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 doc/users/next_whats_new/new_rcparams_grid_options.rst diff --git a/doc/users/next_whats_new/new_rcparams_grid_options.rst b/doc/users/next_whats_new/new_rcparams_grid_options.rst new file mode 100644 index 000000000000..c2c0455eecbb --- /dev/null +++ b/doc/users/next_whats_new/new_rcparams_grid_options.rst @@ -0,0 +1,33 @@ +Separate styling options for major/minor grid line in rcParams +-------------------------------------------------------------- + +Using :rc:`grid.major.*` or :rc:`grid.minor.*` will overwrite the value in +:rc:`grid.*` for the major and minor gridlines, respectively. + +.. plot:: + :include-source: true + :alt: Modifying the gridlines using the new options `rcParams` + + import matplotlib as mpl + import matplotlib.pyplot as plt + + + # Set visibility for major and minor gridlines + mpl.rcParams["axes.grid"] = True + mpl.rcParams["ytick.minor.visible"] = True + mpl.rcParams["xtick.minor.visible"] = True + mpl.rcParams["axes.grid.which"] = "both" + + # Using old values to set both major and minor properties + mpl.rcParams["grid.color"] = "red" + mpl.rcParams["grid.linewidth"] = 1 + + # Overwrite some values for major and minor separately + mpl.rcParams["grid.major.color"] = "black" + mpl.rcParams["grid.major.linewidth"] = 2 + mpl.rcParams["grid.minor.linestyle"] = ":" + mpl.rcParams["grid.minor.alpha"] = 0.6 + + plt.plot([0, 1], [0, 1]) + + plt.show() diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 9abc6c5a84dd..8ffd002d43bc 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1309,11 +1309,18 @@ def is_interactive(): return rcParams['interactive'] -def _val_or_rc(val, rc_name): +def _val_or_rc(val, *rc_names): """ - If *val* is None, return ``mpl.rcParams[rc_name]``, otherwise return val. + If *val* is None, the first not-None value in ``mpl.rcParams[rc_names[i]]``. + If all are None returns ``mpl.rcParams[rc_names[-1]]``. """ - return val if val is not None else rcParams[rc_name] + if val is not None: + return val + + for rc_name in rc_names[:-1]: + if rcParams[rc_name] is not None: + return rcParams[rc_name] + return rcParams[rc_names[-1]] def _init_tests(): diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 19096fc29d3e..fafdf92017f2 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -125,16 +125,33 @@ def __init__( zorder = mlines.Line2D.zorder self._zorder = zorder - grid_color = mpl._val_or_rc(grid_color, "grid.color") - grid_linestyle = mpl._val_or_rc(grid_linestyle, "grid.linestyle") - grid_linewidth = mpl._val_or_rc(grid_linewidth, "grid.linewidth") + grid_color = mpl._val_or_rc( + grid_color, + f"grid.{major_minor}.color", + "grid.color", + ) + grid_linestyle = mpl._val_or_rc( + grid_linestyle, + f"grid.{major_minor}.linestyle", + "grid.linestyle", + ) + grid_linewidth = mpl._val_or_rc( + grid_linewidth, + f"grid.{major_minor}.linewidth", + "grid.linewidth", + ) if grid_alpha is None and not mcolors._has_alpha_channel(grid_color): # alpha precedence: kwarg > color alpha > rcParams['grid.alpha'] # Note: only resolve to rcParams if the color does not have alpha # otherwise `grid(color=(1, 1, 1, 0.5))` would work like # grid(color=(1, 1, 1, 0.5), alpha=rcParams['grid.alpha']) # so the that the rcParams default would override color alpha. - grid_alpha = mpl.rcParams["grid.alpha"] + grid_alpha = mpl._val_or_rc( + # grid_alpha is None so we can use the first key + mpl.rcParams[f"grid.{major_minor}.alpha"], + "grid.alpha", + ) + grid_kw = {k[5:]: v for k, v in kwargs.items() if k != "rotation_mode"} self.tick1line = mlines.Line2D( diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index 780dcd377041..ec649560ba3b 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -543,6 +543,16 @@ #grid.linewidth: 0.8 # in points #grid.alpha: 1.0 # transparency, between 0.0 and 1.0 +#grid.major.color: None # If None defaults to grid.color +#grid.major.linestyle: None # If None defaults to grid.linestyle +#grid.major.linewidth: None # If None defaults to grid.linewidth +#grid.major.alpha: None # If None defaults to grid.alpha + +#grid.minor.color: None # If None defaults to grid.color +#grid.minor.linestyle: None # If None defaults to grid.linestyle +#grid.minor.linewidth: None # If None defaults to grid.linewidth +#grid.minor.alpha: None # If None defaults to grid.alpha + ## *************************************************************************** ## * LEGEND * diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 02e3601ff4c2..80d25659888e 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -361,6 +361,12 @@ def validate_color(s): raise ValueError(f'{s!r} does not look like a color arg') +def _validate_color_or_None(s): + if s is None or cbook._str_equal(s, "None"): + return None + return validate_color(s) + + validate_colorlist = _listify_validator( validate_color, allow_stringlist=True, doc='return a list of colorspecs') @@ -515,6 +521,13 @@ def _is_iterable_not_string_like(x): raise ValueError(f"linestyle {ls!r} is not a valid on-off ink sequence.") +def _validate_linestyle_or_None(s): + if s is None or cbook._str_equal(s, "None"): + return None + + return _validate_linestyle(s) + + validate_fillstyle = ValidateInStrings( 'markers.fillstyle', ['full', 'left', 'right', 'bottom', 'top', 'none']) @@ -1242,6 +1255,16 @@ def _convert_validator_spec(key, conv): "grid.linewidth": validate_float, # in points "grid.alpha": validate_float, + "grid.major.color": _validate_color_or_None, # grid color + "grid.major.linestyle": _validate_linestyle_or_None, # solid + "grid.major.linewidth": validate_float_or_None, # in points + "grid.major.alpha": validate_float_or_None, + + "grid.minor.color": _validate_color_or_None, # grid color + "grid.minor.linestyle": _validate_linestyle_or_None, # solid + "grid.minor.linewidth": validate_float_or_None, # in points + "grid.minor.alpha": validate_float_or_None, + ## figure props # figure title "figure.titlesize": validate_fontsize, diff --git a/lib/matplotlib/rcsetup.pyi b/lib/matplotlib/rcsetup.pyi index eb1d7c9f3a33..c6611845723d 100644 --- a/lib/matplotlib/rcsetup.pyi +++ b/lib/matplotlib/rcsetup.pyi @@ -48,6 +48,7 @@ def validate_color_or_auto(s: Any) -> ColorType | Literal["auto"]: ... def _validate_color_or_edge(s: Any) -> ColorType | Literal["edge"]: ... def validate_color_for_prop_cycle(s: Any) -> ColorType: ... def validate_color(s: Any) -> ColorType: ... +def _validate_color_or_None(s: Any) -> ColorType | None: ... def validate_colorlist(s: Any) -> list[ColorType]: ... def _validate_color_or_linecolor( s: Any, @@ -137,6 +138,7 @@ def validate_fillstylelist( ) -> list[Literal["full", "left", "right", "bottom", "top", "none"]]: ... def validate_markevery(s: Any) -> MarkEveryType: ... def _validate_linestyle(s: Any) -> LineStyleType: ... +def _validate_linestyle_or_None(s: Any) -> LineStyleType | None: ... def validate_markeverylist(s: Any) -> list[MarkEveryType]: ... def validate_bbox(s: Any) -> Literal["tight", "standard"] | None: ... def validate_sketch(s: Any) -> None | tuple[float, float, float]: ... diff --git a/lib/matplotlib/tests/test_axis.py b/lib/matplotlib/tests/test_axis.py index 5cb3ff4d3856..97884a33208f 100644 --- a/lib/matplotlib/tests/test_axis.py +++ b/lib/matplotlib/tests/test_axis.py @@ -67,3 +67,31 @@ def test_get_tick_position_tick_params(): right=True, labelright=True, left=False, labelleft=False) assert ax.xaxis.get_ticks_position() == "top" assert ax.yaxis.get_ticks_position() == "right" + + +def test_grid_rcparams(): + """Tests that `grid.major/minor.*` overwrites `grid.*` in rcParams.""" + plt.rcParams.update({ + "axes.grid": True, "axes.grid.which": "both", + "ytick.minor.visible": True, "xtick.minor.visible": True, + }) + def_linewidth = plt.rcParams["grid.linewidth"] + def_linestyle = plt.rcParams["grid.linestyle"] + def_alpha = plt.rcParams["grid.alpha"] + + plt.rcParams.update({ + "grid.color": "gray", "grid.minor.color": "red", + "grid.major.linestyle": ":", "grid.major.linewidth": 2, + "grid.minor.alpha": 0.6, + }) + _, ax = plt.subplots() + ax.plot([0, 1]) + + assert ax.xaxis.get_major_ticks()[0].gridline.get_color() == "gray" + assert ax.xaxis.get_minor_ticks()[0].gridline.get_color() == "red" + assert ax.xaxis.get_major_ticks()[0].gridline.get_linewidth() == 2 + assert ax.xaxis.get_minor_ticks()[0].gridline.get_linewidth() == def_linewidth + assert ax.xaxis.get_major_ticks()[0].gridline.get_linestyle() == ":" + assert ax.xaxis.get_minor_ticks()[0].gridline.get_linestyle() == def_linestyle + assert ax.xaxis.get_major_ticks()[0].gridline.get_alpha() == def_alpha + assert ax.xaxis.get_minor_ticks()[0].gridline.get_alpha() == 0.6 From e3c7a64a350bc575c6d3bf203929c9d666e644da Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 19 May 2025 15:07:52 +0200 Subject: [PATCH 12/50] Simplify dviFontInfo layout in backend pdf. - Use a simpler deterministic mapping of tex font names to pdf embedding names. - Only resolve the required attributes when needed (in _embedTeXFont), which avoids e.g. having to carry around and worry about attributes with different names (e.g. "encoding" vs. "encodingfile"). --- lib/matplotlib/backends/backend_pdf.py | 89 +++++++++++++------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 6d6bea585ff3..f20bdffd4a3a 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -721,7 +721,7 @@ def __init__(self, filename, metadata=None): self._internal_font_seq = (Name(f'F{i}') for i in itertools.count(1)) self._fontNames = {} # maps filenames to internal font names - self._dviFontInfo = {} # maps dvi font names to embedding information + self._dviFontInfo = {} # maps pdf names to dvifonts self._character_tracker = _backend_pdf_ps.CharacterTracker() self.alphaStates = {} # maps alpha values to graphics state objects @@ -764,9 +764,30 @@ def __init__(self, filename, metadata=None): self.writeObject(self.resourceObject, resources) fontNames = _api.deprecated("3.11")(property(lambda self: self._fontNames)) - dviFontInfo = _api.deprecated("3.11")(property(lambda self: self._dviFontInfo)) type1Descriptors = _api.deprecated("3.11")(property(lambda _: {})) + @_api.deprecated("3.11") + @property + def dviFontInfo(self): + d = {} + tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map')) + for pdfname, dvifont in self._dviFontInfo.items(): + psfont = tex_font_map[dvifont.texname] + if psfont.filename is None: + raise ValueError( + "No usable font file found for {} (TeX: {}); " + "the font may lack a Type-1 version" + .format(psfont.psname, dvifont.texname)) + d[dvifont.texname] = types.SimpleNamespace( + dvifont=dvifont, + pdfname=pdfname, + fontfile=psfont.filename, + basefont=psfont.psname, + encodingfile=psfont.encoding, + effects=psfont.effects, + ) + return d + def newPage(self, width, height): self.endStream() @@ -938,39 +959,19 @@ def fontName(self, fontprop): def dviFontName(self, dvifont): """ Given a dvi font object, return a name suitable for Op.selectfont. - This registers the font information internally (in ``_dviFontInfo``) if - not yet registered. - """ - - dvi_info = self._dviFontInfo.get(dvifont.texname) - if dvi_info is not None: - return dvi_info.pdfname - - tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map')) - psfont = tex_font_map[dvifont.texname] - if psfont.filename is None: - raise ValueError( - "No usable font file found for {} (TeX: {}); " - "the font may lack a Type-1 version" - .format(psfont.psname, dvifont.texname)) - pdfname = next(self._internal_font_seq) + Register the font internally (in ``_dviFontInfo``) if not yet registered. + """ + pdfname = Name(f"F-{dvifont.texname.decode('ascii')}") _log.debug('Assigning font %s = %s (dvi)', pdfname, dvifont.texname) - self._dviFontInfo[dvifont.texname] = types.SimpleNamespace( - dvifont=dvifont, - pdfname=pdfname, - fontfile=psfont.filename, - basefont=psfont.psname, - encodingfile=psfont.encoding, - effects=psfont.effects) - return pdfname + self._dviFontInfo[pdfname] = dvifont + return Name(pdfname) def writeFonts(self): fonts = {} - for dviname, info in sorted(self._dviFontInfo.items()): - Fx = info.pdfname - _log.debug('Embedding Type-1 font %s from dvi.', dviname) - fonts[Fx] = self._embedTeXFont(info) + for pdfname, dvifont in sorted(self._dviFontInfo.items()): + _log.debug('Embedding Type-1 font %s from dvi.', dvifont.texname) + fonts[pdfname] = self._embedTeXFont(dvifont) for filename in sorted(self._fontNames): Fx = self._fontNames[filename] _log.debug('Embedding font %s.', filename) @@ -998,9 +999,14 @@ def _write_afm_font(self, filename): self.writeObject(fontdictObject, fontdict) return fontdictObject - def _embedTeXFont(self, fontinfo): - _log.debug('Embedding TeX font %s - fontinfo=%s', - fontinfo.dvifont.texname, fontinfo.__dict__) + def _embedTeXFont(self, dvifont): + tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map')) + psfont = tex_font_map[dvifont.texname] + if psfont.filename is None: + raise ValueError( + "No usable font file found for {} (TeX: {}); " + "the font may lack a Type-1 version" + .format(psfont.psname, dvifont.texname)) # The font dictionary is the top-level object describing a font fontdictObject = self.reserveObject('font dictionary') @@ -1010,17 +1016,17 @@ def _embedTeXFont(self, fontinfo): } # Read the font file and apply any encoding changes and effects - t1font = _type1font.Type1Font(fontinfo.fontfile) - if fontinfo.encodingfile is not None: + t1font = _type1font.Type1Font(psfont.filename) + if psfont.encoding is not None: t1font = t1font.with_encoding( - {i: c for i, c in enumerate(dviread._parse_enc(fontinfo.encodingfile))} + {i: c for i, c in enumerate(dviread._parse_enc(psfont.encoding))} ) - if fontinfo.effects: - t1font = t1font.transform(fontinfo.effects) + if psfont.effects: + t1font = t1font.transform(psfont.effects) # Reduce the font to only the glyphs used in the document, get the encoding # for that subset, and compute various properties based on the encoding. - chars = frozenset(self._character_tracker.used[fontinfo.dvifont.fname]) + chars = frozenset(self._character_tracker.used[dvifont.fname]) t1font = t1font.subset(chars, self._get_subset_prefix(chars)) fontdict['BaseFont'] = Name(t1font.prop['FontName']) # createType1Descriptor writes the font data as a side effect @@ -1031,17 +1037,15 @@ def _embedTeXFont(self, fontinfo): lc = fontdict['LastChar'] = max(encoding.keys(), default=255) # Convert glyph widths from TeX 12.20 fixed point to 1/1000 text space units - tfm = fontinfo.dvifont._tfm + tfm = dvifont._tfm widths = [(1000 * metrics.tex_width) >> 20 if (metrics := tfm.get_metrics(char)) else 0 for char in range(fc, lc + 1)] fontdict['Widths'] = widthsObject = self.reserveObject('glyph widths') self.writeObject(widthsObject, widths) - self.writeObject(fontdictObject, fontdict) return fontdictObject - def _generate_encoding(self, encoding): prev = -2 result = [] @@ -1055,7 +1059,6 @@ def _generate_encoding(self, encoding): 'Differences': result } - @_api.delete_parameter("3.11", "fontfile") def createType1Descriptor(self, t1font, fontfile=None): # Create and write the font descriptor and the font file From c7edce3820d918474fadb42e55818d1c40be1979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jouni=20K=2E=20Sepp=C3=A4nen?= Date: Sat, 21 Jun 2025 09:56:00 +0300 Subject: [PATCH 13/50] Implement Path.__deepcopy__ avoiding infinite recursion To deep copy an object without calling deepcopy on the object itself, create a new object of the correct class and iterate calling deepcopy on its __dict__. Closes #29157 without relying on private CPython methods. Does not fix the other issue with TransformNode.__copy__. Co-authored-by: Serhiy Storchaka --- lib/matplotlib/path.py | 26 ++++++++++++++++++--- lib/matplotlib/path.pyi | 4 ++-- lib/matplotlib/tests/test_path.py | 38 +++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index a021706fb1e5..f65ade669167 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -275,17 +275,37 @@ def copy(self): """ return copy.copy(self) - def __deepcopy__(self, memo=None): + def __deepcopy__(self, memo): """ Return a deepcopy of the `Path`. The `Path` will not be readonly, even if the source `Path` is. """ # Deepcopying arrays (vertices, codes) strips the writeable=False flag. - p = copy.deepcopy(super(), memo) + cls = type(self) + memo[id(self)] = p = cls.__new__(cls) + + for k, v in self.__dict__.items(): + setattr(p, k, copy.deepcopy(v, memo)) + p._readonly = False return p - deepcopy = __deepcopy__ + def deepcopy(self, memo=None): + """ + Return a deep copy of the `Path`. The `Path` will not be readonly, + even if the source `Path` is. + + Parameters + ---------- + memo : dict, optional + A dictionary to use for memoizing, passed to `copy.deepcopy`. + + Returns + ------- + Path + A deep copy of the `Path`, but not readonly. + """ + return copy.deepcopy(self, memo) @classmethod def make_compound_path_from_polys(cls, XY): diff --git a/lib/matplotlib/path.pyi b/lib/matplotlib/path.pyi index 464fc6d9a912..8a5a5c03792e 100644 --- a/lib/matplotlib/path.pyi +++ b/lib/matplotlib/path.pyi @@ -44,8 +44,8 @@ class Path: @property def readonly(self) -> bool: ... def copy(self) -> Path: ... - def __deepcopy__(self, memo: dict[int, Any] | None = ...) -> Path: ... - deepcopy = __deepcopy__ + def __deepcopy__(self, memo: dict[int, Any]) -> Path: ... + def deepcopy(self, memo: dict[int, Any] | None = None) -> Path: ... @classmethod def make_compound_path_from_polys(cls, XY: ArrayLike) -> Path: ... diff --git a/lib/matplotlib/tests/test_path.py b/lib/matplotlib/tests/test_path.py index d4dc0141e63b..5dde1d876b04 100644 --- a/lib/matplotlib/tests/test_path.py +++ b/lib/matplotlib/tests/test_path.py @@ -355,15 +355,49 @@ def test_path_deepcopy(): # Should not raise any error verts = [[0, 0], [1, 1]] codes = [Path.MOVETO, Path.LINETO] - path1 = Path(verts) - path2 = Path(verts, codes) + path1 = Path(verts, readonly=True) + path2 = Path(verts, codes, readonly=True) path1_copy = path1.deepcopy() path2_copy = path2.deepcopy() assert path1 is not path1_copy assert path1.vertices is not path1_copy.vertices + assert np.all(path1.vertices == path1_copy.vertices) + assert path1.readonly + assert not path1_copy.readonly assert path2 is not path2_copy assert path2.vertices is not path2_copy.vertices + assert np.all(path2.vertices == path2_copy.vertices) assert path2.codes is not path2_copy.codes + assert all(path2.codes == path2_copy.codes) + assert path2.readonly + assert not path2_copy.readonly + + +def test_path_deepcopy_cycle(): + class PathWithCycle(Path): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.x = self + + p = PathWithCycle([[0, 0], [1, 1]], readonly=True) + p_copy = p.deepcopy() + assert p_copy is not p + assert p.readonly + assert not p_copy.readonly + assert p_copy.x is p_copy + + class PathWithCycle2(Path): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.x = [self] * 2 + + p2 = PathWithCycle2([[0, 0], [1, 1]], readonly=True) + p2_copy = p2.deepcopy() + assert p2_copy is not p2 + assert p2.readonly + assert not p2_copy.readonly + assert p2_copy.x[0] is p2_copy + assert p2_copy.x[1] is p2_copy def test_path_shallowcopy(): From b20cf20024a5836e06e38f58bc4e88f3caec5423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jouni=20K=2E=20Sepp=C3=A4nen?= Date: Tue, 24 Jun 2025 07:40:20 +0300 Subject: [PATCH 14/50] Compare arrays with assert_array_equal Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_path.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_path.py b/lib/matplotlib/tests/test_path.py index 5dde1d876b04..a61f01c0d48a 100644 --- a/lib/matplotlib/tests/test_path.py +++ b/lib/matplotlib/tests/test_path.py @@ -361,14 +361,14 @@ def test_path_deepcopy(): path2_copy = path2.deepcopy() assert path1 is not path1_copy assert path1.vertices is not path1_copy.vertices - assert np.all(path1.vertices == path1_copy.vertices) + assert_array_equal(path1.vertices, path1_copy.vertices) assert path1.readonly assert not path1_copy.readonly assert path2 is not path2_copy assert path2.vertices is not path2_copy.vertices - assert np.all(path2.vertices == path2_copy.vertices) + assert_array_equal(path2.vertices, path2_copy.vertices) assert path2.codes is not path2_copy.codes - assert all(path2.codes == path2_copy.codes) + assert_array_equal(path2.codes, path2_copy.codes) assert path2.readonly assert not path2_copy.readonly From 5c7c91542127adf03fa2f401dcb1d3c38b9a9a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jouni=20K=2E=20Sepp=C3=A4nen?= Date: Sat, 21 Jun 2025 17:18:38 +0300 Subject: [PATCH 15/50] Fix TransformNode.__copy__ without calling copy.copy --- lib/matplotlib/transforms.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 7228f05bcf9e..350113c56170 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -35,7 +35,6 @@ # `np.minimum` instead of the builtin `min`, and likewise for `max`. This is # done so that `nan`s are propagated, instead of being silently dropped. -import copy import functools import itertools import textwrap @@ -139,7 +138,9 @@ def __setstate__(self, data_dict): for k, v in self._parents.items() if v is not None} def __copy__(self): - other = copy.copy(super()) + cls = type(self) + other = cls.__new__(cls) + other.__dict__.update(self.__dict__) # If `c = a + b; a1 = copy(a)`, then modifications to `a1` do not # propagate back to `c`, i.e. we need to clear the parents of `a1`. other._parents = {} From 4ab1ab55a7bcfedb59ce4d5e433a58e0d4e5813c Mon Sep 17 00:00:00 2001 From: Tingwei Zhu <852445892@qq.com> Date: Wed, 25 Jun 2025 14:24:10 +0800 Subject: [PATCH 16/50] Update deprecations.rst --- doc/api/prev_api_changes/api_changes_3.5.0/deprecations.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/prev_api_changes/api_changes_3.5.0/deprecations.rst b/doc/api/prev_api_changes/api_changes_3.5.0/deprecations.rst index 05f42035f1ac..04836687f76a 100644 --- a/doc/api/prev_api_changes/api_changes_3.5.0/deprecations.rst +++ b/doc/api/prev_api_changes/api_changes_3.5.0/deprecations.rst @@ -282,7 +282,7 @@ Miscellaneous deprecations - The *format* parameter of ``dviread.find_tex_file`` is deprecated (with no replacement). - ``FancyArrowPatch.get_path_in_displaycoord`` and - ``ConnectionPath.get_path_in_displaycoord`` are deprecated. The path in + ``ConnectionPatch.get_path_in_displaycoord`` are deprecated. The path in display coordinates can still be obtained, as for other patches, using ``patch.get_transform().transform_path(patch.get_path())``. - The ``font_manager.win32InstalledFonts`` and From dd03655e9fda05dc365960047f36ff852b859cf9 Mon Sep 17 00:00:00 2001 From: Tingwei Zhu <852445892@qq.com> Date: Wed, 25 Jun 2025 14:24:48 +0800 Subject: [PATCH 17/50] Update removals.rst --- doc/api/prev_api_changes/api_changes_3.7.0/removals.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/prev_api_changes/api_changes_3.7.0/removals.rst b/doc/api/prev_api_changes/api_changes_3.7.0/removals.rst index 03239be31057..56b3ad5c253e 100644 --- a/doc/api/prev_api_changes/api_changes_3.7.0/removals.rst +++ b/doc/api/prev_api_changes/api_changes_3.7.0/removals.rst @@ -323,7 +323,7 @@ Miscellaneous removals - The *format* parameter of ``dviread.find_tex_file`` is removed (with no replacement). - ``FancyArrowPatch.get_path_in_displaycoord`` and - ``ConnectionPath.get_path_in_displaycoord`` are removed. The path in + ``ConnectionPatch.get_path_in_displaycoord`` are removed. The path in display coordinates can still be obtained, as for other patches, using ``patch.get_transform().transform_path(patch.get_path())``. - The ``font_manager.win32InstalledFonts`` and From 3ea5b633c38876dbe191b1959d49f71cf03b0442 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 23 May 2025 11:42:06 +0200 Subject: [PATCH 18/50] Fix label_outer in the presence of colorbars. The subgridspec to be considered should be the one containing both the axes and the colorbar, not the sub-subgridspec of just the axes. --- lib/matplotlib/axes/_base.py | 19 +++++++++++++++---- lib/matplotlib/colorbar.py | 4 ++-- lib/matplotlib/tests/test_subplots.py | 10 ++++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 1ce205ede613..87d42b4d3014 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -4749,14 +4749,25 @@ def label_outer(self, remove_inner_ticks=False): self._label_outer_yaxis(skip_non_rectangular_axes=False, remove_inner_ticks=remove_inner_ticks) + def _get_subplotspec_with_optional_colorbar(self): + """ + Return the subplotspec for this Axes, except that if this Axes has been + moved to a subgridspec to make room for a colorbar, then return the + subplotspec that encloses both this Axes and the colorbar Axes. + """ + ss = self.get_subplotspec() + if any(cax.get_subplotspec() for cax in self._colorbars): + ss = ss.get_gridspec()._subplot_spec + return ss + def _label_outer_xaxis(self, *, skip_non_rectangular_axes, remove_inner_ticks=False): # see documentation in label_outer. if skip_non_rectangular_axes and not isinstance(self.patch, mpl.patches.Rectangle): return - ss = self.get_subplotspec() - if not ss: + ss = self._get_subplotspec_with_optional_colorbar() + if ss is None: return label_position = self.xaxis.get_label_position() if not ss.is_first_row(): # Remove top label/ticklabels/offsettext. @@ -4782,8 +4793,8 @@ def _label_outer_yaxis(self, *, skip_non_rectangular_axes, if skip_non_rectangular_axes and not isinstance(self.patch, mpl.patches.Rectangle): return - ss = self.get_subplotspec() - if not ss: + ss = self._get_subplotspec_with_optional_colorbar() + if ss is None: return label_position = self.yaxis.get_label_position() if not ss.is_first_col(): # Remove left label/ticklabels/offsettext. diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 19bdbe605d88..4348f02cfc34 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1455,8 +1455,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, cax = fig.add_axes(pbcb, label="") for a in parents: - # tell the parent it has a colorbar - a._colorbars += [cax] + a._colorbars.append(cax) # tell the parent it has a colorbar cax._colorbar_info = dict( parents=parents, location=location, @@ -1549,6 +1548,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, fig = parent.get_figure() cax = fig.add_subplot(ss_cb, label="") + parent._colorbars.append(cax) # tell the parent it has a colorbar cax.set_anchor(anchor) cax.set_box_aspect(aspect) cax.set_aspect('auto') diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index a899110ac77a..0f00a88aa72d 100644 --- a/lib/matplotlib/tests/test_subplots.py +++ b/lib/matplotlib/tests/test_subplots.py @@ -4,6 +4,7 @@ import numpy as np import pytest +import matplotlib as mpl from matplotlib.axes import Axes, SubplotBase import matplotlib.pyplot as plt from matplotlib.testing.decorators import check_figures_equal, image_comparison @@ -111,10 +112,15 @@ def test_shared(): @pytest.mark.parametrize('remove_ticks', [True, False]) -def test_label_outer(remove_ticks): - f, axs = plt.subplots(2, 2, sharex=True, sharey=True) +@pytest.mark.parametrize('layout_engine', ['none', 'tight', 'constrained']) +@pytest.mark.parametrize('with_colorbar', [True, False]) +def test_label_outer(remove_ticks, layout_engine, with_colorbar): + fig = plt.figure(layout=layout_engine) + axs = fig.subplots(2, 2, sharex=True, sharey=True) for ax in axs.flat: ax.set(xlabel="foo", ylabel="bar") + if with_colorbar: + fig.colorbar(mpl.cm.ScalarMappable(), ax=ax) ax.label_outer(remove_inner_ticks=remove_ticks) check_ticklabel_visible( axs.flat, [False, False, True, True], [True, False, True, False]) From 257430efe5a9d6b97fa00200c73cddf1ea9ec861 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 20 Jun 2025 05:15:23 -0400 Subject: [PATCH 19/50] ci: Enable wheel builds on Python 3.14 This should only end up on the nightly wheel upload for now. Also, re-enable testing in places where Pillow wheels were previously missing, but are now available. --- .github/workflows/cibuildwheel.yml | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cibuildwheel.yml b/.github/workflows/cibuildwheel.yml index 57f1c71c5047..fececb0dfc40 100644 --- a/.github/workflows/cibuildwheel.yml +++ b/.github/workflows/cibuildwheel.yml @@ -140,6 +140,20 @@ jobs: name: cibw-sdist path: dist/ + - name: Build wheels for CPython 3.14 + uses: pypa/cibuildwheel@5f22145df44122af0f5a201f93cf0207171beca7 # v3.0.0 + with: + package-dir: dist/${{ needs.build_sdist.outputs.SDIST_NAME }} + env: + CIBW_BUILD: "cp314-* cp314t-*" + CIBW_ENABLE: "cpython-freethreading cpython-prerelease" + CIBW_ARCHS: ${{ matrix.cibw_archs }} + CIBW_MANYLINUX_X86_64_IMAGE: manylinux_2_28 + CIBW_BEFORE_TEST: >- + python -m pip install + --index-url https://pypi.anaconda.org/scientific-python-nightly-wheels/simple + --upgrade --pre --only-binary=:all: contourpy numpy pillow + - name: Build wheels for CPython 3.13 uses: pypa/cibuildwheel@5f22145df44122af0f5a201f93cf0207171beca7 # v3.0.0 with: @@ -147,8 +161,6 @@ jobs: env: CIBW_BUILD: "cp313-* cp313t-*" CIBW_ENABLE: cpython-freethreading - # No free-threading wheels available for aarch64 on Pillow. - CIBW_TEST_SKIP: "cp313t-manylinux_aarch64" CIBW_ARCHS: ${{ matrix.cibw_archs }} - name: Build wheels for CPython 3.12 @@ -167,7 +179,6 @@ jobs: CIBW_BUILD: "cp311-*" CIBW_ARCHS: ${{ matrix.cibw_archs }} - - name: Build wheels for PyPy uses: pypa/cibuildwheel@5f22145df44122af0f5a201f93cf0207171beca7 # v3.0.0 with: @@ -176,8 +187,6 @@ jobs: CIBW_BUILD: "pp311-*" CIBW_ARCHS: ${{ matrix.cibw_archs }} CIBW_ENABLE: pypy - # No wheels available for Pillow with pp311 yet. - CIBW_TEST_SKIP: "pp311*" if: matrix.cibw_archs != 'aarch64' && matrix.os != 'windows-latest' - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 From b06d1f4d8d7199746063b1ef0d1a1c37ccf48188 Mon Sep 17 00:00:00 2001 From: chrisjbillington Date: Thu, 26 Jun 2025 12:38:33 +1000 Subject: [PATCH 20/50] Clean up Qt socket notifier to avoid spurious interrupt handler calls Closes #29688 Objects without a parent are not necessarily cleaned up in `PyQt5/6` when their reference count reaches zero, and must be explicitly cleaned up with `deleteLater()` This prevents the notifier firing after the signal handling was supposed to have been reset to its previous state. Rather than have both `bakend_bases._allow_interrupt()` and `backend_qt._allow_interrupt_qt()` hold a reference to the notifier, we pass it to the backend-specific `handle_signint()` function for cleanup. Note the approach to cleaning up the notifier with `.deleteLater()` followed by `sendPostedEvents()` is the documented workaround for when immediate deletion is desired: https://doc.qt.io/qt-6/qobject.html#deleteLater This ensures the object is still deleted up even if the same event loop does not run again. --- lib/matplotlib/backend_bases.py | 10 ++++++---- lib/matplotlib/backends/backend_qt.py | 11 ++++++++--- src/_macosx.m | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 527d8c010710..626852f2aa34 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1619,7 +1619,8 @@ def _allow_interrupt(prepare_notifier, handle_sigint): If SIGINT was indeed caught, after exiting the on_signal() function the interpreter reacts to the signal according to the handler function which had been set up by a signal.signal() call; here, we arrange to call the - backend-specific *handle_sigint* function. Finally, we call the old SIGINT + backend-specific *handle_sigint* function, passing the notifier object + as returned by prepare_notifier(). Finally, we call the old SIGINT handler with the same arguments that were given to our custom handler. We do this only if the old handler for SIGINT was not None, which means @@ -1629,7 +1630,7 @@ def _allow_interrupt(prepare_notifier, handle_sigint): Parameters ---------- prepare_notifier : Callable[[socket.socket], object] - handle_sigint : Callable[[], object] + handle_sigint : Callable[[object], object] """ old_sigint_handler = signal.getsignal(signal.SIGINT) @@ -1645,9 +1646,10 @@ def _allow_interrupt(prepare_notifier, handle_sigint): notifier = prepare_notifier(rsock) def save_args_and_handle_sigint(*args): - nonlocal handler_args + nonlocal handler_args, notifier handler_args = args - handle_sigint() + handle_sigint(notifier) + notifier = None signal.signal(signal.SIGINT, save_args_and_handle_sigint) try: diff --git a/lib/matplotlib/backends/backend_qt.py b/lib/matplotlib/backends/backend_qt.py index 5cde4866cad7..9089e982cea6 100644 --- a/lib/matplotlib/backends/backend_qt.py +++ b/lib/matplotlib/backends/backend_qt.py @@ -169,9 +169,14 @@ def _may_clear_sock(): # be forgiving about reading an empty socket. pass - return sn # Actually keep the notifier alive. - - def handle_sigint(): + # We return the QSocketNotifier so that the caller holds a reference, and we + # also explicitly clean it up in handle_sigint(). Without doing both, deletion + # of the socket notifier can happen prematurely or not at all. + return sn + + def handle_sigint(sn): + sn.deleteLater() + QtCore.QCoreApplication.sendPostedEvents(sn, QtCore.QEvent.Type.DeferredDelete) if hasattr(qapp_or_eventloop, 'closeAllWindows'): qapp_or_eventloop.closeAllWindows() qapp_or_eventloop.quit() diff --git a/src/_macosx.m b/src/_macosx.m index aa2a6e68cda5..1372157bc80d 100755 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -258,7 +258,7 @@ static void lazy_init(void) { } static PyObject* -stop(PyObject* self) +stop(PyObject* self, PyObject* _ /* ignored */) { stopWithEvent(); Py_RETURN_NONE; @@ -1863,7 +1863,7 @@ - (void)flagsChanged:(NSEvent *)event "written on the file descriptor given as argument.")}, {"stop", (PyCFunction)stop, - METH_NOARGS, + METH_VARARGS, PyDoc_STR("Stop the NSApp.")}, {"show", (PyCFunction)show, From c78c2f449669347842e000d03bc072806af198ea Mon Sep 17 00:00:00 2001 From: Vagner Messias <32200525+vagnermcj@users.noreply.github.com> Date: Thu, 26 Jun 2025 23:28:51 -0300 Subject: [PATCH 21/50] Refactoring: Removing axis parameter from scales (#29988) * Adding a decorator and Refactoring functions * Fixing Ruff Errors * Update scale.pyi * Adding new line to the end of scale.pyi * Update in docstring * Fixing Handle Function * Support optional axis in scales Updated my refactor based on the feedbacks received * Fixing ruff error * change in parameters and in decorator * parameter fix * minor change in pyi * Update lib/matplotlib/scale.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Update lib/matplotlib/scale.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Update lib/matplotlib/scale.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Update lib/matplotlib/scale.pyi Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Updating self and axis in pyi * returning scale_factory to default * Ruff checks * description fix * Update lib/matplotlib/scale.pyi Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Undoing Unrelated Modifications * fixing mypy tests * Update lib/matplotlib/scale.pyi Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * keyword-argument suggestion Co-authored-by: Elliott Sales de Andrade * kwargs pop before function call Co-authored-by: Elliott Sales de Andrade --------- Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/scale.py | 63 +++++++++++++++++++++++++++++++++++++--- lib/matplotlib/scale.pyi | 13 ++++++--- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/scale.py b/lib/matplotlib/scale.py index 44fbe5209c4d..4517b8946b03 100644 --- a/lib/matplotlib/scale.py +++ b/lib/matplotlib/scale.py @@ -31,6 +31,7 @@ import inspect import textwrap +from functools import wraps import numpy as np @@ -103,6 +104,53 @@ def limit_range_for_scale(self, vmin, vmax, minpos): return vmin, vmax +def _make_axis_parameter_optional(init_func): + """ + Decorator to allow leaving out the *axis* parameter in scale constructors. + + This decorator ensures backward compatibility for scale classes that + previously required an *axis* parameter. It allows constructors to be + callerd with or without the *axis* parameter. + + For simplicity, this does not handle the case when *axis* + is passed as a keyword. However, + scanning GitHub, there's no evidence that that is used anywhere. + + Parameters + ---------- + init_func : callable + The original __init__ method of a scale class. + + Returns + ------- + callable + A wrapped version of *init_func* that handles the optional *axis*. + + Notes + ----- + If the wrapped constructor defines *axis* as its first argument, the + parameter is preserved when present. Otherwise, the value `None` is injected + as the first argument. + + Examples + -------- + >>> from matplotlib.scale import ScaleBase + >>> class CustomScale(ScaleBase): + ... @_make_axis_parameter_optional + ... def __init__(self, axis, custom_param=1): + ... self.custom_param = custom_param + """ + @wraps(init_func) + def wrapper(self, *args, **kwargs): + if args and isinstance(args[0], mpl.axis.Axis): + return init_func(self, *args, **kwargs) + else: + # Remove 'axis' from kwargs to avoid double assignment + axis = kwargs.pop('axis', None) + return init_func(self, axis, *args, **kwargs) + return wrapper + + class LinearScale(ScaleBase): """ The default linear scale. @@ -110,6 +158,7 @@ class LinearScale(ScaleBase): name = 'linear' + @_make_axis_parameter_optional def __init__(self, axis): # This method is present only to prevent inheritance of the base class' # constructor docstring, which would otherwise end up interpolated into @@ -180,6 +229,7 @@ class FuncScale(ScaleBase): name = 'function' + @_make_axis_parameter_optional def __init__(self, axis, functions): """ Parameters @@ -279,7 +329,8 @@ class LogScale(ScaleBase): """ name = 'log' - def __init__(self, axis, *, base=10, subs=None, nonpositive="clip"): + @_make_axis_parameter_optional + def __init__(self, axis=None, *, base=10, subs=None, nonpositive="clip"): """ Parameters ---------- @@ -330,6 +381,7 @@ class FuncScaleLog(LogScale): name = 'functionlog' + @_make_axis_parameter_optional def __init__(self, axis, functions, base=10): """ Parameters @@ -455,7 +507,8 @@ class SymmetricalLogScale(ScaleBase): """ name = 'symlog' - def __init__(self, axis, *, base=10, linthresh=2, subs=None, linscale=1): + @_make_axis_parameter_optional + def __init__(self, axis=None, *, base=10, linthresh=2, subs=None, linscale=1): self._transform = SymmetricalLogTransform(base, linthresh, linscale) self.subs = subs @@ -547,7 +600,8 @@ class AsinhScale(ScaleBase): 1024: (256, 512) } - def __init__(self, axis, *, linear_width=1.0, + @_make_axis_parameter_optional + def __init__(self, axis=None, *, linear_width=1.0, base=10, subs='auto', **kwargs): """ Parameters @@ -645,7 +699,8 @@ class LogitScale(ScaleBase): """ name = 'logit' - def __init__(self, axis, nonpositive='mask', *, + @_make_axis_parameter_optional + def __init__(self, axis=None, nonpositive='mask', *, one_half=r"\frac{1}{2}", use_overline=False): r""" Parameters diff --git a/lib/matplotlib/scale.pyi b/lib/matplotlib/scale.pyi index 7fec8e68cc5a..ba9f269b8c78 100644 --- a/lib/matplotlib/scale.pyi +++ b/lib/matplotlib/scale.pyi @@ -15,6 +15,10 @@ class ScaleBase: class LinearScale(ScaleBase): name: str + def __init__( + self, + axis: Axis | None, + ) -> None: ... class FuncTransform(Transform): input_dims: int @@ -57,7 +61,7 @@ class LogScale(ScaleBase): subs: Iterable[int] | None def __init__( self, - axis: Axis | None, + axis: Axis | None = ..., *, base: float = ..., subs: Iterable[int] | None = ..., @@ -104,7 +108,7 @@ class SymmetricalLogScale(ScaleBase): subs: Iterable[int] | None def __init__( self, - axis: Axis | None, + axis: Axis | None = ..., *, base: float = ..., linthresh: float = ..., @@ -138,7 +142,7 @@ class AsinhScale(ScaleBase): auto_tick_multipliers: dict[int, tuple[int, ...]] def __init__( self, - axis: Axis | None, + axis: Axis | None = ..., *, linear_width: float = ..., base: float = ..., @@ -165,7 +169,7 @@ class LogitScale(ScaleBase): name: str def __init__( self, - axis: Axis | None, + axis: Axis | None = ..., nonpositive: Literal["mask", "clip"] = ..., *, one_half: str = ..., @@ -176,3 +180,4 @@ class LogitScale(ScaleBase): def get_scale_names() -> list[str]: ... def scale_factory(scale: str, axis: Axis, **kwargs) -> ScaleBase: ... def register_scale(scale_class: type[ScaleBase]) -> None: ... +def _make_axis_parameter_optional(init_func: Callable[..., None]) -> Callable[..., None]: ... From ac863bc96a2b991fa4452a39b029feed0ccd88c4 Mon Sep 17 00:00:00 2001 From: DWesl <22566757+DWesl@users.noreply.github.com> Date: Fri, 27 Jun 2025 12:09:40 -0400 Subject: [PATCH 22/50] BUG: Include python-including headers first --- src/ft2font.cpp | 6 +++--- src/ft2font.h | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ft2font.cpp b/src/ft2font.cpp index bdfa2873ca80..da1bd19dca57 100644 --- a/src/ft2font.cpp +++ b/src/ft2font.cpp @@ -1,5 +1,8 @@ /* -*- mode: c++; c-basic-offset: 4 -*- */ +#include "ft2font.h" +#include "mplutils.h" + #include #include #include @@ -9,9 +12,6 @@ #include #include -#include "ft2font.h" -#include "mplutils.h" - #ifndef M_PI #define M_PI 3.14159265358979323846264338328 #endif diff --git a/src/ft2font.h b/src/ft2font.h index 8db0239ed4fd..6676a7dd4818 100644 --- a/src/ft2font.h +++ b/src/ft2font.h @@ -6,6 +6,9 @@ #ifndef MPL_FT2FONT_H #define MPL_FT2FONT_H +#include +#include + #include #include #include @@ -22,8 +25,6 @@ extern "C" { #include FT_TRUETYPE_TABLES_H } -#include -#include namespace py = pybind11; // By definition, FT_FIXED as 2 16bit values stored in a single long. From 4b03f0c99fc0de819a40def011d6f19fddc2b367 Mon Sep 17 00:00:00 2001 From: ZPyrolink <73246085+ZPyrolink@users.noreply.github.com> Date: Fri, 27 Jun 2025 20:30:44 +0200 Subject: [PATCH 23/50] Add explicit overloads for pyplot.{show,subplot} (#30202) * Create show overloads on pyplot.py (cherry picked from commit 1ff2bd2be6ab4610b9c59e5d44c44bdcdfda7241) * Create subplot overloads on pyplot.py (cherry picked from commit f4693e3c984f4775e29c3d10e48f58ae7de84e98) * Removed subplot(Axes) on overloads and docstring * Update show overloads * Correct mypy error * Add typing to overloaded copy of show * Revert superfluous changes --------- Co-authored-by: Corenthin ZOZOR --- lib/matplotlib/pyplot.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index cf5c9b4b739f..d3da41ca9d5c 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -569,6 +569,14 @@ def draw_if_interactive(*args, **kwargs): return _get_backend_mod().draw_if_interactive(*args, **kwargs) +@overload +def show(*, block: bool, **kwargs) -> None: ... + + +@overload +def show(*args: Any, **kwargs: Any) -> None: ... + + # This function's signature is rewritten upon backend-load by switch_backend. def show(*args, **kwargs) -> None: """ @@ -1393,6 +1401,18 @@ def cla() -> None: ## More ways of creating Axes ## +@overload +def subplot(nrows: int, ncols: int, index: int, /, **kwargs): ... + + +@overload +def subplot(pos: int | SubplotSpec, /, **kwargs): ... + + +@overload +def subplot(**kwargs): ... + + @_docstring.interpd def subplot(*args, **kwargs) -> Axes: """ @@ -1406,7 +1426,6 @@ def subplot(*args, **kwargs) -> Axes: subplot(nrows, ncols, index, **kwargs) subplot(pos, **kwargs) subplot(**kwargs) - subplot(ax) Parameters ---------- From e36bffb3f7f866506f2bb8b99b30f50667617ea0 Mon Sep 17 00:00:00 2001 From: ZPyrolink <73246085+ZPyrolink@users.noreply.github.com> Date: Fri, 27 Jun 2025 20:31:37 +0200 Subject: [PATCH 24/50] Add explicit getter / setter overloads for pyplot.{xlim,ylim} (#30199) * Add xlim / ylim autogeneration - Create get_signature method - Create AXES_GETTER_SETTER_TEMPLATE - Create call_param method on generate_function (cherry picked from commit e57b32165cae851b3f9cb846e37e3e49339aed43) * Format with ruff (cherry picked from commit 64e7921b0b3f56c88c1f449a4f2081e862289279) * Remove old xlim and ylim (cherry picked from commit 66ee0714ff310e0693e05c4616bbb702e45a6407) * Format with ruff * Revert superfluous changes * Revert boilerplate.py and manually copy xlim and ylim overloads on pyplot.py * Remove incorrect comment about autogenerated method --------- Co-authored-by: Corenthin ZOZOR --- lib/matplotlib/pyplot.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index d3da41ca9d5c..d77b06115268 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2115,6 +2115,24 @@ def box(on: bool | None = None) -> None: ## Axis ## +@overload +def xlim() -> tuple[float, float]: + ... + + +@overload +def xlim( + left: float | tuple[float, float] | None = None, + right: float | None = None, + *, + emit: bool = True, + auto: bool | None = False, + xmin: float | None = None, + xmax: float | None = None, +) -> tuple[float, float]: + ... + + def xlim(*args, **kwargs) -> tuple[float, float]: """ Get or set the x limits of the current Axes. @@ -2152,6 +2170,24 @@ def xlim(*args, **kwargs) -> tuple[float, float]: return ret +@overload +def ylim() -> tuple[float, float]: + ... + + +@overload +def ylim( + bottom: float | tuple[float, float] | None = None, + top: float | None = None, + *, + emit: bool = True, + auto: bool | None = False, + ymin: float | None = None, + ymax: float | None = None, +) -> tuple[float, float]: + ... + + def ylim(*args, **kwargs) -> tuple[float, float]: """ Get or set the y-limits of the current Axes. From 0b2fa3f952b6b97a121a3d7f05a0753b52b802cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 28 Jun 2025 11:39:49 +0200 Subject: [PATCH 25/50] BUG: fix future incompatibility with Pillow 13 --- lib/matplotlib/backends/_backend_tk.py | 2 +- lib/matplotlib/backends/backend_pdf.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index 0bbff1379ffa..eaf868fd8bec 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -775,7 +775,7 @@ def _recolor_icon(image, color): image_data = np.asarray(image).copy() black_mask = (image_data[..., :3] == 0).all(axis=-1) image_data[black_mask, :3] = color - return Image.fromarray(image_data, mode="RGBA") + return Image.fromarray(image_data) # Use the high-resolution (48x48 px) icon if it exists and is needed with Image.open(path_large if (size > 24 and path_large.exists()) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index f20bdffd4a3a..4429dc9ba707 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1784,7 +1784,8 @@ def _writeImg(self, data, id, smask=None): data[:, :, 2]) indices = np.argsort(palette24).astype(np.uint8) rgb8 = indices[np.searchsorted(palette24, rgb24, sorter=indices)] - img = Image.fromarray(rgb8, mode='P') + img = Image.fromarray(rgb8) + img.convert("P") img.putpalette(palette) png_data, bit_depth, palette = self._writePng(img) if bit_depth is None or palette is None: From 7edd74a20b59d8b61a7349b59056b8706058878b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Trygve=20Magnus=20R=C3=A6der?= Date: Sat, 28 Jun 2025 16:13:01 +0200 Subject: [PATCH 26/50] Abstract base class for Normalize (#30178) * Abstract base class for Normalize * Include the Norm ABC in the docs * Changed name of temporary class Norm to ScaleNorm in _make_norm_from_scale() * removal of inverse() in Norm ABC --- doc/api/colors_api.rst | 1 + lib/matplotlib/colorizer.py | 2 +- lib/matplotlib/colorizer.pyi | 14 +- lib/matplotlib/colors.py | 143 ++++++++++++------ lib/matplotlib/colors.pyi | 24 ++- .../test_colors/test_norm_abc.png | Bin 0 -> 16129 bytes lib/matplotlib/tests/test_colors.py | 47 ++++++ 7 files changed, 177 insertions(+), 54 deletions(-) create mode 100644 lib/matplotlib/tests/baseline_images/test_colors/test_norm_abc.png diff --git a/doc/api/colors_api.rst b/doc/api/colors_api.rst index 6b02f723d74d..49a42c8f9601 100644 --- a/doc/api/colors_api.rst +++ b/doc/api/colors_api.rst @@ -21,6 +21,7 @@ Color norms :toctree: _as_gen/ :template: autosummary.rst + Norm Normalize NoNorm AsinhNorm diff --git a/lib/matplotlib/colorizer.py b/lib/matplotlib/colorizer.py index b4223f389804..92a6e4ea4c4f 100644 --- a/lib/matplotlib/colorizer.py +++ b/lib/matplotlib/colorizer.py @@ -90,7 +90,7 @@ def norm(self): @norm.setter def norm(self, norm): - _api.check_isinstance((colors.Normalize, str, None), norm=norm) + _api.check_isinstance((colors.Norm, str, None), norm=norm) if norm is None: norm = colors.Normalize() elif isinstance(norm, str): diff --git a/lib/matplotlib/colorizer.pyi b/lib/matplotlib/colorizer.pyi index f35ebe5295e4..9a5a73415d83 100644 --- a/lib/matplotlib/colorizer.pyi +++ b/lib/matplotlib/colorizer.pyi @@ -10,12 +10,12 @@ class Colorizer: def __init__( self, cmap: str | colors.Colormap | None = ..., - norm: str | colors.Normalize | None = ..., + norm: str | colors.Norm | None = ..., ) -> None: ... @property - def norm(self) -> colors.Normalize: ... + def norm(self) -> colors.Norm: ... @norm.setter - def norm(self, norm: colors.Normalize | str | None) -> None: ... + def norm(self, norm: colors.Norm | str | None) -> None: ... def to_rgba( self, x: np.ndarray, @@ -63,10 +63,10 @@ class _ColorizerInterface: def get_cmap(self) -> colors.Colormap: ... def set_cmap(self, cmap: str | colors.Colormap) -> None: ... @property - def norm(self) -> colors.Normalize: ... + def norm(self) -> colors.Norm: ... @norm.setter - def norm(self, norm: colors.Normalize | str | None) -> None: ... - def set_norm(self, norm: colors.Normalize | str | None) -> None: ... + def norm(self, norm: colors.Norm | str | None) -> None: ... + def set_norm(self, norm: colors.Norm | str | None) -> None: ... def autoscale(self) -> None: ... def autoscale_None(self) -> None: ... @@ -74,7 +74,7 @@ class _ColorizerInterface: class _ScalarMappable(_ColorizerInterface): def __init__( self, - norm: colors.Normalize | None = ..., + norm: colors.Norm | None = ..., cmap: str | colors.Colormap | None = ..., *, colorizer: Colorizer | None = ..., diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 254e2c1a203b..a09b4f3d4f5c 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -41,6 +41,7 @@ import base64 from collections.abc import Sequence, Mapping +from abc import ABC, abstractmethod import functools import importlib import inspect @@ -2257,7 +2258,87 @@ def _init(self): self._isinit = True -class Normalize: +class Norm(ABC): + """ + Abstract base class for normalizations. + + Subclasses include `Normalize` which maps from a scalar to + a scalar. However, this class makes no such requirement, and subclasses may + support the normalization of multiple variates simultaneously, with + separate normalization for each variate. + """ + + def __init__(self): + self.callbacks = cbook.CallbackRegistry(signals=["changed"]) + + @property + @abstractmethod + def vmin(self): + """Lower limit of the input data interval; maps to 0.""" + pass + + @property + @abstractmethod + def vmax(self): + """Upper limit of the input data interval; maps to 1.""" + pass + + @property + @abstractmethod + def clip(self): + """ + Determines the behavior for mapping values outside the range ``[vmin, vmax]``. + + See the *clip* parameter in `.Normalize`. + """ + pass + + @abstractmethod + def __call__(self, value, clip=None): + """ + Normalize the data and return the normalized data. + + Parameters + ---------- + value + Data to normalize. + clip : bool, optional + See the description of the parameter *clip* in `.Normalize`. + + If ``None``, defaults to ``self.clip`` (which defaults to + ``False``). + + Notes + ----- + If not already initialized, ``self.vmin`` and ``self.vmax`` are + initialized using ``self.autoscale_None(value)``. + """ + pass + + @abstractmethod + def autoscale(self, A): + """Set *vmin*, *vmax* to min, max of *A*.""" + pass + + @abstractmethod + def autoscale_None(self, A): + """If *vmin* or *vmax* are not set, use the min/max of *A* to set them.""" + pass + + @abstractmethod + def scaled(self): + """Return whether *vmin* and *vmax* are both set.""" + pass + + def _changed(self): + """ + Call this whenever the norm is changed to notify all the + callback listeners to the 'changed' signal. + """ + self.callbacks.process('changed') + + +class Normalize(Norm): """ A class which, when called, maps values within the interval ``[vmin, vmax]`` linearly to the interval ``[0.0, 1.0]``. The mapping of @@ -2307,15 +2388,15 @@ def __init__(self, vmin=None, vmax=None, clip=False): ----- If ``vmin == vmax``, input data will be mapped to 0. """ + super().__init__() self._vmin = _sanitize_extrema(vmin) self._vmax = _sanitize_extrema(vmax) self._clip = clip self._scale = None - self.callbacks = cbook.CallbackRegistry(signals=["changed"]) @property def vmin(self): - """Lower limit of the input data interval; maps to 0.""" + # docstring inherited return self._vmin @vmin.setter @@ -2327,7 +2408,7 @@ def vmin(self, value): @property def vmax(self): - """Upper limit of the input data interval; maps to 1.""" + # docstring inherited return self._vmax @vmax.setter @@ -2339,11 +2420,7 @@ def vmax(self, value): @property def clip(self): - """ - Determines the behavior for mapping values outside the range ``[vmin, vmax]``. - - See the *clip* parameter in `.Normalize`. - """ + # docstring inherited return self._clip @clip.setter @@ -2352,13 +2429,6 @@ def clip(self, value): self._clip = value self._changed() - def _changed(self): - """ - Call this whenever the norm is changed to notify all the - callback listeners to the 'changed' signal. - """ - self.callbacks.process('changed') - @staticmethod def process_value(value): """ @@ -2400,24 +2470,7 @@ def process_value(value): return result, is_scalar def __call__(self, value, clip=None): - """ - Normalize the data and return the normalized data. - - Parameters - ---------- - value - Data to normalize. - clip : bool, optional - See the description of the parameter *clip* in `.Normalize`. - - If ``None``, defaults to ``self.clip`` (which defaults to - ``False``). - - Notes - ----- - If not already initialized, ``self.vmin`` and ``self.vmax`` are - initialized using ``self.autoscale_None(value)``. - """ + # docstring inherited if clip is None: clip = self.clip @@ -2468,7 +2521,7 @@ def inverse(self, value): return vmin + value * (vmax - vmin) def autoscale(self, A): - """Set *vmin*, *vmax* to min, max of *A*.""" + # docstring inherited with self.callbacks.blocked(): # Pause callbacks while we are updating so we only get # a single update signal at the end @@ -2477,7 +2530,7 @@ def autoscale(self, A): self._changed() def autoscale_None(self, A): - """If *vmin* or *vmax* are not set, use the min/max of *A* to set them.""" + # docstring inherited A = np.asanyarray(A) if isinstance(A, np.ma.MaskedArray): @@ -2491,7 +2544,7 @@ def autoscale_None(self, A): self.vmax = A.max() def scaled(self): - """Return whether *vmin* and *vmax* are both set.""" + # docstring inherited return self.vmin is not None and self.vmax is not None @@ -2775,7 +2828,7 @@ def _make_norm_from_scale( unlike to arbitrary lambdas. """ - class Norm(base_norm_cls): + class ScaleNorm(base_norm_cls): def __reduce__(self): cls = type(self) # If the class is toplevel-accessible, it is possible to directly @@ -2855,15 +2908,15 @@ def autoscale_None(self, A): return super().autoscale_None(in_trf_domain) if base_norm_cls is Normalize: - Norm.__name__ = f"{scale_cls.__name__}Norm" - Norm.__qualname__ = f"{scale_cls.__qualname__}Norm" + ScaleNorm.__name__ = f"{scale_cls.__name__}Norm" + ScaleNorm.__qualname__ = f"{scale_cls.__qualname__}Norm" else: - Norm.__name__ = base_norm_cls.__name__ - Norm.__qualname__ = base_norm_cls.__qualname__ - Norm.__module__ = base_norm_cls.__module__ - Norm.__doc__ = base_norm_cls.__doc__ + ScaleNorm.__name__ = base_norm_cls.__name__ + ScaleNorm.__qualname__ = base_norm_cls.__qualname__ + ScaleNorm.__module__ = base_norm_cls.__module__ + ScaleNorm.__doc__ = base_norm_cls.__doc__ - return Norm + return ScaleNorm def _create_empty_object_of_class(cls): diff --git a/lib/matplotlib/colors.pyi b/lib/matplotlib/colors.pyi index eadd759bcaa3..cdc6e5e7d89f 100644 --- a/lib/matplotlib/colors.pyi +++ b/lib/matplotlib/colors.pyi @@ -1,4 +1,5 @@ from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence +from abc import ABC, abstractmethod from matplotlib import cbook, scale import re @@ -249,8 +250,29 @@ class BivarColormapFromImage(BivarColormap): origin: Sequence[float] = ..., name: str = ... ) -> None: ... -class Normalize: +class Norm(ABC): callbacks: cbook.CallbackRegistry + def __init__(self) -> None: ... + @property + @abstractmethod + def vmin(self) -> float | tuple[float] | None: ... + @property + @abstractmethod + def vmax(self) -> float | tuple[float] | None: ... + @property + @abstractmethod + def clip(self) -> bool | tuple[bool]: ... + @abstractmethod + def __call__(self, value: np.ndarray, clip: bool | None = ...) -> ArrayLike: ... + @abstractmethod + def autoscale(self, A: ArrayLike) -> None: ... + @abstractmethod + def autoscale_None(self, A: ArrayLike) -> None: ... + @abstractmethod + def scaled(self) -> bool: ... + + +class Normalize(Norm): def __init__( self, vmin: float | None = ..., vmax: float | None = ..., clip: bool = ... ) -> None: ... diff --git a/lib/matplotlib/tests/baseline_images/test_colors/test_norm_abc.png b/lib/matplotlib/tests/baseline_images/test_colors/test_norm_abc.png new file mode 100644 index 0000000000000000000000000000000000000000..077365674ac27803460ddb533d5efa09ea7ef9c7 GIT binary patch literal 16129 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 m@y{*)?+fJr!spj} Date: Sat, 28 Jun 2025 13:04:07 +0100 Subject: [PATCH 27/50] Fix polar inner patch boundary and spine location for log scale --- lib/matplotlib/projections/polar.py | 8 +++++++- lib/matplotlib/spines.py | 8 +++++++- lib/matplotlib/tests/test_polar.py | 20 ++++++++++++++++++++ lib/matplotlib/tests/test_spines.py | 12 ++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index 948b3a6e704f..8fdb31b4256e 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -817,6 +817,10 @@ def _init_axis(self): self.xaxis = ThetaAxis(self, clear=False) self.yaxis = RadialAxis(self, clear=False) self.spines['polar'].register_axis(self.yaxis) + inner_spine = self.spines.get('inner', None) + if inner_spine is not None: + # Subclasses may not have inner spine. + inner_spine.register_axis(self.yaxis) def _set_lim_and_transforms(self): # A view limit where the minimum radius can be locked if the user @@ -961,7 +965,9 @@ def draw(self, renderer): thetamin, thetamax = np.rad2deg(self._realViewLim.intervalx) if thetamin > thetamax: thetamin, thetamax = thetamax, thetamin - rmin, rmax = ((self._realViewLim.intervaly - self.get_rorigin()) * + rscale_tr = self.yaxis.get_transform() + rmin, rmax = ((rscale_tr.transform(self._realViewLim.intervaly) - + rscale_tr.transform(self.get_rorigin())) * self.get_rsign()) if isinstance(self.patch, mpatches.Wedge): # Backwards-compatibility: Any subclassed Axes might override the diff --git a/lib/matplotlib/spines.py b/lib/matplotlib/spines.py index 7e77a393f2a2..9732a2f3347a 100644 --- a/lib/matplotlib/spines.py +++ b/lib/matplotlib/spines.py @@ -265,11 +265,17 @@ def _adjust_location(self): self._path = mpath.Path.arc(np.rad2deg(low), np.rad2deg(high)) if self.spine_type == 'bottom': - rmin, rmax = self.axes.viewLim.intervaly + if self.axis is None: + tr = mtransforms.IdentityTransform() + else: + tr = self.axis.get_transform() + rmin, rmax = tr.transform(self.axes.viewLim.intervaly) try: rorigin = self.axes.get_rorigin() except AttributeError: rorigin = rmin + else: + rorigin = tr.transform(rorigin) scaled_diameter = (rmin - rorigin) / (rmax - rorigin) self._height = scaled_diameter self._width = scaled_diameter diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index 31e8cdd89a21..c0bf72b89eb0 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -482,6 +482,26 @@ def test_polar_log(): ax.plot(np.linspace(0, 2 * np.pi, n), np.logspace(0, 2, n)) +@check_figures_equal() +def test_polar_log_rorigin(fig_ref, fig_test): + # Test that equivalent linear and log radial settings give the same axes patch + # and spines. + ax_ref = fig_ref.add_subplot(projection='polar', facecolor='red') + ax_ref.set_rlim(0, 2) + ax_ref.set_rorigin(-3) + ax_ref.set_rticks(np.linspace(0, 2, 5)) + + ax_test = fig_test.add_subplot(projection='polar', facecolor='red') + ax_test.set_rscale('log') + ax_test.set_rlim(1, 100) + ax_test.set_rorigin(10**-3) + ax_test.set_rticks(np.logspace(0, 2, 5)) + + for ax in ax_ref, ax_test: + # Radial tick labels should be the only difference, so turn them off. + ax.tick_params(labelleft=False) + + def test_polar_neg_theta_lims(): fig = plt.figure() ax = fig.add_subplot(projection='polar') diff --git a/lib/matplotlib/tests/test_spines.py b/lib/matplotlib/tests/test_spines.py index 353aede00298..d6ddcabb6878 100644 --- a/lib/matplotlib/tests/test_spines.py +++ b/lib/matplotlib/tests/test_spines.py @@ -154,3 +154,15 @@ def test_spines_black_axes(): ax.set_xticks([]) ax.set_yticks([]) ax.set_facecolor((0, 0, 0)) + + +def test_arc_spine_inner_no_axis(): + # Backcompat: smoke test that inner arc spine does not need a registered + # axis in order to be drawn + fig = plt.figure() + ax = fig.add_subplot(projection="polar") + inner_spine = ax.spines["inner"] + inner_spine.register_axis(None) + assert ax.spines["inner"].axis is None + + fig.draw_without_rendering() From 6ede069136c6e9c7f1bceb9b1d4e264ba928aa55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Mon, 30 Jun 2025 13:41:34 +0200 Subject: [PATCH 28/50] fixup Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/backends/backend_pdf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 4429dc9ba707..a75a8a86eb92 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1784,8 +1784,7 @@ def _writeImg(self, data, id, smask=None): data[:, :, 2]) indices = np.argsort(palette24).astype(np.uint8) rgb8 = indices[np.searchsorted(palette24, rgb24, sorter=indices)] - img = Image.fromarray(rgb8) - img.convert("P") + img = Image.fromarray(rgb8).convert("P") img.putpalette(palette) png_data, bit_depth, palette = self._writePng(img) if bit_depth is None or palette is None: From 0e430b9b19f6cdcdbfc583392f6887686a47b74c Mon Sep 17 00:00:00 2001 From: ZPyrolink <73246085+ZPyrolink@users.noreply.github.com> Date: Mon, 30 Jun 2025 21:43:52 +0200 Subject: [PATCH 29/50] Add explicit signatures for pyplot.{polar,savefig,set_loglevel} (#30200) * Create tests (cherry picked from commit 608b51fd6321ac07133b8d66a14e15a906e21169) * Update test_pyplot.py to include type on signature (cherry picked from commit 4ea0ff8e50f3a2460d18694aa1d58e7757c8726a) * Update polar and set_loglevel signature on pyplot.py (cherry picked from commit 41b701b41858cb2868485ca9f5747db4cd1f6d4a) * Update savefig signature on pyplot.py (cherry picked from commit b863ba298abe37c08c92f1ac1afc41f985d0bbff) * Add type hint on polar and set_loglevel on pyplot.py. Correct polar content (cherry picked from commit 92dc04501bab539586cac48a3266891c75a4cb7c) * Format with ruff (cherry picked from commit 64e7921b0b3f56c88c1f449a4f2081e862289279) * Revert polar on pyplot.py and remove corresponding test * Remove extra work when stub file doesn't exists Co-authored-by: Elliott Sales de Andrade * Replace assert_signatures_identical (check return_annotation and full parameters) with assert_same_signature (only check len(parameters), names and kinds) * Remove unused import * Renaming assert_signature arguments * Correct typo and ruff error --------- Co-authored-by: Corenthin ZOZOR Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/pyplot.py | 10 +++++----- lib/matplotlib/tests/test_pyplot.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index d77b06115268..8c9d1e1e5a29 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -50,7 +50,7 @@ import sys import threading import time -from typing import TYPE_CHECKING, cast, overload +from typing import IO, TYPE_CHECKING, cast, overload from cycler import cycler # noqa: F401 import matplotlib @@ -338,8 +338,8 @@ def uninstall_repl_displayhook() -> None: # Ensure this appears in the pyplot docs. @_copy_docstring_and_deprecators(matplotlib.set_loglevel) -def set_loglevel(*args, **kwargs) -> None: - return matplotlib.set_loglevel(*args, **kwargs) +def set_loglevel(level: str) -> None: + return matplotlib.set_loglevel(level) @_copy_docstring_and_deprecators(Artist.findobj) @@ -1259,11 +1259,11 @@ def draw() -> None: @_copy_docstring_and_deprecators(Figure.savefig) -def savefig(*args, **kwargs) -> None: +def savefig(fname: str | os.PathLike | IO, **kwargs) -> None: fig = gcf() # savefig default implementation has no return, so mypy is unhappy # presumably this is here because subclasses can return? - res = fig.savefig(*args, **kwargs) # type: ignore[func-returns-value] + res = fig.savefig(fname, **kwargs) # type: ignore[func-returns-value] fig.canvas.draw_idle() # Need this if 'transparent=True', to reset colors. return res diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index ab713707bace..55f7c33cb52e 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -1,4 +1,5 @@ import difflib +import inspect import numpy as np import sys @@ -484,3 +485,26 @@ def test_matshow(): # Smoke test that matshow does not ask for a new figsize on the existing figure plt.matshow(arr, fignum=fig.number) + + +def assert_same_signature(func1, func2): + """ + Assert that `func1` and `func2` have the same arguments, + i.e. same parameter count, names and kinds. + + :param func1: First function to check + :param func2: Second function to check + """ + params1 = inspect.signature(func1).parameters + params2 = inspect.signature(func2).parameters + + assert len(params1) == len(params2) + assert all([ + params1[p].name == params2[p].name and + params1[p].kind == params2[p].kind + for p in params1 + ]) + + +def test_setloglevel_signature(): + assert_same_signature(plt.set_loglevel, mpl.set_loglevel) From 4c06e718d4a28d14586b38fcad8d3734e901e719 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 30 Jun 2025 23:47:56 +0000 Subject: [PATCH 30/50] Bump github/codeql-action from 3.29.0 to 3.29.2 in the actions group Bumps the actions group with 1 update: [github/codeql-action](https://github.com/github/codeql-action). Updates `github/codeql-action` from 3.29.0 to 3.29.2 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/ce28f5bb42b7a9f2c824e633a3f6ee835bab6858...181d5eefc20863364f96762470ba6f862bdef56b) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.29.2 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: actions ... Signed-off-by: dependabot[bot] --- .github/workflows/codeql-analysis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index c05454884b55..d6d1eba02560 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -32,7 +32,7 @@ jobs: persist-credentials: false - name: Initialize CodeQL - uses: github/codeql-action/init@ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 # v3.29.0 + uses: github/codeql-action/init@181d5eefc20863364f96762470ba6f862bdef56b # v3.29.2 with: languages: ${{ matrix.language }} @@ -43,4 +43,4 @@ jobs: pip install --user -v . - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 # v3.29.0 + uses: github/codeql-action/analyze@181d5eefc20863364f96762470ba6f862bdef56b # v3.29.2 From b5235812a83c7eee9901757320c70120caea3d1e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 1 Jul 2025 12:47:28 +0200 Subject: [PATCH 31/50] Copy-edit the docstring of AuxTransformBox. It can like be made even clearer; here I tried to just repeat what was written but with a clearer style. Some minor additional edits are included as well. --- lib/matplotlib/offsetbox.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index 1e07125cdc2a..974cc4f2db05 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -201,7 +201,7 @@ def _get_aligned_offsets(yspans, height, align="baseline"): class OffsetBox(martist.Artist): """ - The OffsetBox is a simple container artist. + A simple container artist. The child artists are meant to be drawn at a relative position to its parent. @@ -826,17 +826,18 @@ def draw(self, renderer): class AuxTransformBox(OffsetBox): """ - Offset Box with the aux_transform. Its children will be - transformed with the aux_transform first then will be - offsetted. The absolute coordinate of the aux_transform is meaning - as it will be automatically adjust so that the left-lower corner - of the bounding box of children will be set to (0, 0) before the - offset transform. - - It is similar to drawing area, except that the extent of the box - is not predetermined but calculated from the window extent of its - children. Furthermore, the extent of the children will be - calculated in the transformed coordinate. + An OffsetBox with an auxiliary transform. + + All child artists are first transformed with *aux_transform*, then + translated with an offset (the same for all children) so the bounding + box of the children matches the drawn box. (In other words, adding an + arbitrary translation to *aux_transform* has no effect as it will be + cancelled out by the later offsetting.) + + `AuxTransformBox` is similar to `.DrawingArea`, except that the extent of + the box is not predetermined but calculated from the window extent of its + children, and the extent of the children will be calculated in the + transformed coordinate. """ def __init__(self, aux_transform): self.aux_transform = aux_transform @@ -853,10 +854,7 @@ def add_artist(self, a): self.stale = True def get_transform(self): - """ - Return the :class:`~matplotlib.transforms.Transform` applied - to the children - """ + """Return the `.Transform` applied to the children.""" return (self.aux_transform + self.ref_offset_transform + self.offset_transform) @@ -908,7 +906,7 @@ def draw(self, renderer): class AnchoredOffsetbox(OffsetBox): """ - An offset box placed according to location *loc*. + An OffsetBox placed according to location *loc*. AnchoredOffsetbox has a single child. When multiple children are needed, use an extra OffsetBox to enclose them. By default, the offset box is From 1295158096805f6675cbb42f982ff6c1402e9ba7 Mon Sep 17 00:00:00 2001 From: Roman A <121314722+GameRoMan@users.noreply.github.com> Date: Tue, 1 Jul 2025 16:13:17 +0100 Subject: [PATCH 32/50] Add explicit `**options: Any` for `add_subplot` method to remove "partially unknown type" warnings from type checkers like Pyright and Pylance --- lib/matplotlib/figure.pyi | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index e7c5175d8af9..61dc79619a80 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -89,19 +89,20 @@ class FigureBase(Artist): # TODO: docstring indicates SubplotSpec a valid arg, but none of the listed signatures appear to be that @overload - def add_subplot(self, *args, projection: Literal["3d"], **kwargs) -> Axes3D: ... + def add_subplot(self, *args: Any, projection: Literal["3d"], **kwargs: Any) -> Axes3D: ... @overload def add_subplot( - self, nrows: int, ncols: int, index: int | tuple[int, int], **kwargs + self, nrows: int, ncols: int, index: int | tuple[int, int], **kwargs: Any ) -> Axes: ... @overload - def add_subplot(self, pos: int, **kwargs) -> Axes: ... + def add_subplot(self, pos: int, **kwargs: Any) -> Axes: ... @overload - def add_subplot(self, ax: Axes, **kwargs) -> Axes: ... + def add_subplot(self, ax: Axes, **kwargs: Any) -> Axes: ... @overload - def add_subplot(self, ax: SubplotSpec, **kwargs) -> Axes: ... + def add_subplot(self, ax: SubplotSpec, **kwargs: Any) -> Axes: ... @overload - def add_subplot(self, **kwargs) -> Axes: ... + def add_subplot(self, **kwargs: Any) -> Axes: ... + @overload def subplots( self, From c8361f0a33c8e88abf8f3862dce8388bd1a00eac Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Wed, 2 Jul 2025 07:36:04 +0200 Subject: [PATCH 33/50] DOC: Recommend to use bare Figure instances for saving to file --- doc/users/faq.rst | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/users/faq.rst b/doc/users/faq.rst index b08bd75cee4e..c6bbc5ca8d87 100644 --- a/doc/users/faq.rst +++ b/doc/users/faq.rst @@ -281,8 +281,23 @@ locators as desired because the two axes are independent. Generate images without having a window appear ---------------------------------------------- -Simply do not call `~matplotlib.pyplot.show`, and directly save the figure to -the desired format:: +The recommended approach since matplotlib 3.1 is to explicitly create a Figure +instance:: + + from matplotlib.figure import Figure + fig = Figure() + ax = fig.subplots() + ax.plot([1, 2, 3]) + fig.savefig('myfig.png') + +This prevents any interaction with GUI frameworks and the window manager. + +It's alternatively still possible to use the pyplot interface. Instead of +calling `matplotlib.pyplot.show`, call `matplotlib.pyplot.savefig`. + +Additionally, you must ensure to close the figure after saving it. Not +closing the figure is a memory leak, because pyplot keeps references +to all not-yet-shown figures:: import matplotlib.pyplot as plt plt.plot([1, 2, 3]) From 919c7b9c440fe97393ee8e15ff35ddc6a338c5c6 Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Wed, 2 Jul 2025 08:34:51 +0200 Subject: [PATCH 34/50] adjust logic to allow inherritance --- lib/matplotlib/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 8ffd002d43bc..8f672af70ebc 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -799,13 +799,13 @@ def find_all(self, pattern): """ pattern_re = re.compile(pattern) - return RcParams((key, value) - for key, value in self.items() - if pattern_re.search(key)) + return self.__class__( + (key, value) for key, value in self.items() if pattern_re.search(key) + ) def copy(self): """Copy this RcParams instance.""" - rccopy = RcParams() + rccopy = self.__class__() for k in self: # Skip deprecations and revalidation. rccopy._set(k, self._get(k)) return rccopy From 77f868cbcc5e7cd6e8860fd5eb7934eb9397181c Mon Sep 17 00:00:00 2001 From: ianlv Date: Wed, 2 Jul 2025 15:54:25 +0800 Subject: [PATCH 35/50] chore: remove redundant words in comment Signed-off-by: ianlv --- doc/devel/tag_guidelines.rst | 2 +- doc/users/prev_whats_new/whats_new_3.8.0.rst | 4 ++-- galleries/examples/scales/custom_scale.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/devel/tag_guidelines.rst b/doc/devel/tag_guidelines.rst index 2c80065982bc..2ff77d5279d5 100644 --- a/doc/devel/tag_guidelines.rst +++ b/doc/devel/tag_guidelines.rst @@ -61,7 +61,7 @@ Proposing new tags 1. Review existing tag list, looking out for similar entries (i.e. ``axes`` and ``axis``). 2. If a relevant tag or subcategory does not yet exist, propose it. Each tag is two parts: ``subcategory: tag``. Tags should be one or two words. -3. New tags should be be added when they are relevant to existing gallery entries too. +3. New tags should be added when they are relevant to existing gallery entries too. Avoid tags that will link to only a single gallery entry. 4. Tags can recreate other forms of organization. diff --git a/doc/users/prev_whats_new/whats_new_3.8.0.rst b/doc/users/prev_whats_new/whats_new_3.8.0.rst index 88f987172adb..fe1d5f7a7952 100644 --- a/doc/users/prev_whats_new/whats_new_3.8.0.rst +++ b/doc/users/prev_whats_new/whats_new_3.8.0.rst @@ -359,7 +359,7 @@ The following delimiter names have been supported earlier, but can now be sized * ``\leftparen`` and ``\rightparen`` There are really no obvious advantages in using these. -Instead, they are are added for completeness. +Instead, they are added for completeness. ``mathtext`` documentation improvements --------------------------------------- @@ -513,7 +513,7 @@ Plot Directive now can make responsive images with "srcset" The plot sphinx directive (``matplotlib.sphinxext.plot_directive``, invoked in rst as ``.. plot::``) can be configured to automatically make higher res -figures and add these to the the built html docs. In ``conf.py``:: +figures and add these to the built html docs. In ``conf.py``:: extensions = [ ... diff --git a/galleries/examples/scales/custom_scale.py b/galleries/examples/scales/custom_scale.py index 0eedb16ec5cf..1b6bdd6f3e09 100644 --- a/galleries/examples/scales/custom_scale.py +++ b/galleries/examples/scales/custom_scale.py @@ -22,7 +22,7 @@ * You want to override the default locators and formatters for the axis (``set_default_locators_and_formatters`` below). - * You want to limit the range of the the axis (``limit_range_for_scale`` below). + * You want to limit the range of the axis (``limit_range_for_scale`` below). """ From 70d5ad48fc26045739d5c57538686f7619cfa8bb Mon Sep 17 00:00:00 2001 From: N R Navaneet <156576749+nrnavaneet@users.noreply.github.com> Date: Thu, 3 Jul 2025 10:44:51 +0530 Subject: [PATCH 36/50] Fix FancyArrow rendering for zero-length arrows (#30243) * WIP: changed FancyArrow empty logic * Update test_patches.py * Update test_patches.py * Fix crash when drawing FancyArrow with zero length * Update patches.py * Delete .python-version --- lib/matplotlib/patches.py | 2 +- lib/matplotlib/tests/test_patches.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 63453d416b99..477eee9f5a7a 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -1538,7 +1538,7 @@ def _make_verts(self): length = distance else: length = distance + head_length - if not length: + if np.size(length) == 0: self.verts = np.empty([0, 2]) # display nothing if empty else: # start by drawing horizontal arrow, point at (0, 0) diff --git a/lib/matplotlib/tests/test_patches.py b/lib/matplotlib/tests/test_patches.py index 4ed9222eb95e..d69a9dad4337 100644 --- a/lib/matplotlib/tests/test_patches.py +++ b/lib/matplotlib/tests/test_patches.py @@ -1093,3 +1093,9 @@ def test_facecolor_none_edgecolor_force_edgecolor(): rcParams['patch.edgecolor'] = 'red' rect = Rectangle((0, 0), 1, 1, facecolor="none") assert mcolors.same_color(rect.get_edgecolor(), rcParams['patch.edgecolor']) + + +def test_empty_fancyarrow(): + fig, ax = plt.subplots() + arrow = ax.arrow([], [], [], []) + assert arrow is not None From a87684faa62f86e65da21bfb0942046e5e59b55f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jouni=20K=2E=20Sepp=C3=A4nen?= Date: Tue, 1 Jul 2025 19:28:51 +0300 Subject: [PATCH 37/50] Upgrade to Visual Studio 2022 in appveyor.yml --- .appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.appveyor.yml b/.appveyor.yml index a637fe545466..c3fcb0ea9591 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -17,7 +17,7 @@ skip_commits: clone_depth: 50 -image: Visual Studio 2019 +image: Visual Studio 2022 environment: From d231a25efa0764fc9cbe2e2f4e7aa3f56cb75b7c Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 1 Jul 2025 05:13:45 -0400 Subject: [PATCH 38/50] Don't expose private styles in style.available They remain in `style.library`, because that's how we look them up, but this prevents them being exposed as something someone might use. Also, fix `reload_library`, which was accidentally modifying the original base library information each time. Fixes itprojects/MasVisGtk#13 --- lib/matplotlib/style/__init__.py | 7 ++++--- lib/matplotlib/tests/test_style.py | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/style/__init__.py b/lib/matplotlib/style/__init__.py index a202cfe08b20..80c6de00a18d 100644 --- a/lib/matplotlib/style/__init__.py +++ b/lib/matplotlib/style/__init__.py @@ -6,7 +6,8 @@ ``context`` Context manager to use a style sheet temporarily. ``available`` - List available style sheets. + List available style sheets. Underscore-prefixed names are considered private and + not listed, though may still be accessed directly from ``library``. ``library`` A dictionary of style names and matplotlib settings. """ @@ -245,8 +246,8 @@ def update_nested_dict(main_dict, new_dict): def reload_library(): """Reload the style library.""" library.clear() - library.update(_update_user_library(_base_library)) - available[:] = sorted(library.keys()) + library.update(_update_user_library(_base_library.copy())) + available[:] = sorted(name for name in library if not name.startswith('_')) reload_library() diff --git a/lib/matplotlib/tests/test_style.py b/lib/matplotlib/tests/test_style.py index 4d76a4ecfa8b..7b54f1141720 100644 --- a/lib/matplotlib/tests/test_style.py +++ b/lib/matplotlib/tests/test_style.py @@ -21,6 +21,7 @@ def temp_style(style_name, settings=None): if not settings: settings = DUMMY_SETTINGS temp_file = f'{style_name}.mplstyle' + orig_library_paths = style.USER_LIBRARY_PATHS try: with TemporaryDirectory() as tmpdir: # Write style settings to file in the tmpdir. @@ -32,6 +33,7 @@ def temp_style(style_name, settings=None): style.reload_library() yield finally: + style.USER_LIBRARY_PATHS = orig_library_paths style.reload_library() @@ -46,8 +48,17 @@ def test_invalid_rc_warning_includes_filename(caplog): def test_available(): - with temp_style('_test_', DUMMY_SETTINGS): - assert '_test_' in style.available + # Private name should not be listed in available but still usable. + assert '_classic_test_patch' not in style.available + assert '_classic_test_patch' in style.library + + with temp_style('_test_', DUMMY_SETTINGS), temp_style('dummy', DUMMY_SETTINGS): + assert 'dummy' in style.available + assert 'dummy' in style.library + assert '_test_' not in style.available + assert '_test_' in style.library + assert 'dummy' not in style.available + assert '_test_' not in style.available def test_use(): From 6a01311f9181f3ba1801034cb81b31a427deae03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jouni=20K=2E=20Sepp=C3=A4nen?= Date: Thu, 3 Jul 2025 19:37:49 +0300 Subject: [PATCH 39/50] Time out in _get_executable_info Time out after 30 seconds. This is used for version queries which should be very fast, so a 30-second delay would be unusual. GitHub Actions test runs have been hanging trying to get the inkscape version when using Python 3.14: https://github.com/matplotlib/matplotlib/actions/runs/16043158943/job/45268507848#step:13:836 --- lib/matplotlib/__init__.py | 5 ++++- lib/matplotlib/tests/test_matplotlib.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 8f672af70ebc..e98e8ea07502 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -400,12 +400,15 @@ def impl(args, regex, min_ver=None, ignore_exit_code=False): try: output = subprocess.check_output( args, stderr=subprocess.STDOUT, - text=True, errors="replace") + text=True, errors="replace", timeout=30) except subprocess.CalledProcessError as _cpe: if ignore_exit_code: output = _cpe.output else: raise ExecutableNotFoundError(str(_cpe)) from _cpe + except subprocess.TimeoutExpired as _te: + msg = f"Timed out running {cbook._pformat_subprocess(args)}" + raise ExecutableNotFoundError(msg) from _te except OSError as _ose: raise ExecutableNotFoundError(str(_ose)) from _ose match = re.search(regex, output) diff --git a/lib/matplotlib/tests/test_matplotlib.py b/lib/matplotlib/tests/test_matplotlib.py index 37b41fafdb78..d0a3f8c617e1 100644 --- a/lib/matplotlib/tests/test_matplotlib.py +++ b/lib/matplotlib/tests/test_matplotlib.py @@ -1,6 +1,7 @@ import os import subprocess import sys +from unittest.mock import patch import pytest @@ -80,3 +81,16 @@ def test_importable_with__OO(): [sys.executable, "-OO", "-c", program], env={**os.environ, "MPLBACKEND": ""}, check=True ) + + +@patch('matplotlib.subprocess.check_output') +def test_get_executable_info_timeout(mock_check_output): + """ + Test that _get_executable_info raises ExecutableNotFoundError if the + command times out. + """ + + mock_check_output.side_effect = subprocess.TimeoutExpired(cmd=['mock'], timeout=30) + + with pytest.raises(matplotlib.ExecutableNotFoundError, match='Timed out'): + matplotlib._get_executable_info.__wrapped__('inkscape') From bf96be4bc0a53e947b6993238b61be4081461cae Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 3 Jul 2025 23:52:36 -0400 Subject: [PATCH 40/50] Fix linting in test notebooks --- .../backends/web_backend/nbagg_uat.ipynb | 27 ++++++++++--------- pyproject.toml | 3 +-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/backends/web_backend/nbagg_uat.ipynb b/lib/matplotlib/backends/web_backend/nbagg_uat.ipynb index e9fc62bc2883..0513fee2b54c 100644 --- a/lib/matplotlib/backends/web_backend/nbagg_uat.ipynb +++ b/lib/matplotlib/backends/web_backend/nbagg_uat.ipynb @@ -309,7 +309,7 @@ "metadata": {}, "outputs": [], "source": [ - "from matplotlib.backends.backend_nbagg import new_figure_manager,show\n", + "from matplotlib.backends.backend_nbagg import new_figure_manager\n", "\n", "manager = new_figure_manager(1000)\n", "fig = manager.canvas.figure\n", @@ -341,15 +341,18 @@ "x = np.arange(0, 2*np.pi, 0.01) # x-array\n", "line, = ax.plot(x, np.sin(x))\n", "\n", + "\n", "def animate(i):\n", " line.set_ydata(np.sin(x+i/10.0)) # update the data\n", " return line,\n", "\n", - "#Init only required for blitting to give a clean slate.\n", + "\n", + "# Init only required for blitting to give a clean slate.\n", "def init():\n", " line.set_ydata(np.ma.array(x, mask=True))\n", " return line,\n", "\n", + "\n", "ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,\n", " interval=100., blit=True)\n", "plt.show()" @@ -405,6 +408,8 @@ "ln, = ax.plot(x,y)\n", "evt = []\n", "colors = iter(itertools.cycle(['r', 'g', 'b', 'k', 'c']))\n", + "\n", + "\n", "def on_event(event):\n", " if event.name.startswith('key'):\n", " fig.suptitle('%s: %s' % (event.name, event.key))\n", @@ -417,6 +422,7 @@ " fig.canvas.draw()\n", " fig.canvas.draw_idle()\n", "\n", + "\n", "fig.canvas.mpl_connect('button_press_event', on_event)\n", "fig.canvas.mpl_connect('button_release_event', on_event)\n", "fig.canvas.mpl_connect('scroll_event', on_event)\n", @@ -448,10 +454,12 @@ "fig, ax = plt.subplots()\n", "text = ax.text(0.5, 0.5, '', ha='center')\n", "\n", + "\n", "def update(text):\n", " text.set(text=time.ctime())\n", " text.axes.figure.canvas.draw()\n", - " \n", + "\n", + "\n", "timer = fig.canvas.new_timer(500, [(update, [text], {})])\n", "timer.start()\n", "plt.show()" @@ -471,7 +479,7 @@ "outputs": [], "source": [ "fig, ax = plt.subplots()\n", - "text = ax.text(0.5, 0.5, '', ha='center') \n", + "text = ax.text(0.5, 0.5, '', ha='center')\n", "timer = fig.canvas.new_timer(500, [(update, [text], {})])\n", "\n", "timer.single_shot = True\n", @@ -578,11 +586,12 @@ "cnt = itertools.count()\n", "bg = None\n", "\n", + "\n", "def onclick_handle(event):\n", " \"\"\"Should draw elevating green line on each mouse click\"\"\"\n", " global bg\n", " if bg is None:\n", - " bg = ax.figure.canvas.copy_from_bbox(ax.bbox) \n", + " bg = ax.figure.canvas.copy_from_bbox(ax.bbox)\n", " ax.figure.canvas.restore_region(bg)\n", "\n", " cur_y = (next(cnt) % 10) * 0.1\n", @@ -590,6 +599,7 @@ " ax.draw_artist(ln)\n", " ax.figure.canvas.blit(ax.bbox)\n", "\n", + "\n", "fig, ax = plt.subplots()\n", "ax.plot([0, 1], [0, 1], 'r')\n", "ln, = ax.plot([0, 1], [0, 0], 'g', animated=True)\n", @@ -598,13 +608,6 @@ "\n", "ax.figure.canvas.mpl_connect('button_press_event', onclick_handle)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/pyproject.toml b/pyproject.toml index b980e512769a..ba2f9d29cf28 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -101,8 +101,6 @@ exclude = [ "tools/gh_api.py", ".tox", ".eggs", - # TODO: fix .ipynb files - "*.ipynb" ] line-length = 88 target-version = "py311" @@ -174,6 +172,7 @@ convention = "numpy" [tool.ruff.lint.per-file-ignores] "*.pyi" = ["E501"] +"*.ipynb" = ["E402"] "doc/conf.py" = ["E402"] "galleries/examples/animation/frame_grabbing_sgskip.py" = ["E402"] "galleries/examples/images_contours_and_fields/tricontour_demo.py" = ["E201"] From ca3d239bc8f2fcb52a459346bd682056dea9fdd1 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 4 Jul 2025 00:11:38 -0400 Subject: [PATCH 41/50] Clean up unused ruff config ruff uses `project.requires-python`, so the ruff-only `target-version` is not needed. Other exceptions can be dropped since they are all clear of warnings. --- pyproject.toml | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ba2f9d29cf28..8f8c73a3344f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,17 +93,13 @@ sections = "FUTURE,STDLIB,THIRDPARTY,PYDATA,FIRSTPARTY,LOCALFOLDER" force_sort_within_sections = true [tool.ruff] -exclude = [ - ".git", +extend-exclude = [ "build", "doc/gallery", "doc/tutorials", "tools/gh_api.py", - ".tox", - ".eggs", ] line-length = 88 -target-version = "py311" [tool.ruff.lint] ignore = [ @@ -129,9 +125,7 @@ ignore = [ "D404", "D413", "D415", - "D416", "D417", - "E24", "E266", "E305", "E306", @@ -174,14 +168,11 @@ convention = "numpy" "*.pyi" = ["E501"] "*.ipynb" = ["E402"] "doc/conf.py" = ["E402"] -"galleries/examples/animation/frame_grabbing_sgskip.py" = ["E402"] "galleries/examples/images_contours_and_fields/tricontour_demo.py" = ["E201"] "galleries/examples/images_contours_and_fields/tripcolor_demo.py" = ["E201"] "galleries/examples/images_contours_and_fields/triplot_demo.py" = ["E201"] "galleries/examples/lines_bars_and_markers/marker_reference.py" = ["E402"] -"galleries/examples/misc/print_stdout_sgskip.py" = ["E402"] "galleries/examples/misc/table_demo.py" = ["E201"] -"galleries/examples/style_sheets/bmh.py" = ["E501"] "galleries/examples/subplots_axes_and_figures/demo_constrained_layout.py" = ["E402"] "galleries/examples/text_labels_and_annotations/custom_legends.py" = ["E402"] "galleries/examples/ticks/date_concise_formatter.py" = ["E402"] @@ -210,11 +201,9 @@ convention = "numpy" "lib/mpl_toolkits/axisartist/angle_helper.py" = ["E221"] "lib/mpl_toolkits/mplot3d/proj3d.py" = ["E201"] -"galleries/users_explain/artists/paths.py" = ["E402"] "galleries/users_explain/quick_start.py" = ["E402"] "galleries/users_explain/artists/patheffects_guide.py" = ["E402"] -"galleries/users_explain/artists/transforms_tutorial.py" = ["E402", "E501"] -"galleries/users_explain/colors/colormaps.py" = ["E501"] +"galleries/users_explain/artists/transforms_tutorial.py" = ["E402"] "galleries/users_explain/colors/colors.py" = ["E402"] "galleries/tutorials/artists.py" = ["E402"] "galleries/users_explain/axes/constrainedlayout_guide.py" = ["E402"] @@ -224,7 +213,6 @@ convention = "numpy" "galleries/tutorials/images.py" = ["E501"] "galleries/tutorials/pyplot.py" = ["E402", "E501"] "galleries/users_explain/text/annotations.py" = ["E402", "E501"] -"galleries/users_explain/text/mathtext.py" = ["E501"] "galleries/users_explain/text/text_intro.py" = ["E402"] "galleries/users_explain/text/text_props.py" = ["E501"] From 18f93ee05c75985c17a9336c92c77bc03d719cd4 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 4 Jul 2025 00:43:21 -0400 Subject: [PATCH 42/50] Clean up mypy settings The removed excludes are covered by the first line. The Unpack feature was enabled by default in 1.9, which is our minimum. --- pyproject.toml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8f8c73a3344f..6202a386069c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -223,18 +223,12 @@ enable_error_code = [ "redundant-expr", "truthy-bool", ] -enable_incomplete_feature = [ - "Unpack", -] exclude = [ #stubtest ".*/matplotlib/(sphinxext|backends|pylab|testing/jpl_units)", #mypy precommit "galleries/", "doc/", - "lib/matplotlib/backends/", - "lib/matplotlib/sphinxext", - "lib/matplotlib/testing/jpl_units", "lib/mpl_toolkits/", #removing tests causes errors in backends "lib/matplotlib/tests/", From f4398bf068e49bcc39b3f6f70106b5cad0587cee Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 4 Jul 2025 05:00:44 -0400 Subject: [PATCH 43/50] Clean up some simple linting exceptions These examples only needed a single fix to remove the exception. --- galleries/examples/ticks/date_formatters_locators.py | 1 + galleries/tutorials/images.py | 4 ++-- pyproject.toml | 1 - 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/galleries/examples/ticks/date_formatters_locators.py b/galleries/examples/ticks/date_formatters_locators.py index 39492168242f..8c3b24bb4c26 100644 --- a/galleries/examples/ticks/date_formatters_locators.py +++ b/galleries/examples/ticks/date_formatters_locators.py @@ -12,6 +12,7 @@ import matplotlib.pyplot as plt import numpy as np +# While these appear unused directly, they are used from eval'd strings. from matplotlib.dates import (FR, MO, MONTHLY, SA, SU, TH, TU, WE, AutoDateFormatter, AutoDateLocator, ConciseDateFormatter, DateFormatter, DayLocator, diff --git a/galleries/tutorials/images.py b/galleries/tutorials/images.py index 0867f7b6d672..a7c474dab40b 100644 --- a/galleries/tutorials/images.py +++ b/galleries/tutorials/images.py @@ -33,8 +33,8 @@ In [1]: %matplotlib inline -This turns on inline plotting, where plot graphics will appear in your -notebook. This has important implications for interactivity. For inline plotting, commands in +This turns on inline plotting, where plot graphics will appear in your notebook. This +has important implications for interactivity. For inline plotting, commands in cells below the cell that outputs a plot will not affect the plot. For example, changing the colormap is not possible from cells below the cell that creates a plot. However, for other backends, such as Qt, that open a separate window, diff --git a/pyproject.toml b/pyproject.toml index 6202a386069c..b580feff930e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -210,7 +210,6 @@ convention = "numpy" "galleries/users_explain/axes/legend_guide.py" = ["E402"] "galleries/users_explain/axes/tight_layout_guide.py" = ["E402"] "galleries/users_explain/animations/animations.py" = ["E501"] -"galleries/tutorials/images.py" = ["E501"] "galleries/tutorials/pyplot.py" = ["E402", "E501"] "galleries/users_explain/text/annotations.py" = ["E402", "E501"] "galleries/users_explain/text/text_intro.py" = ["E402"] From 757282354c88eb25610a2ca60a4dda88f2053fda Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 1 Jul 2025 03:47:19 -0400 Subject: [PATCH 44/50] Check that stem input is 1D This also has the side-effect of casting torch Tensors to NumPy arrays, which fixes #30216. Since `stem` is made up of `plot` and `[hv]lines` whic already do this cast, this just moves it up one level which prevents doing it twice. --- lib/matplotlib/axes/_axes.py | 3 +++ lib/matplotlib/tests/test_axes.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index b4ed7ae22d35..8ac300296538 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3436,6 +3436,9 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0, else: # horizontal heads, locs = self._process_unit_info([("x", heads), ("y", locs)]) + heads = cbook._check_1d(heads) + locs = cbook._check_1d(locs) + # resolve line format if linefmt is None: linefmt = args[0] if len(args) > 0 else "C0-" diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index db0629de99b5..bba9f8648799 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4744,6 +4744,11 @@ def _assert_equal(stem_container, expected): _assert_equal(ax.stem(y, linefmt='r--'), expected=([0, 1, 2], y)) _assert_equal(ax.stem(y, 'r--'), expected=([0, 1, 2], y)) + with pytest.raises(ValueError): + ax.stem([[y]]) + with pytest.raises(ValueError): + ax.stem([[x]], y) + def test_stem_markerfmt(): """Test that stem(..., markerfmt=...) produces the intended markers.""" From e78735bb8e0375205c7597a6a151c5972ff31426 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 4 Jul 2025 23:32:49 +0200 Subject: [PATCH 45/50] Tweak docstrings of get_window_extent/get_tightbbox. Make the difference between the two methods clearer (only the latter takes clipping into account). --- lib/matplotlib/artist.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index fd35b312835a..50c9842ff010 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -321,13 +321,12 @@ def stale(self, val): def get_window_extent(self, renderer=None): """ - Get the artist's bounding box in display space. + Get the artist's bounding box in display space, ignoring clipping. The bounding box's width and height are non-negative. - Subclasses should override for inclusion in the bounding box - "tight" calculation. Default is to return an empty bounding - box at 0, 0. + Subclasses should override for inclusion in the bounding box "tight" + calculation. Default is to return an empty bounding box at 0, 0. .. warning:: @@ -341,28 +340,40 @@ def get_window_extent(self, renderer=None): screen render incorrectly when saved to file. To get accurate results you may need to manually call - `matplotlib.figure.Figure.savefig` or - `matplotlib.figure.Figure.draw_without_rendering` to have Matplotlib - compute the rendered size. + `~.Figure.savefig` or `~.Figure.draw_without_rendering` to have + Matplotlib compute the rendered size. + Parameters + ---------- + renderer : `~matplotlib.backend_bases.RendererBase`, optional + Renderer used to draw the figure (i.e. ``fig.canvas.get_renderer()``). + + See Also + -------- + `~.Artist.get_tightbbox` : + Get the artist bounding box, taking clipping into account. """ return Bbox([[0, 0], [0, 0]]) def get_tightbbox(self, renderer=None): """ - Like `.Artist.get_window_extent`, but includes any clipping. + Get the artist's bounding box in display space, taking clipping into account. Parameters ---------- - renderer : `~matplotlib.backend_bases.RendererBase` subclass, optional - renderer that will be used to draw the figures (i.e. - ``fig.canvas.get_renderer()``) + renderer : `~matplotlib.backend_bases.RendererBase`, optional + Renderer used to draw the figure (i.e. ``fig.canvas.get_renderer()``). Returns ------- `.Bbox` or None - The enclosing bounding box (in figure pixel coordinates). - Returns None if clipping results in no intersection. + The enclosing bounding box (in figure pixel coordinates), or None + if clipping results in no intersection. + + See Also + -------- + `~.Artist.get_window_extent` : + Get the artist bounding box, ignoring clipping. """ bbox = self.get_window_extent(renderer) if self.get_clip_on(): From 9c3f73bad3952ba3cb0cbde904e989d38e7ccaa2 Mon Sep 17 00:00:00 2001 From: Ruth Comer <10599679+rcomer@users.noreply.github.com> Date: Sat, 5 Jul 2025 19:33:04 +0100 Subject: [PATCH 46/50] DOC: fix artist see also sections --- lib/matplotlib/artist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index 50c9842ff010..eaaae43e283a 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -350,7 +350,7 @@ def get_window_extent(self, renderer=None): See Also -------- - `~.Artist.get_tightbbox` : + .Artist.get_tightbbox : Get the artist bounding box, taking clipping into account. """ return Bbox([[0, 0], [0, 0]]) @@ -372,7 +372,7 @@ def get_tightbbox(self, renderer=None): See Also -------- - `~.Artist.get_window_extent` : + .Artist.get_window_extent : Get the artist bounding box, ignoring clipping. """ bbox = self.get_window_extent(renderer) From db6cf1579e6da5c787d89b47088b9ce853343f04 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 18 Jun 2025 23:06:16 +0100 Subject: [PATCH 47/50] Fix figure legend when drawing stackplots --- lib/matplotlib/legend_handler.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 263945b050d0..65a78891b17f 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -799,7 +799,6 @@ def get_first(prop_array): legend_handle.set_linewidth(get_first(orig_handle.get_linewidths())) legend_handle.set_linestyle(get_first(orig_handle.get_linestyles())) legend_handle.set_transform(get_first(orig_handle.get_transforms())) - legend_handle.set_figure(orig_handle.get_figure()) # Alpha is already taken into account by the color attributes. def create_artists(self, legend, orig_handle, From b370e30c4cc5412ded32796ea56a4a1926e39b02 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 6 Jul 2025 21:51:28 +0100 Subject: [PATCH 48/50] Add smoke test --- lib/matplotlib/tests/test_axes.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index bba9f8648799..c96173e340f7 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -3420,6 +3420,20 @@ def test_stackplot_hatching(fig_ref, fig_test): ax_ref.set_ylim(0, 70) +def test_stackplot_subfig_legend(): + # Smoke test for https://github.com/matplotlib/matplotlib/issues/30158 + + fig = plt.figure() + subfigs = fig.subfigures(nrows=1, ncols=2) + + for _fig in subfigs: + ax = _fig.subplots(nrows=1, ncols=1) + ax.stackplot([3, 4], [[1, 2]], labels=['a']) + + fig.legend() + fig.draw_without_rendering() + + def _bxp_test_helper( stats_kwargs={}, transform_stats=lambda s: s, bxp_kwargs={}): np.random.seed(937) From f31ba35f41f2a1074fdf9ecc33e34317fe9e19a0 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 22 Aug 2022 18:43:28 -0400 Subject: [PATCH 49/50] Use old stride_windows implementation on 32-bit builds This was originally for i686 on Fedora, but is now applicable to WASM, which is 32-bit. The older implementation doesn't OOM. --- lib/matplotlib/mlab.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index f538b79e44f0..b4b4c3f96828 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -48,7 +48,8 @@ """ import functools -from numbers import Number +from numbers import Integral, Number +import sys import numpy as np @@ -210,6 +211,23 @@ def detrend_linear(y): return y - (b*x + a) +def _stride_windows(x, n, noverlap=0): + x = np.asarray(x) + + _api.check_isinstance(Integral, n=n, noverlap=noverlap) + if not (1 <= n <= x.size and n < noverlap): + raise ValueError(f'n ({n}) and noverlap ({noverlap}) must be positive integers ' + f'with n < noverlap and n <= x.size ({x.size})') + + if n == 1 and noverlap == 0: + return x[np.newaxis] + + step = n - noverlap + shape = (n, (x.shape[-1]-noverlap)//step) + strides = (x.strides[0], step*x.strides[0]) + return np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides) + + def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None, mode=None): @@ -304,8 +322,12 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None, raise ValueError( "The window length must match the data's first dimension") - result = np.lib.stride_tricks.sliding_window_view( - x, NFFT, axis=0)[::NFFT - noverlap].T + if sys.maxsize > 2**32: + result = np.lib.stride_tricks.sliding_window_view( + x, NFFT, axis=0)[::NFFT - noverlap].T + else: + # The NumPy version on 32-bit will OOM, so use old implementation. + result = _stride_windows(x, NFFT, noverlap=noverlap) result = detrend(result, detrend_func, axis=0) result = result * window.reshape((-1, 1)) result = np.fft.fft(result, n=pad_to, axis=0)[:numFreqs, :] @@ -313,8 +335,12 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None, if not same_data: # if same_data is False, mode must be 'psd' - resultY = np.lib.stride_tricks.sliding_window_view( - y, NFFT, axis=0)[::NFFT - noverlap].T + if sys.maxsize > 2**32: + resultY = np.lib.stride_tricks.sliding_window_view( + y, NFFT, axis=0)[::NFFT - noverlap].T + else: + # The NumPy version on 32-bit will OOM, so use old implementation. + resultY = _stride_windows(y, NFFT, noverlap=noverlap) resultY = detrend(resultY, detrend_func, axis=0) resultY = resultY * window.reshape((-1, 1)) resultY = np.fft.fft(resultY, n=pad_to, axis=0)[:numFreqs, :] From 389373eca101a613ffb7f88271d5eb9c10712005 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 30 Jun 2025 17:44:09 -0400 Subject: [PATCH 50/50] ci: Preload existing test images from text-overhaul-figures branch This allows checking that there are no _new_ failures, without committing the new figures to the repo until the branch is complete. --- .appveyor.yml | 20 ++++++++++++++++++++ .github/workflows/tests.yml | 19 +++++++++++++++++++ azure-pipelines.yml | 19 +++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/.appveyor.yml b/.appveyor.yml index c3fcb0ea9591..3e3a3b884d18 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -60,6 +60,26 @@ install: - micromamba env create -f environment.yml python=%PYTHON_VERSION% pywin32 - micromamba activate mpl-dev +before_test: + - git config --global user.name 'Matplotlib' + - git config --global user.email 'nobody@matplotlib.org' + - git fetch https://github.com/QuLogic/matplotlib.git text-overhaul-figures:text-overhaul-figures + - git merge --no-commit text-overhaul-figures || true + # If there are any conflicts in baseline images, then pick "ours", + # which should be the updated images in the PR. + - ps: | + $conflicts = git diff --name-only --diff-filter=U ` + lib/matplotlib/tests/baseline_images ` + lib/mpl_toolkits/*/tests/baseline_images + if ($conflicts) { + git checkout --ours -- $conflicts + git add -- $conflicts + } + git status + # If committing fails, there were conflicts other than the baseline images, + # which should not be allowed to happen, and should fail the build. + - git commit -m "Preload test images from branch text-overhaul-figures" + test_script: # Now build the thing.. - set LINK=/LIBPATH:%cd%\lib diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 85ace93445b6..53d47346c6eb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -95,6 +95,25 @@ jobs: fetch-depth: 0 persist-credentials: false + - name: Preload test images + run: | + git config --global user.name 'Matplotlib' + git config --global user.email 'nobody@matplotlib.org' + git fetch https://github.com/QuLogic/matplotlib.git text-overhaul-figures:text-overhaul-figures + git merge --no-commit text-overhaul-figures || true + # If there are any conflicts in baseline images, then pick "ours", + # which should be the updated images in the PR. + conflicts=$(git diff --name-only --diff-filter=U \ + lib/matplotlib/tests/baseline_images \ + lib/mpl_toolkits/*/tests/baseline_images) + if [ -n "${conflicts}" ]; then + git checkout --ours -- "${conflicts}" + git add -- "${conflicts}" + fi + # If committing fails, there were conflicts other than the baseline images, + # which should not be allowed to happen, and should fail the build. + git commit -m 'Preload test images from branch text-overhaul-figures' + - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: diff --git a/azure-pipelines.yml b/azure-pipelines.yml index d68a9d36f0d3..a5a0e965e97b 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -68,6 +68,25 @@ stages: architecture: 'x64' displayName: 'Use Python $(python.version)' + - bash: | + git config --global user.name 'Matplotlib' + git config --global user.email 'nobody@matplotlib.org' + git fetch https://github.com/QuLogic/matplotlib.git text-overhaul-figures:text-overhaul-figures + git merge --no-commit text-overhaul-figures || true + # If there are any conflicts in baseline images, then pick "ours", + # which should be the updated images in the PR. + conflicts=$(git diff --name-only --diff-filter=U \ + lib/matplotlib/tests/baseline_images \ + lib/mpl_toolkits/*/tests/baseline_images) + if [ -n "${conflicts}" ]; then + git checkout --ours -- "${conflicts}" + git add -- "${conflicts}" + fi + # If committing fails, there were conflicts other than the baseline images, + # which should not be allowed to happen, and should fail the build. + git commit -m 'Preload test images from branch text-overhaul-figures' + displayName: Preload test images + - bash: | choco install ninja displayName: 'Install dependencies'