From f157c7317b67711c086a5d4d4c2b796c1bfb31d8 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 13 Feb 2023 09:24:42 -0800 Subject: [PATCH] Backport PR #25198 - DOC: remove constrained_layout kwarg from examples Manual backport of #25198 due to conflicts in examples/subplots_axes_and_figures/demo_constrained_layout.py This was because some of the changes here were right below the section separators which changed in main at #25021. --- examples/axisartist/simple_axisartist1.py | 2 +- .../contourf_demo.py | 6 ++-- .../image_antialiasing.py | 4 +-- .../image_nonuniform.py | 2 +- .../pcolormesh_grids.py | 4 +-- examples/lines_bars_and_markers/barchart.py | 2 +- .../curve_error_band.py | 3 +- .../lines_bars_and_markers/markevery_demo.py | 8 ++--- examples/lines_bars_and_markers/psd_demo.py | 4 +-- .../lines_bars_and_markers/scatter_hist.py | 2 +- examples/lines_bars_and_markers/timeline.py | 2 +- examples/misc/rasterization_demo.py | 2 +- examples/scales/asinh_demo.py | 2 +- examples/scales/scales.py | 3 +- .../hatch_style_reference.py | 6 ++-- examples/spines/spines.py | 2 +- examples/statistics/barchart_demo.py | 2 +- examples/statistics/time_series_histogram.py | 2 +- .../style_sheets/style_sheets_reference.py | 2 +- .../axes_box_aspect.py | 6 ++-- .../colorbar_placement.py | 8 ++--- .../demo_constrained_layout.py | 12 ++++---- .../subplots_axes_and_figures/figure_title.py | 4 +-- .../gridspec_multicolumn.py | 2 +- examples/subplots_axes_and_figures/mosaic.py | 30 +++++++++---------- .../secondary_axis.py | 8 ++--- .../subplots_axes_and_figures/subfigures.py | 8 ++--- .../text_labels_and_annotations/arrow_demo.py | 2 +- examples/text_labels_and_annotations/date.py | 2 +- .../figlegend_demo.py | 4 +-- .../label_subplots.py | 6 ++-- .../legend_demo.py | 4 +-- .../titles_demo.py | 4 +-- examples/ticks/date_concise_formatter.py | 10 +++---- examples/ticks/date_index_formatter.py | 7 ++--- examples/ticks/date_precision_and_epochs.py | 4 +-- examples/ticks/ticks_too_many.py | 4 +-- examples/units/units_sample.py | 2 +- .../gtk4_spreadsheet_sgskip.py | 2 +- examples/userdemo/connectionstyle_demo.py | 2 +- examples/widgets/rectangle_selector.py | 2 +- 41 files changed, 95 insertions(+), 98 deletions(-) diff --git a/examples/axisartist/simple_axisartist1.py b/examples/axisartist/simple_axisartist1.py index 95d710cbe0b1..ed7d02d8f66b 100644 --- a/examples/axisartist/simple_axisartist1.py +++ b/examples/axisartist/simple_axisartist1.py @@ -18,7 +18,7 @@ import numpy as np -fig = plt.figure(figsize=(6, 3), constrained_layout=True) +fig = plt.figure(figsize=(6, 3), layout="constrained") # To construct axes of two different classes, we need to use gridspec (or # MATLAB-style add_subplot calls). gs = fig.add_gridspec(1, 2) diff --git a/examples/images_contours_and_fields/contourf_demo.py b/examples/images_contours_and_fields/contourf_demo.py index d7b1bbe9c284..3a3f2b9564df 100644 --- a/examples/images_contours_and_fields/contourf_demo.py +++ b/examples/images_contours_and_fields/contourf_demo.py @@ -40,7 +40,7 @@ # a good idea, because they don't occur on nice boundaries, but we do it here # for purposes of illustration. -fig1, ax2 = plt.subplots(constrained_layout=True) +fig1, ax2 = plt.subplots(layout='constrained') CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.bone, origin=origin) # Note that in the following, we explicitly pass in a subset of the contour @@ -66,7 +66,7 @@ # Now make a contour plot with the levels specified, and with the colormap # generated automatically from a list of colors. -fig2, ax2 = plt.subplots(constrained_layout=True) +fig2, ax2 = plt.subplots(layout='constrained') levels = [-1.5, -1, -0.5, 0, 0.5, 1] CS3 = ax2.contourf(X, Y, Z, levels, colors=('r', 'g', 'b'), @@ -101,7 +101,7 @@ # no effect: # cmap.set_bad("red") -fig, axs = plt.subplots(2, 2, constrained_layout=True) +fig, axs = plt.subplots(2, 2, layout="constrained") for ax, extend in zip(axs.flat, extends): cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend, origin=origin) diff --git a/examples/images_contours_and_fields/image_antialiasing.py b/examples/images_contours_and_fields/image_antialiasing.py index 0ada6251b653..eb8c431f8d96 100644 --- a/examples/images_contours_and_fields/image_antialiasing.py +++ b/examples/images_contours_and_fields/image_antialiasing.py @@ -66,7 +66,7 @@ # original colormap, so it is no longer possible to invert individual # pixels back to their data value. -fig, axs = plt.subplots(2, 2, figsize=(5, 6), constrained_layout=True) +fig, axs = plt.subplots(2, 2, figsize=(5, 6), layout='constrained') axs[0, 0].imshow(a, interpolation='nearest', cmap='RdBu_r') axs[0, 0].set_xlim(100, 200) axs[0, 0].set_ylim(275, 175) @@ -104,7 +104,7 @@ # Apart from the default 'hanning' antialiasing, `~.Axes.imshow` supports a # number of different interpolation algorithms, which may work better or # worse depending on the pattern. -fig, axs = plt.subplots(1, 2, figsize=(7, 4), constrained_layout=True) +fig, axs = plt.subplots(1, 2, figsize=(7, 4), layout='constrained') for ax, interp in zip(axs, ['hanning', 'lanczos']): ax.imshow(a, interpolation=interp, cmap='gray') ax.set_title(f"interpolation='{interp}'") diff --git a/examples/images_contours_and_fields/image_nonuniform.py b/examples/images_contours_and_fields/image_nonuniform.py index 2b0c4fb8a798..80554673eff3 100644 --- a/examples/images_contours_and_fields/image_nonuniform.py +++ b/examples/images_contours_and_fields/image_nonuniform.py @@ -25,7 +25,7 @@ z = np.sqrt(x[np.newaxis, :]**2 + y[:, np.newaxis]**2) -fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True) +fig, axs = plt.subplots(nrows=2, ncols=2, layout='constrained') fig.suptitle('NonUniformImage class', fontsize='large') ax = axs[0, 0] im = NonUniformImage(ax, interpolation=interp, extent=(-4, 4, -4, 4), diff --git a/examples/images_contours_and_fields/pcolormesh_grids.py b/examples/images_contours_and_fields/pcolormesh_grids.py index 7fc75f8b6dd9..7faaf3a86696 100644 --- a/examples/images_contours_and_fields/pcolormesh_grids.py +++ b/examples/images_contours_and_fields/pcolormesh_grids.py @@ -89,7 +89,7 @@ def _annotate(ax, x, y, title): # to use, in this case ``shading='auto'`` will decide whether to use 'flat' or # 'nearest' shading based on the shapes of *X*, *Y* and *Z*. -fig, axs = plt.subplots(2, 1, constrained_layout=True) +fig, axs = plt.subplots(2, 1, layout='constrained') ax = axs[0] x = np.arange(ncols) y = np.arange(nrows) @@ -110,7 +110,7 @@ def _annotate(ax, x, y, title): # be specified, where the color in the quadrilaterals is linearly interpolated # between the grid points. The shapes of *X*, *Y*, *Z* must be the same. -fig, ax = plt.subplots(constrained_layout=True) +fig, ax = plt.subplots(layout='constrained') x = np.arange(ncols) y = np.arange(nrows) ax.pcolormesh(x, y, Z, shading='gouraud', vmin=Z.min(), vmax=Z.max()) diff --git a/examples/lines_bars_and_markers/barchart.py b/examples/lines_bars_and_markers/barchart.py index f5ee6833a881..da30bbf75937 100644 --- a/examples/lines_bars_and_markers/barchart.py +++ b/examples/lines_bars_and_markers/barchart.py @@ -23,7 +23,7 @@ width = 0.25 # the width of the bars multiplier = 0 -fig, ax = plt.subplots(constrained_layout=True) +fig, ax = plt.subplots(layout='constrained') for attribute, measurement in penguin_means.items(): offset = width * multiplier diff --git a/examples/lines_bars_and_markers/curve_error_band.py b/examples/lines_bars_and_markers/curve_error_band.py index e55b467849a8..4d0e09482b31 100644 --- a/examples/lines_bars_and_markers/curve_error_band.py +++ b/examples/lines_bars_and_markers/curve_error_band.py @@ -63,8 +63,7 @@ def draw_error_band(ax, x, y, err, **kwargs): ax.add_patch(PathPatch(path, **kwargs)) -axs = (plt.figure(constrained_layout=True) - .subplots(1, 2, sharex=True, sharey=True)) +_, axs = plt.subplots(1, 2, layout='constrained', sharex=True, sharey=True) errs = [ (axs[0], "constant error", 0.05), (axs[1], "variable error", 0.05 * np.sin(2 * t) ** 2 + 0.04), diff --git a/examples/lines_bars_and_markers/markevery_demo.py b/examples/lines_bars_and_markers/markevery_demo.py index 8dc02f52e28a..d385515043d5 100644 --- a/examples/lines_bars_and_markers/markevery_demo.py +++ b/examples/lines_bars_and_markers/markevery_demo.py @@ -44,7 +44,7 @@ # markevery with linear scales # ---------------------------- -fig, axs = plt.subplots(3, 3, figsize=(10, 6), constrained_layout=True) +fig, axs = plt.subplots(3, 3, figsize=(10, 6), layout='constrained') for ax, markevery in zip(axs.flat, cases): ax.set_title(f'markevery={markevery}') ax.plot(x, y, 'o', ls='-', ms=4, markevery=markevery) @@ -58,7 +58,7 @@ # fraction of figure size creates even distributions, because it's based on # fractions of the Axes diagonal, not on data coordinates or data indices. -fig, axs = plt.subplots(3, 3, figsize=(10, 6), constrained_layout=True) +fig, axs = plt.subplots(3, 3, figsize=(10, 6), layout='constrained') for ax, markevery in zip(axs.flat, cases): ax.set_title(f'markevery={markevery}') ax.set_xscale('log') @@ -75,7 +75,7 @@ # diagonal, it changes the displayed data range, and more points will be # displayed when zooming. -fig, axs = plt.subplots(3, 3, figsize=(10, 6), constrained_layout=True) +fig, axs = plt.subplots(3, 3, figsize=(10, 6), layout='constrained') for ax, markevery in zip(axs.flat, cases): ax.set_title(f'markevery={markevery}') ax.plot(x, y, 'o', ls='-', ms=4, markevery=markevery) @@ -89,7 +89,7 @@ r = np.linspace(0, 3.0, 200) theta = 2 * np.pi * r -fig, axs = plt.subplots(3, 3, figsize=(10, 6), constrained_layout=True, +fig, axs = plt.subplots(3, 3, figsize=(10, 6), layout='constrained', subplot_kw={'projection': 'polar'}) for ax, markevery in zip(axs.flat, cases): ax.set_title(f'markevery={markevery}') diff --git a/examples/lines_bars_and_markers/psd_demo.py b/examples/lines_bars_and_markers/psd_demo.py index 0ab038c2991b..b4949019a2d6 100644 --- a/examples/lines_bars_and_markers/psd_demo.py +++ b/examples/lines_bars_and_markers/psd_demo.py @@ -111,7 +111,7 @@ xn = (A * np.sin(2 * np.pi * f * t)).sum(axis=0) xn += 5 * np.random.randn(*t.shape) -fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True) +fig, (ax0, ax1) = plt.subplots(ncols=2, layout='constrained') yticks = np.arange(-50, 30, 10) yrange = (yticks[0], yticks[-1]) @@ -151,7 +151,7 @@ f = np.array([150, 140]).reshape(-1, 1) xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * prng.randn(*t.shape) -fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True) +fig, (ax0, ax1) = plt.subplots(ncols=2, layout='constrained') yticks = np.arange(-50, 30, 10) yrange = (yticks[0], yticks[-1]) diff --git a/examples/lines_bars_and_markers/scatter_hist.py b/examples/lines_bars_and_markers/scatter_hist.py index 29b2f96947a4..7819808261f3 100644 --- a/examples/lines_bars_and_markers/scatter_hist.py +++ b/examples/lines_bars_and_markers/scatter_hist.py @@ -89,7 +89,7 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy): # of the axes. # Create a Figure, which doesn't have to be square. -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout='constrained') # Create the main axes, leaving 25% of the figure space at the top and on the # right to position marginals. ax = fig.add_gridspec(top=0.75, right=0.75).subplots() diff --git a/examples/lines_bars_and_markers/timeline.py b/examples/lines_bars_and_markers/timeline.py index e13bab115a30..72327f18fc84 100644 --- a/examples/lines_bars_and_markers/timeline.py +++ b/examples/lines_bars_and_markers/timeline.py @@ -68,7 +68,7 @@ int(np.ceil(len(dates)/6)))[:len(dates)] # Create figure and plot a stem plot with the date -fig, ax = plt.subplots(figsize=(8.8, 4), constrained_layout=True) +fig, ax = plt.subplots(figsize=(8.8, 4), layout="constrained") ax.set(title="Matplotlib release dates") ax.vlines(dates, 0, levels, color="tab:red") # The vertical stems. diff --git a/examples/misc/rasterization_demo.py b/examples/misc/rasterization_demo.py index f0188c684fc7..4030587b693a 100644 --- a/examples/misc/rasterization_demo.py +++ b/examples/misc/rasterization_demo.py @@ -44,7 +44,7 @@ xx = x*np.cos(theta) - y*np.sin(theta) # rotate x by -theta yy = x*np.sin(theta) + y*np.cos(theta) # rotate y by -theta -fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, constrained_layout=True) +fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, layout="constrained") # pcolormesh without rasterization ax1.set_aspect(1) diff --git a/examples/scales/asinh_demo.py b/examples/scales/asinh_demo.py index ae13fb1265de..854e634a640f 100644 --- a/examples/scales/asinh_demo.py +++ b/examples/scales/asinh_demo.py @@ -65,7 +65,7 @@ ############################################################################### # Compare "asinh" graphs with different scale parameter "linear_width": -fig2 = plt.figure(constrained_layout=True) +fig2 = plt.figure(layout='constrained') axs = fig2.subplots(1, 3, sharex=True) for ax, (a0, base) in zip(axs, ((0.2, 2), (1.0, 0), (5.0, 10))): ax.set_title(f'linear_width={a0:.3g}') diff --git a/examples/scales/scales.py b/examples/scales/scales.py index 25ec82608f01..803c801b4080 100644 --- a/examples/scales/scales.py +++ b/examples/scales/scales.py @@ -23,8 +23,7 @@ x = np.arange(len(y)) # plot with various axes scales -fig, axs = plt.subplots(3, 2, figsize=(6, 8), - constrained_layout=True) +fig, axs = plt.subplots(3, 2, figsize=(6, 8), layout='constrained') # linear ax = axs[0, 0] diff --git a/examples/shapes_and_collections/hatch_style_reference.py b/examples/shapes_and_collections/hatch_style_reference.py index 5fa13163ff33..1b31f28740a9 100644 --- a/examples/shapes_and_collections/hatch_style_reference.py +++ b/examples/shapes_and_collections/hatch_style_reference.py @@ -16,7 +16,7 @@ import matplotlib.pyplot as plt from matplotlib.patches import Rectangle -fig, axs = plt.subplots(2, 5, constrained_layout=True, figsize=(6.4, 3.2)) +fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2)) hatches = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'] @@ -33,7 +33,7 @@ def hatches_plot(ax, h): ############################################################################### # Hatching patterns can be repeated to increase the density. -fig, axs = plt.subplots(2, 5, constrained_layout=True, figsize=(6.4, 3.2)) +fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2)) hatches = ['//', '\\\\', '||', '--', '++', 'xx', 'oo', 'OO', '..', '**'] @@ -43,7 +43,7 @@ def hatches_plot(ax, h): ############################################################################### # Hatching patterns can be combined to create additional patterns. -fig, axs = plt.subplots(2, 5, constrained_layout=True, figsize=(6.4, 3.2)) +fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2)) hatches = ['/o', '\\|', '|*', '-\\', '+o', 'x*', 'o-', 'O|', 'O.', '*-'] diff --git a/examples/spines/spines.py b/examples/spines/spines.py index 0917fc8c107c..659f9fc3e8c2 100644 --- a/examples/spines/spines.py +++ b/examples/spines/spines.py @@ -23,7 +23,7 @@ y = 2 * np.sin(x) # Constrained layout makes sure the labels don't overlap the axes. -fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, constrained_layout=True) +fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, layout='constrained') ax0.plot(x, y) ax0.set_title('normal spines') diff --git a/examples/statistics/barchart_demo.py b/examples/statistics/barchart_demo.py index 18819ca35b08..d91a093d21cb 100644 --- a/examples/statistics/barchart_demo.py +++ b/examples/statistics/barchart_demo.py @@ -45,7 +45,7 @@ def format_score(score): def plot_student_results(student, scores_by_test, cohort_size): - fig, ax1 = plt.subplots(figsize=(9, 7), constrained_layout=True) + fig, ax1 = plt.subplots(figsize=(9, 7), layout='constrained') fig.canvas.manager.set_window_title('Eldorado K-8 Fitness Chart') ax1.set_title(student.name) diff --git a/examples/statistics/time_series_histogram.py b/examples/statistics/time_series_histogram.py index a22ceb4e24d4..d35b6393504e 100644 --- a/examples/statistics/time_series_histogram.py +++ b/examples/statistics/time_series_histogram.py @@ -31,7 +31,7 @@ import matplotlib.pyplot as plt from matplotlib.colors import LogNorm -fig, axes = plt.subplots(nrows=3, figsize=(6, 8), constrained_layout=True) +fig, axes = plt.subplots(nrows=3, figsize=(6, 8), layout='constrained') # Make some data; a 1D random walk + small fraction of sine waves num_series = 1000 diff --git a/examples/style_sheets/style_sheets_reference.py b/examples/style_sheets/style_sheets_reference.py index 90e5812a30b8..8557cc10a203 100644 --- a/examples/style_sheets/style_sheets_reference.py +++ b/examples/style_sheets/style_sheets_reference.py @@ -116,7 +116,7 @@ def plot_figure(style_label=""): prng = np.random.RandomState(96917002) fig, axs = plt.subplots(ncols=6, nrows=1, num=style_label, - figsize=(14.8, 2.8), constrained_layout=True) + figsize=(14.8, 2.8), layout='constrained') # make a suptitle, in the same style for all subfigures, # except those with dark backgrounds, which get a lighter color: diff --git a/examples/subplots_axes_and_figures/axes_box_aspect.py b/examples/subplots_axes_and_figures/axes_box_aspect.py index 019c7e43c787..089f84c26eea 100644 --- a/examples/subplots_axes_and_figures/axes_box_aspect.py +++ b/examples/subplots_axes_and_figures/axes_box_aspect.py @@ -74,10 +74,10 @@ # height. `~.Axes.set_box_aspect` provides an easy solution to that by allowing # to have the normal plot's axes use the images dimensions as box aspect. # -# This example also shows that ``constrained_layout`` interplays nicely with +# This example also shows that *constrained layout* interplays nicely with # a fixed box aspect. -fig4, (ax, ax2) = plt.subplots(ncols=2, constrained_layout=True) +fig4, (ax, ax2) = plt.subplots(ncols=2, layout="constrained") np.random.seed(19680801) # Fixing random state for reproducibility im = np.random.rand(16, 27) @@ -139,7 +139,7 @@ # following creates a 2 by 3 subplot grid with all square Axes. fig7, axs = plt.subplots(2, 3, subplot_kw=dict(box_aspect=1), - sharex=True, sharey=True, constrained_layout=True) + sharex=True, sharey=True, layout="constrained") for i, ax in enumerate(axs.flat): ax.scatter(i % 3, -((i // 3) - 0.5)*200, c=[plt.cm.hsv(i / 6)], s=300) diff --git a/examples/subplots_axes_and_figures/colorbar_placement.py b/examples/subplots_axes_and_figures/colorbar_placement.py index 66e267ce6d43..7bd952247637 100644 --- a/examples/subplots_axes_and_figures/colorbar_placement.py +++ b/examples/subplots_axes_and_figures/colorbar_placement.py @@ -41,9 +41,9 @@ ###################################################################### # Relatively complicated colorbar layouts are possible using this # paradigm. Note that this example works far better with -# ``constrained_layout=True`` +# ``layout='constrained'`` -fig, axs = plt.subplots(3, 3, constrained_layout=True) +fig, axs = plt.subplots(3, 3, layout='constrained') for ax in axs.flat: pcm = ax.pcolormesh(np.random.random((20, 20))) @@ -59,7 +59,7 @@ # Placing colorbars for axes with a fixed aspect ratio pose a particular # challenge as the parent axes changes size depending on the data view. -fig, axs = plt.subplots(2, 2, constrained_layout=True) +fig, axs = plt.subplots(2, 2, layout='constrained') cmaps = ['RdBu_r', 'viridis'] for col in range(2): for row in range(2): @@ -78,7 +78,7 @@ # axes in axes coordinates. Note that if you zoom in on the axes, and # change the shape of the axes, the colorbar will also change position. -fig, axs = plt.subplots(2, 2, constrained_layout=True) +fig, axs = plt.subplots(2, 2, layout='constrained') cmaps = ['RdBu_r', 'viridis'] for col in range(2): for row in range(2): diff --git a/examples/subplots_axes_and_figures/demo_constrained_layout.py b/examples/subplots_axes_and_figures/demo_constrained_layout.py index 26109cbe3129..bf2acd797c76 100644 --- a/examples/subplots_axes_and_figures/demo_constrained_layout.py +++ b/examples/subplots_axes_and_figures/demo_constrained_layout.py @@ -3,7 +3,7 @@ Resizing axes with constrained layout ===================================== -Constrained layout attempts to resize subplots in +*Constrained layout* attempts to resize subplots in a figure so that there are no overlaps between axes objects and labels on the axes. @@ -23,17 +23,17 @@ def example_plot(ax): ############################################################################### -# If we don't use constrained_layout, then labels overlap the axes +# If we don't use *constrained layout*, then labels overlap the axes -fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=False) +fig, axs = plt.subplots(nrows=2, ncols=2, layout=None) for ax in axs.flat: example_plot(ax) ############################################################################### -# adding ``constrained_layout=True`` automatically adjusts. +# adding ``layout='constrained'`` automatically adjusts. -fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True) +fig, axs = plt.subplots(nrows=2, ncols=2, layout='constrained') for ax in axs.flat: example_plot(ax) @@ -41,7 +41,7 @@ def example_plot(ax): ############################################################################### # Below is a more complicated example using nested gridspecs. -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout='constrained') import matplotlib.gridspec as gridspec diff --git a/examples/subplots_axes_and_figures/figure_title.py b/examples/subplots_axes_and_figures/figure_title.py index 0b8a7e2c5855..195bab34d6be 100644 --- a/examples/subplots_axes_and_figures/figure_title.py +++ b/examples/subplots_axes_and_figures/figure_title.py @@ -18,7 +18,7 @@ x = np.linspace(0.0, 5.0, 501) -fig, (ax1, ax2) = plt.subplots(1, 2, constrained_layout=True, sharey=True) +fig, (ax1, ax2) = plt.subplots(1, 2, layout='constrained', sharey=True) ax1.plot(x, np.cos(6*x) * np.exp(-x)) ax1.set_title('damped') ax1.set_xlabel('time (s)') @@ -34,7 +34,7 @@ # A global x- or y-label can be set using the `.FigureBase.supxlabel` and # `.FigureBase.supylabel` methods. -fig, axs = plt.subplots(3, 5, figsize=(8, 5), constrained_layout=True, +fig, axs = plt.subplots(3, 5, figsize=(8, 5), layout='constrained', sharex=True, sharey=True) fname = get_sample_data('percent_bachelors_degrees_women_usa.csv', diff --git a/examples/subplots_axes_and_figures/gridspec_multicolumn.py b/examples/subplots_axes_and_figures/gridspec_multicolumn.py index 5a22aa2d310c..533967b998cb 100644 --- a/examples/subplots_axes_and_figures/gridspec_multicolumn.py +++ b/examples/subplots_axes_and_figures/gridspec_multicolumn.py @@ -17,7 +17,7 @@ def format_axes(fig): ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center") ax.tick_params(labelbottom=False, labelleft=False) -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout="constrained") gs = GridSpec(3, 3, figure=fig) ax1 = fig.add_subplot(gs[0, :]) diff --git a/examples/subplots_axes_and_figures/mosaic.py b/examples/subplots_axes_and_figures/mosaic.py index 9b77893bb2d6..3c7a333e22bd 100644 --- a/examples/subplots_axes_and_figures/mosaic.py +++ b/examples/subplots_axes_and_figures/mosaic.py @@ -60,7 +60,7 @@ def identify_axes(ax_dict, fontsize=48): hist_data = np.random.randn(1_500) -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout="constrained") ax_array = fig.subplots(2, 2, squeeze=False) ax_array[0, 0].bar(["a", "b", "c"], [5, 7, 9]) @@ -76,7 +76,7 @@ def identify_axes(ax_dict, fontsize=48): # Using `.Figure.subplot_mosaic` we can produce the same mosaic but give the # axes semantic names -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout="constrained") ax_dict = fig.subplot_mosaic( [ ["bar", "plot"], @@ -116,7 +116,7 @@ def identify_axes(ax_dict, fontsize=48): # figure mosaic as above (but now labeled with ``{"A", "B", "C", # "D"}`` rather than ``{"bar", "plot", "hist", "image"}``). -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout="constrained") ax_dict = fig.subplot_mosaic(mosaic) identify_axes(ax_dict) @@ -128,7 +128,7 @@ def identify_axes(ax_dict, fontsize=48): # will give you the same composition, where the ``";"`` is used # as the row separator instead of newline. -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout="constrained") ax_dict = fig.subplot_mosaic(mosaic) identify_axes(ax_dict) @@ -145,7 +145,7 @@ def identify_axes(ax_dict, fontsize=48): # If we want to re-arrange our four Axes to have ``"C"`` be a horizontal # span on the bottom and ``"D"`` be a vertical span on the right we would do -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( """ ABD CCD @@ -158,7 +158,7 @@ def identify_axes(ax_dict, fontsize=48): # we can specify some spaces in the grid to be blank -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( """ A.C BBB @@ -173,7 +173,7 @@ def identify_axes(ax_dict, fontsize=48): # to mark the empty space, we can use *empty_sentinel* to specify the # character to use. -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( """ aX Xb @@ -188,7 +188,7 @@ def identify_axes(ax_dict, fontsize=48): # Internally there is no meaning attached to the letters we use, any # Unicode code point is valid! -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( """αб ℝ☢""" ) @@ -212,7 +212,7 @@ def identify_axes(ax_dict, fontsize=48): # `.Figure.subplot_mosaic` calling sequence. -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( """ .a. bAc @@ -265,7 +265,7 @@ def identify_axes(ax_dict, fontsize=48): mosaic = """AA BC""" -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout="constrained") left, right = fig.subfigures(nrows=1, ncols=2) axd = left.subplot_mosaic(mosaic) identify_axes(axd) @@ -283,7 +283,7 @@ def identify_axes(ax_dict, fontsize=48): # of the Axes created. -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( "AB", subplot_kw={"projection": "polar"} ) identify_axes(axd) @@ -330,7 +330,7 @@ def identify_axes(ax_dict, fontsize=48): # merged with *per_subplot_kw* taking priority: -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( "AB;CD", subplot_kw={"facecolor": "xkcd:tangerine"}, per_subplot_kw={ @@ -349,7 +349,7 @@ def identify_axes(ax_dict, fontsize=48): # passing in a list (internally we convert the string shorthand to a nested # list), for example using spans, blanks, and *gridspec_kw*: -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( [ ["main", "zoom"], ["main", "BLANK"], @@ -373,7 +373,7 @@ def identify_axes(ax_dict, fontsize=48): ["main", inner], ["bottom", "bottom"], ] -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( outer_nested_mosaic, empty_sentinel=None ) identify_axes(axd, fontsize=36) @@ -384,7 +384,7 @@ def identify_axes(ax_dict, fontsize=48): mosaic = np.zeros((4, 4), dtype=int) for j in range(4): mosaic[j, j] = j + 1 -axd = plt.figure(constrained_layout=True).subplot_mosaic( +axd = plt.figure(layout="constrained").subplot_mosaic( mosaic, empty_sentinel=0, ) diff --git a/examples/subplots_axes_and_figures/secondary_axis.py b/examples/subplots_axes_and_figures/secondary_axis.py index a8ba17d3478b..464a88aa1064 100644 --- a/examples/subplots_axes_and_figures/secondary_axis.py +++ b/examples/subplots_axes_and_figures/secondary_axis.py @@ -17,7 +17,7 @@ import matplotlib.dates as mdates from matplotlib.ticker import AutoMinorLocator -fig, ax = plt.subplots(constrained_layout=True) +fig, ax = plt.subplots(layout='constrained') x = np.arange(0, 360, 1) y = np.sin(2 * x * np.pi / 180) ax.plot(x, y) @@ -47,7 +47,7 @@ def rad2deg(x): # In this case, the xscale of the parent is logarithmic, so the child is # made logarithmic as well. -fig, ax = plt.subplots(constrained_layout=True) +fig, ax = plt.subplots(layout='constrained') x = np.arange(0.02, 1, 0.02) np.random.seed(19680801) y = np.random.randn(len(x)) ** 2 @@ -91,7 +91,7 @@ def one_over(x): # arguments *left*, *right* such that values outside the data range are # mapped well outside the plot limits. -fig, ax = plt.subplots(constrained_layout=True) +fig, ax = plt.subplots(layout='constrained') xdata = np.arange(1, 11, 0.4) ydata = np.random.randn(len(xdata)) ax.plot(xdata, ydata, label='Plotted data') @@ -129,7 +129,7 @@ def inverse(x): dates = [datetime.datetime(2018, 1, 1) + datetime.timedelta(hours=k * 6) for k in range(240)] temperature = np.random.randn(len(dates)) * 4 + 6.7 -fig, ax = plt.subplots(constrained_layout=True) +fig, ax = plt.subplots(layout='constrained') ax.plot(dates, temperature) ax.set_ylabel(r'$T\ [^oC]$') diff --git a/examples/subplots_axes_and_figures/subfigures.py b/examples/subplots_axes_and_figures/subfigures.py index 1e7ae401554b..4a95932fe7f5 100644 --- a/examples/subplots_axes_and_figures/subfigures.py +++ b/examples/subplots_axes_and_figures/subfigures.py @@ -31,7 +31,7 @@ def example_plot(ax, fontsize=12, hide_labels=False): np.random.seed(19680808) # gridspec inside gridspec -fig = plt.figure(constrained_layout=True, figsize=(10, 4)) +fig = plt.figure(layout='constrained', figsize=(10, 4)) subfigs = fig.subfigures(1, 2, wspace=0.07) axsLeft = subfigs[0].subplots(1, 2, sharey=True) @@ -62,7 +62,7 @@ def example_plot(ax, fontsize=12, hide_labels=False): # `matplotlib.figure.Figure.add_subfigure`. This requires getting # the gridspec that the subplots are laid out on. -fig, axs = plt.subplots(2, 3, constrained_layout=True, figsize=(10, 4)) +fig, axs = plt.subplots(2, 3, layout='constrained', figsize=(10, 4)) gridspec = axs[0, 0].get_subplotspec().get_gridspec() # clear the left column for the subfigure: @@ -90,7 +90,7 @@ def example_plot(ax, fontsize=12, hide_labels=False): # Subfigures can have different widths and heights. This is exactly the # same example as the first example, but *width_ratios* has been changed: -fig = plt.figure(constrained_layout=True, figsize=(10, 4)) +fig = plt.figure(layout='constrained', figsize=(10, 4)) subfigs = fig.subfigures(1, 2, wspace=0.07, width_ratios=[2, 1]) axsLeft = subfigs[0].subplots(1, 2, sharey=True) @@ -119,7 +119,7 @@ def example_plot(ax, fontsize=12, hide_labels=False): ############################################################################## # Subfigures can be also be nested: -fig = plt.figure(constrained_layout=True, figsize=(10, 8)) +fig = plt.figure(layout='constrained', figsize=(10, 8)) fig.suptitle('fig') diff --git a/examples/text_labels_and_annotations/arrow_demo.py b/examples/text_labels_and_annotations/arrow_demo.py index cf9e86d12c3b..9607818181dc 100644 --- a/examples/text_labels_and_annotations/arrow_demo.py +++ b/examples/text_labels_and_annotations/arrow_demo.py @@ -148,7 +148,7 @@ def make_arrow_graph(ax, data, size=4, display='length', shape='right', } size = 4 - fig = plt.figure(figsize=(3 * size, size), constrained_layout=True) + fig = plt.figure(figsize=(3 * size, size), layout="constrained") axs = fig.subplot_mosaic([["length", "width", "alpha"]]) for display, ax in axs.items(): diff --git a/examples/text_labels_and_annotations/date.py b/examples/text_labels_and_annotations/date.py index 9c72972a04e4..1bce29c2f5f1 100644 --- a/examples/text_labels_and_annotations/date.py +++ b/examples/text_labels_and_annotations/date.py @@ -32,7 +32,7 @@ # the date column. data = cbook.get_sample_data('goog.npz', np_load=True)['price_data'] -fig, axs = plt.subplots(3, 1, figsize=(6.4, 7), constrained_layout=True) +fig, axs = plt.subplots(3, 1, figsize=(6.4, 7), layout='constrained') # common to all three: for ax in axs: ax.plot('date', 'adj_close', data=data) diff --git a/examples/text_labels_and_annotations/figlegend_demo.py b/examples/text_labels_and_annotations/figlegend_demo.py index c749ae795cd5..e14d242bdd73 100644 --- a/examples/text_labels_and_annotations/figlegend_demo.py +++ b/examples/text_labels_and_annotations/figlegend_demo.py @@ -31,8 +31,8 @@ ############################################################################## # Sometimes we do not want the legend to overlap the axes. If you use -# constrained_layout you can specify "outside right upper", and -# constrained_layout will make room for the legend. +# *constrained layout* you can specify "outside right upper", and +# *constrained layout* will make room for the legend. fig, axs = plt.subplots(1, 2, layout='constrained') diff --git a/examples/text_labels_and_annotations/label_subplots.py b/examples/text_labels_and_annotations/label_subplots.py index 974b775a9a97..b74e9a9d7437 100644 --- a/examples/text_labels_and_annotations/label_subplots.py +++ b/examples/text_labels_and_annotations/label_subplots.py @@ -17,7 +17,7 @@ import matplotlib.transforms as mtransforms fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']], - constrained_layout=True) + layout='constrained') for label, ax in axs.items(): # label physical distance in and down: @@ -33,7 +33,7 @@ # with each other, in which case we use a slightly different transform: fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']], - constrained_layout=True) + layout='constrained') for label, ax in axs.items(): # label physical distance to the left and up: @@ -48,7 +48,7 @@ # use the *loc* keyword argument: fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']], - constrained_layout=True) + layout='constrained') for label, ax in axs.items(): ax.set_title('Normal Title', fontstyle='italic') diff --git a/examples/text_labels_and_annotations/legend_demo.py b/examples/text_labels_and_annotations/legend_demo.py index 11df93095fe4..13b846389280 100644 --- a/examples/text_labels_and_annotations/legend_demo.py +++ b/examples/text_labels_and_annotations/legend_demo.py @@ -63,7 +63,7 @@ ############################################################################### # Here we attach legends to more complex plots. -fig, axs = plt.subplots(3, 1, constrained_layout=True) +fig, axs = plt.subplots(3, 1, layout="constrained") top_ax, middle_ax, bottom_ax = axs top_ax.bar([0, 1, 2], [0.2, 0.3, 0.1], width=0.4, label="Bar 1", @@ -86,7 +86,7 @@ ############################################################################### # Now we'll showcase legend entries with more than one legend key. -fig, (ax1, ax2) = plt.subplots(2, 1, constrained_layout=True) +fig, (ax1, ax2) = plt.subplots(2, 1, layout='constrained') # First plot: two legend keys for a single entry p1 = ax1.scatter([1], [5], c='r', marker='s', s=100) diff --git a/examples/text_labels_and_annotations/titles_demo.py b/examples/text_labels_and_annotations/titles_demo.py index 57323aac38e3..4bf75a87adea 100644 --- a/examples/text_labels_and_annotations/titles_demo.py +++ b/examples/text_labels_and_annotations/titles_demo.py @@ -21,7 +21,7 @@ # The vertical position is automatically chosen to avoid decorations # (i.e. labels and ticks) on the topmost x-axis: -fig, axs = plt.subplots(1, 2, constrained_layout=True) +fig, axs = plt.subplots(1, 2, layout='constrained') ax = axs[0] ax.plot(range(10)) @@ -41,7 +41,7 @@ # Automatic positioning can be turned off by manually specifying the *y* # keyword argument for the title or setting :rc:`axes.titley` in the rcParams. -fig, axs = plt.subplots(1, 2, constrained_layout=True) +fig, axs = plt.subplots(1, 2, layout='constrained') ax = axs[0] ax.plot(range(10)) diff --git a/examples/ticks/date_concise_formatter.py b/examples/ticks/date_concise_formatter.py index 979214b3b406..10b239affac2 100644 --- a/examples/ticks/date_concise_formatter.py +++ b/examples/ticks/date_concise_formatter.py @@ -29,7 +29,7 @@ np.random.seed(19680801) y = np.cumsum(np.random.randn(N)) -fig, axs = plt.subplots(3, 1, constrained_layout=True, figsize=(6, 6)) +fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(6, 6)) lims = [(np.datetime64('2005-02'), np.datetime64('2005-04')), (np.datetime64('2005-02-03'), np.datetime64('2005-02-15')), (np.datetime64('2005-02-03 11:00'), np.datetime64('2005-02-04 13:20'))] @@ -49,7 +49,7 @@ # for this example the labels do not need to be rotated as they do for the # default formatter because the labels are as small as possible. -fig, axs = plt.subplots(3, 1, constrained_layout=True, figsize=(6, 6)) +fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(6, 6)) for nn, ax in enumerate(axs): locator = mdates.AutoDateLocator(minticks=3, maxticks=7) formatter = mdates.ConciseDateFormatter(locator) @@ -73,7 +73,7 @@ munits.registry[datetime.date] = converter munits.registry[datetime.datetime] = converter -fig, axs = plt.subplots(3, 1, figsize=(6, 6), constrained_layout=True) +fig, axs = plt.subplots(3, 1, figsize=(6, 6), layout='constrained') for nn, ax in enumerate(axs): ax.plot(dates, y) ax.set_xlim(lims[nn]) @@ -106,7 +106,7 @@ # Here we modify the labels to be "day month year", instead of the ISO # "year month day": -fig, axs = plt.subplots(3, 1, constrained_layout=True, figsize=(6, 6)) +fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(6, 6)) for nn, ax in enumerate(axs): locator = mdates.AutoDateLocator() @@ -172,7 +172,7 @@ munits.registry[datetime.date] = converter munits.registry[datetime.datetime] = converter -fig, axs = plt.subplots(3, 1, constrained_layout=True, figsize=(6, 6)) +fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(6, 6)) for nn, ax in enumerate(axs): ax.plot(dates, y) ax.set_xlim(lims[nn]) diff --git a/examples/ticks/date_index_formatter.py b/examples/ticks/date_index_formatter.py index a2bf4b8796f9..fb01448fb151 100644 --- a/examples/ticks/date_index_formatter.py +++ b/examples/ticks/date_index_formatter.py @@ -25,12 +25,11 @@ # low, close, volume, adj_close from the mpl-data/sample_data directory. The # record array stores the date as an np.datetime64 with a day unit ('D') in # the date column (``r.date``). -r = (cbook.get_sample_data('goog.npz', np_load=True)['price_data'] - .view(np.recarray)) +r = cbook.get_sample_data('goog.npz', np_load=True)['price_data'].view(np.recarray) r = r[:9] # get the first 9 days -fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(6, 6), - constrained_layout={'hspace': .15}) +fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(6, 6), layout='constrained') +fig.get_layout_engine().set(hspace=0.15) # First we'll do it the default way, with gaps on weekends ax1.plot(r.date, r.adj_close, 'o-') diff --git a/examples/ticks/date_precision_and_epochs.py b/examples/ticks/date_precision_and_epochs.py index c50bbd234331..0f4f993f6f04 100644 --- a/examples/ticks/date_precision_and_epochs.py +++ b/examples/ticks/date_precision_and_epochs.py @@ -128,7 +128,7 @@ def _reset_epoch_for_tutorial(): _reset_epoch_for_tutorial() # Don't do this. Just for this tutorial. mdates.set_epoch(new_epoch) -fig, ax = plt.subplots(constrained_layout=True) +fig, ax = plt.subplots(layout='constrained') ax.plot(xold, y) ax.set_title('Epoch: ' + mdates.get_epoch()) ax.xaxis.set_tick_params(rotation=40) @@ -137,7 +137,7 @@ def _reset_epoch_for_tutorial(): ############################################################################# # For dates plotted using the more recent epoch, the plot is smooth: -fig, ax = plt.subplots(constrained_layout=True) +fig, ax = plt.subplots(layout='constrained') ax.plot(x, y) ax.set_title('Epoch: ' + mdates.get_epoch()) ax.xaxis.set_tick_params(rotation=40) diff --git a/examples/ticks/ticks_too_many.py b/examples/ticks/ticks_too_many.py index c1f05bf4c17e..29bde86a4582 100644 --- a/examples/ticks/ticks_too_many.py +++ b/examples/ticks/ticks_too_many.py @@ -21,7 +21,7 @@ import matplotlib.pyplot as plt import numpy as np -fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2.5)) +fig, ax = plt.subplots(1, 2, layout='constrained', figsize=(6, 2.5)) x = ['1', '5', '2', '3'] y = [1, 4, 2, 3] ax[0].plot(x, y, 'd') @@ -60,7 +60,7 @@ # converted from strings to datetime objects to get the proper date locators # and formatters. -fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(6, 2.75)) +fig, ax = plt.subplots(1, 2, layout='constrained', figsize=(6, 2.75)) x = ['2021-10-01', '2021-11-02', '2021-12-03', '2021-09-01'] y = [0, 2, 3, 1] ax[0].plot(x, y, 'd') diff --git a/examples/units/units_sample.py b/examples/units/units_sample.py index 81547601e711..13448a30883e 100644 --- a/examples/units/units_sample.py +++ b/examples/units/units_sample.py @@ -19,7 +19,7 @@ cms = cm * np.arange(0, 10, 2) -fig, axs = plt.subplots(2, 2, constrained_layout=True) +fig, axs = plt.subplots(2, 2, layout='constrained') axs[0, 0].plot(cms, cms) diff --git a/examples/user_interfaces/gtk4_spreadsheet_sgskip.py b/examples/user_interfaces/gtk4_spreadsheet_sgskip.py index 509789f5aedb..c3f053480a71 100644 --- a/examples/user_interfaces/gtk4_spreadsheet_sgskip.py +++ b/examples/user_interfaces/gtk4_spreadsheet_sgskip.py @@ -49,7 +49,7 @@ def __init__(self, *args, **kwargs): sw.set_child(self.treeview) # Matplotlib stuff - fig = Figure(figsize=(6, 4), constrained_layout=True) + fig = Figure(figsize=(6, 4), layout='constrained') self.canvas = FigureCanvas(fig) # a Gtk.DrawingArea self.canvas.set_hexpand(True) diff --git a/examples/userdemo/connectionstyle_demo.py b/examples/userdemo/connectionstyle_demo.py index f9def1d94f24..45e1e605e3e4 100644 --- a/examples/userdemo/connectionstyle_demo.py +++ b/examples/userdemo/connectionstyle_demo.py @@ -30,7 +30,7 @@ def demo_con_style(ax, connectionstyle): transform=ax.transAxes, ha="left", va="top") -fig, axs = plt.subplots(3, 5, figsize=(7, 6.3), constrained_layout=True) +fig, axs = plt.subplots(3, 5, figsize=(7, 6.3), layout="constrained") demo_con_style(axs[0, 0], "angle3,angleA=90,angleB=0") demo_con_style(axs[1, 0], "angle3,angleA=0,angleB=90") demo_con_style(axs[0, 1], "arc3,rad=0.") diff --git a/examples/widgets/rectangle_selector.py b/examples/widgets/rectangle_selector.py index 8ede9ad66fc4..f43f9e56fca3 100644 --- a/examples/widgets/rectangle_selector.py +++ b/examples/widgets/rectangle_selector.py @@ -40,7 +40,7 @@ def toggle_selector(event): selector.set_active(True) -fig = plt.figure(constrained_layout=True) +fig = plt.figure(layout='constrained') axs = fig.subplots(2) N = 100000 # If N is large one can see improvement by using blitting. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy