Skip to content

DOC: fix stylesheet again #21796

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

Closed
wants to merge 3 commits into from
Closed
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
50 changes: 28 additions & 22 deletions examples/style_sheets/style_sheets_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

# Fixing random state for reproducibility
np.random.seed(19680801)
Expand Down Expand Up @@ -98,46 +99,51 @@ def plot_histograms(ax, prng, nb_samples=10000):
return ax


def plot_figure(style_label=""):
def plot_figure(axs, style_label=""):
"""Setup and plot the demonstration figure with a given style."""
# Use a dedicated RandomState instance to draw the same "random" values
# across the different figures.
prng = np.random.RandomState(96917002)

# Tweak the figure size to be better suited for a row of numerous plots:
# double the width and halve the height. NB: use relative changes because
# some styles may have a figure size different from the default one.
(fig_width, fig_height) = plt.rcParams['figure.figsize']
fig_size = [fig_width * 2, fig_height / 2]

fig, axs = plt.subplots(ncols=6, nrows=1, num=style_label,
figsize=fig_size, squeeze=True)
axs[0].set_ylabel(style_label)
# the facecolor rcparam is not applied to subfigures, so
# do so manually here:
axs[0].figure.set_facecolor(plt.rcParams['figure.facecolor'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a workaround because all subfigures are created with one style. This will not be needed for standard use cases where one has one figure only. I think this workaround needs a comment.

Are there more figure-based parameters that would need to be adapted?

axs[0].set_ylabel('ylabel')

plot_scatter(axs[0], prng)
plot_image_and_patch(axs[1], prng)
plot_bar_graphs(axs[2], prng)
plot_colored_circles(axs[3], prng)
plot_colored_sinusoidal_lines(axs[4])
plot_histograms(axs[5], prng)

fig.tight_layout()

return fig

# make a suptitle, in the same style for all subfigures,
# except those with dark backgrounds, which get a lighter
# color:
col = np.array([19, 6, 84])/256
back = mcolors.rgb_to_hsv(
mcolors.to_rgb(plt.rcParams['figure.facecolor']))[2]
if back < 0.5:
col = [0.8, 0.8, 1]
axs[0].figure.suptitle(style_label, x=0.015, fontsize=14, ha='left',
color=col, fontfamily='DejaVu Sans',
fontweight='normal')

if __name__ == "__main__":

# Setup a list of all available styles, in alphabetical order but
# the `default` and `classic` ones, which will be forced resp. in
# first and second position.
# the `default`, `classic`, which will be forced
# resp. in first and second position.
style_list = ['default', 'classic'] + sorted(
style for style in plt.style.available if style != 'classic')
style for style in plt.style.available if
((style != 'classic') and (style[0] != '_')))
nstyles = len(style_list)

fig = plt.figure(figsize=(7, nstyles*2), constrained_layout=True)
subfigs = fig.subfigures(nstyles, 1)
# Plot a demonstration figure for every available style sheet.
for style_label in style_list:
for sfig, style_label in zip(subfigs, style_list):

with plt.rc_context({"figure.max_open_warning": len(style_list)}):
with plt.style.context(style_label):
fig = plot_figure(style_label=style_label)

ax = sfig.subplots(1, 6)
plot_figure(ax, style_label=style_label)
plt.show()