From 4c67ce5f30bbff53672c3fcce0f2a8cbf864e466 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 29 Nov 2021 12:54:04 +0100 Subject: [PATCH 1/3] DOC: fix stylesheet again Co-authored by: David Stansby --- .../style_sheets/style_sheets_reference.py | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/examples/style_sheets/style_sheets_reference.py b/examples/style_sheets/style_sheets_reference.py index a3d6e3dd9ac9..26af51dfa628 100644 --- a/examples/style_sheets/style_sheets_reference.py +++ b/examples/style_sheets/style_sheets_reference.py @@ -98,21 +98,13 @@ 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) + axs[0].figure.set_facecolor(plt.rcParams['figure.facecolor']) + axs[0].set_ylabel('ylabel') plot_scatter(axs[0], prng) plot_image_and_patch(axs[1], prng) @@ -121,23 +113,25 @@ def plot_figure(style_label=""): plot_colored_sinusoidal_lines(axs[4]) plot_histograms(axs[5], prng) - fig.tight_layout() - - return fig - 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) + sfig.suptitle(style_label) plt.show() From f4732284cba91a173c450cfeb6b94c6c49b6580d Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 29 Nov 2021 15:49:31 +0100 Subject: [PATCH 2/3] DOC: fix suptitle --- examples/style_sheets/style_sheets_reference.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/style_sheets/style_sheets_reference.py b/examples/style_sheets/style_sheets_reference.py index 26af51dfa628..cdda03b0fffc 100644 --- a/examples/style_sheets/style_sheets_reference.py +++ b/examples/style_sheets/style_sheets_reference.py @@ -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) @@ -112,7 +113,13 @@ def plot_figure(axs, style_label=""): plot_colored_circles(axs[3], prng) plot_colored_sinusoidal_lines(axs[4]) plot_histograms(axs[5], prng) - + 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.03, fontsize=12, + ha='left', color=col) if __name__ == "__main__": @@ -133,5 +140,4 @@ def plot_figure(axs, style_label=""): with plt.style.context(style_label): ax = sfig.subplots(1, 6) plot_figure(ax, style_label=style_label) - sfig.suptitle(style_label) plt.show() From 25b881ffcaddbf6d42136695b44be538a59ab2dc Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Tue, 30 Nov 2021 07:13:56 +0100 Subject: [PATCH 3/3] DOC: more comments --- examples/style_sheets/style_sheets_reference.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/style_sheets/style_sheets_reference.py b/examples/style_sheets/style_sheets_reference.py index cdda03b0fffc..c20bad4aac99 100644 --- a/examples/style_sheets/style_sheets_reference.py +++ b/examples/style_sheets/style_sheets_reference.py @@ -104,6 +104,8 @@ def plot_figure(axs, style_label=""): # Use a dedicated RandomState instance to draw the same "random" values # across the different figures. prng = np.random.RandomState(96917002) + # the facecolor rcparam is not applied to subfigures, so + # do so manually here: axs[0].figure.set_facecolor(plt.rcParams['figure.facecolor']) axs[0].set_ylabel('ylabel') @@ -113,13 +115,17 @@ def plot_figure(axs, style_label=""): plot_colored_circles(axs[3], prng) plot_colored_sinusoidal_lines(axs[4]) plot_histograms(axs[5], prng) + # 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.03, fontsize=12, - ha='left', color=col) + axs[0].figure.suptitle(style_label, x=0.015, fontsize=14, ha='left', + color=col, fontfamily='DejaVu Sans', + fontweight='normal') if __name__ == "__main__":