From a43f2178f4b706a20eea9d6b10806a9e644cfdd3 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Thu, 26 Sep 2024 00:02:04 +0200 Subject: [PATCH] DOC: Better visualization for the default color cycle example The current visualization is quite messy ( https://matplotlib.org/stable/gallery/color/color_cycle_default.html). Let's focus on: - giving a one clear sequence - giving the color names as 'CN' notation and named colors - showing lines and patches (colors appear substantially different in thin lines and filled areas) And don't bother with: - multiple line widths - they are only a slight visual variation (compared to patches) and multiple widths clutter the example - black background: It's enough to show the default color cycle on the default background. --- .../examples/color/color_cycle_default.py | 48 ++++++++----------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/galleries/examples/color/color_cycle_default.py b/galleries/examples/color/color_cycle_default.py index 16f6634937c0..cec280d4c603 100644 --- a/galleries/examples/color/color_cycle_default.py +++ b/galleries/examples/color/color_cycle_default.py @@ -6,36 +6,29 @@ Display the colors from the default prop_cycle, which is obtained from the :ref:`rc parameters`. """ -import matplotlib.pyplot as plt import numpy as np +import matplotlib.pyplot as plt +from matplotlib.colors import TABLEAU_COLORS, same_color -prop_cycle = plt.rcParams['axes.prop_cycle'] -colors = prop_cycle.by_key()['color'] -lwbase = plt.rcParams['lines.linewidth'] -thin = lwbase / 2 -thick = lwbase * 3 +def f(x, a): + return 0.85 * a * (1 / (1 + np.exp(-x)) + 0.2) -fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True) -for icol in range(2): - if icol == 0: - lwx, lwy = thin, lwbase - else: - lwx, lwy = lwbase, thick - for irow in range(2): - for i, color in enumerate(colors): - axs[irow, icol].axhline(i, color=color, lw=lwx) - axs[irow, icol].axvline(i, color=color, lw=lwy) - axs[1, icol].set_facecolor('k') - axs[1, icol].xaxis.set_ticks(np.arange(0, 10, 2)) - axs[0, icol].set_title(f'line widths (pts): {lwx:g}, {lwy:g}', - fontsize='medium') +fig, ax = plt.subplots() +ax.axis('off') +ax.set_title("Colors in the default property cycle") -for irow in range(2): - axs[irow, 0].yaxis.set_ticks(np.arange(0, 10, 2)) +prop_cycle = plt.rcParams['axes.prop_cycle'] +colors = prop_cycle.by_key()['color'] +x = np.linspace(-4, 4, 200) -fig.suptitle('Colors in the default prop_cycle', fontsize='large') +for i, (color, color_name) in enumerate(zip(colors, TABLEAU_COLORS)): + assert same_color(color, color_name) + pos = i - 4.5 + ax.plot(x, f(x, pos)) + ax.text(4.2, pos, f"'C{i}': '{color_name}'", color=color, va="center") + ax.bar(9, 1, width=1.5, bottom=pos-0.5) plt.show() @@ -46,14 +39,13 @@ # The use of the following functions, methods, classes and modules is shown # in this example: # -# - `matplotlib.axes.Axes.axhline` / `matplotlib.pyplot.axhline` -# - `matplotlib.axes.Axes.axvline` / `matplotlib.pyplot.axvline` -# - `matplotlib.axes.Axes.set_facecolor` -# - `matplotlib.figure.Figure.suptitle` +# - `matplotlib.axes.Axes.axis` +# - `matplotlib.axes.Axes.text` +# - `matplotlib.color.same_color` +# - `cycler.Cycler` # # .. tags:: # # styling: color -# styling: colormap # plot-type: line # level: beginner