From 3c00cc2fb0cbf6fce90362ef5cc122e263bf30d4 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sat, 4 Dec 2021 16:05:36 +0100 Subject: [PATCH 1/3] Exclude internal styles from style sheet reference Also make the style name a bit larger and bold so that it's easier to read. --- examples/style_sheets/style_sheets_reference.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/style_sheets/style_sheets_reference.py b/examples/style_sheets/style_sheets_reference.py index a3d6e3dd9ac9..e73a9ff8757e 100644 --- a/examples/style_sheets/style_sheets_reference.py +++ b/examples/style_sheets/style_sheets_reference.py @@ -112,7 +112,7 @@ def plot_figure(style_label=""): fig, axs = plt.subplots(ncols=6, nrows=1, num=style_label, figsize=fig_size, squeeze=True) - axs[0].set_ylabel(style_label) + axs[0].set_ylabel(style_label, fontsize=13, fontweight='bold') plot_scatter(axs[0], prng) plot_image_and_patch(axs[1], prng) @@ -131,8 +131,11 @@ def plot_figure(style_label=""): # 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. + # styles with leading underscores are for internal use such as testing + # and plot types gallery. These are excluded here. 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 not style.startswith('_')) # Plot a demonstration figure for every available style sheet. for style_label in style_list: From b3714634cbb168cc48752580a93c9a244aad3dea Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sat, 4 Dec 2021 16:28:29 +0100 Subject: [PATCH 2/3] Relpace the sinosoidal lines in the style sheet reference with less overlapping lines to make the color cycle more obvious. Co-authored-by: Jody Klymak --- .../style_sheets/style_sheets_reference.py | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/examples/style_sheets/style_sheets_reference.py b/examples/style_sheets/style_sheets_reference.py index e73a9ff8757e..439269b1a6ae 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) @@ -26,15 +27,19 @@ def plot_scatter(ax, prng, nb_samples=100): return ax -def plot_colored_sinusoidal_lines(ax): - """Plot sinusoidal lines with colors following the style color cycle.""" - L = 2 * np.pi - x = np.linspace(0, L) +def plot_colored_lines(ax): + """Plot lines with colors following the style color cycle.""" + t = np.linspace(-10, 10, 100) + + def sigmoid(t, t0): + return 1 / (1 + np.exp(-(t - t0))) + nb_colors = len(plt.rcParams['axes.prop_cycle']) - shift = np.linspace(0, L, nb_colors, endpoint=False) - for s in shift: - ax.plot(x, np.sin(x + s), '-') - ax.set_xlim([x[0], x[-1]]) + shifts = np.linspace(-5, 5, nb_colors) + amplitudes = np.linspace(1, 1.5, nb_colors) + for t0, a in zip(shifts, amplitudes): + ax.plot(t, a * sigmoid(t, t0), '-') + ax.set_xlim(-10, 10) return ax @@ -108,23 +113,30 @@ def plot_figure(style_label=""): # 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_size = [fig_width * 2, fig_height / 1.75] fig, axs = plt.subplots(ncols=6, nrows=1, num=style_label, - figsize=fig_size, squeeze=True) - axs[0].set_ylabel(style_label, fontsize=13, fontweight='bold') + figsize=fig_size, constrained_layout=True) + + # 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] + fig.suptitle(style_label, x=0.01, fontsize=14, ha='left', + color=col, fontfamily='DejaVu Sans', + fontweight='normal') 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_colored_lines(axs[4]) plot_histograms(axs[5], prng) - fig.tight_layout() - - return fig - if __name__ == "__main__": @@ -141,6 +153,6 @@ def plot_figure(style_label=""): for style_label in 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) + plot_figure(style_label=style_label) plt.show() From ad1b222791f560679f317b7fcf20537d8c51f901 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 17 Dec 2021 00:11:02 +0100 Subject: [PATCH 3/3] Fix the figure size in style sheet reference It's better to override absolute sizes in the style sheet. Otherwise, larger figure images are scaled down due to the limited available width in the html theme. If not, we still don't get a size relation, but we also have different magnifications due to the image scaling. Notes: - The width (7.4 -> 740px, assuming 100 dpi) is chosen to fit in the HTML without scaling. - The size is chosen to approximately keep the aspect ratio as before. - The images are now significantly smaller than previously (all former images were downscaled). We'll have to verify that the images still look good given a lot of information and not much space. If not, we need to add a factor. - All themes use figure.dpi=100, except "classic", which uses 80. Not correcting for that is intentional. IMHO "classic" should come out smaller to highlight the change in dpi. --- .../style_sheets/style_sheets_reference.py | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/examples/style_sheets/style_sheets_reference.py b/examples/style_sheets/style_sheets_reference.py index 439269b1a6ae..657c73aadb10 100644 --- a/examples/style_sheets/style_sheets_reference.py +++ b/examples/style_sheets/style_sheets_reference.py @@ -109,26 +109,19 @@ def plot_figure(style_label=""): # 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 / 1.75] - fig, axs = plt.subplots(ncols=6, nrows=1, num=style_label, - figsize=fig_size, constrained_layout=True) + figsize=(14.8, 2.7), constrained_layout=True) # 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( + # except those with dark backgrounds, which get a lighter color: + background_color = mcolors.rgb_to_hsv( mcolors.to_rgb(plt.rcParams['figure.facecolor']))[2] - if back < 0.5: - col = [0.8, 0.8, 1] - fig.suptitle(style_label, x=0.01, fontsize=14, ha='left', - color=col, fontfamily='DejaVu Sans', - fontweight='normal') + if background_color < 0.5: + title_color = [0.8, 0.8, 1] + else: + title_color = np.array([19, 6, 84]) / 256 + fig.suptitle(style_label, x=0.01, ha='left', color=title_color, + fontsize=14, fontfamily='DejaVu Sans', fontweight='normal') plot_scatter(axs[0], prng) plot_image_and_patch(axs[1], prng)