Skip to content

Backport PR #25198 - DOC: remove constrained_layout kwarg from examples #25215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/axisartist/simple_axisartist1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions examples/images_contours_and_fields/contourf_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'),
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/images_contours_and_fields/image_antialiasing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}'")
Expand Down
2 changes: 1 addition & 1 deletion examples/images_contours_and_fields/image_nonuniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions examples/images_contours_and_fields/pcolormesh_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion examples/lines_bars_and_markers/barchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions examples/lines_bars_and_markers/curve_error_band.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions examples/lines_bars_and_markers/markevery_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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}')
Expand Down
4 changes: 2 additions & 2 deletions examples/lines_bars_and_markers/psd_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion examples/lines_bars_and_markers/scatter_hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion examples/lines_bars_and_markers/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/rasterization_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/scales/asinh_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down
3 changes: 1 addition & 2 deletions examples/scales/scales.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions examples/shapes_and_collections/hatch_style_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '.', '*']

Expand All @@ -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', '..', '**']

Expand All @@ -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.', '*-']

Expand Down
2 changes: 1 addition & 1 deletion examples/spines/spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion examples/statistics/barchart_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/statistics/time_series_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/style_sheets/style_sheets_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions examples/subplots_axes_and_figures/axes_box_aspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions examples/subplots_axes_and_figures/colorbar_placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand All @@ -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):
Expand All @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions examples/subplots_axes_and_figures/demo_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -23,25 +23,25 @@ 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)

###############################################################################
# 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

Expand Down
4 changes: 2 additions & 2 deletions examples/subplots_axes_and_figures/figure_title.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion examples/subplots_axes_and_figures/gridspec_multicolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, :])
Expand Down
Loading