Skip to content

Commit 7722dea

Browse files
authored
Merge pull request matplotlib#28888 from timhoffm/doc-colors
DOC: Better visualization for the default color cycle example
2 parents ab96b8f + 582459f commit 7722dea

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

galleries/examples/color/color_cycle_default.py

+22-27
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,28 @@
99
import matplotlib.pyplot as plt
1010
import numpy as np
1111

12-
prop_cycle = plt.rcParams['axes.prop_cycle']
13-
colors = prop_cycle.by_key()['color']
12+
from matplotlib.colors import TABLEAU_COLORS, same_color
13+
1414

15-
lwbase = plt.rcParams['lines.linewidth']
16-
thin = lwbase / 2
17-
thick = lwbase * 3
15+
def f(x, a):
16+
"""A nice sigmoid-like parametrized curve, ending approximately at *a*."""
17+
return 0.85 * a * (1 / (1 + np.exp(-x)) + 0.2)
1818

19-
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
20-
for icol in range(2):
21-
if icol == 0:
22-
lwx, lwy = thin, lwbase
23-
else:
24-
lwx, lwy = lwbase, thick
25-
for irow in range(2):
26-
for i, color in enumerate(colors):
27-
axs[irow, icol].axhline(i, color=color, lw=lwx)
28-
axs[irow, icol].axvline(i, color=color, lw=lwy)
2919

30-
axs[1, icol].set_facecolor('k')
31-
axs[1, icol].xaxis.set_ticks(np.arange(0, 10, 2))
32-
axs[0, icol].set_title(f'line widths (pts): {lwx:g}, {lwy:g}',
33-
fontsize='medium')
20+
fig, ax = plt.subplots()
21+
ax.axis('off')
22+
ax.set_title("Colors in the default property cycle")
3423

35-
for irow in range(2):
36-
axs[irow, 0].yaxis.set_ticks(np.arange(0, 10, 2))
24+
prop_cycle = plt.rcParams['axes.prop_cycle']
25+
colors = prop_cycle.by_key()['color']
26+
x = np.linspace(-4, 4, 200)
3727

38-
fig.suptitle('Colors in the default prop_cycle', fontsize='large')
28+
for i, (color, color_name) in enumerate(zip(colors, TABLEAU_COLORS)):
29+
assert same_color(color, color_name)
30+
pos = 4.5 - i
31+
ax.plot(x, f(x, pos))
32+
ax.text(4.2, pos, f"'C{i}': '{color_name}'", color=color, va="center")
33+
ax.bar(9, 1, width=1.5, bottom=pos-0.5)
3934

4035
plt.show()
4136

@@ -46,14 +41,14 @@
4641
# The use of the following functions, methods, classes and modules is shown
4742
# in this example:
4843
#
49-
# - `matplotlib.axes.Axes.axhline` / `matplotlib.pyplot.axhline`
50-
# - `matplotlib.axes.Axes.axvline` / `matplotlib.pyplot.axvline`
51-
# - `matplotlib.axes.Axes.set_facecolor`
52-
# - `matplotlib.figure.Figure.suptitle`
44+
# - `matplotlib.axes.Axes.axis`
45+
# - `matplotlib.axes.Axes.text`
46+
# - `matplotlib.colors.same_color`
47+
# - `cycler.Cycler`
5348
#
5449
# .. tags::
5550
#
5651
# styling: color
57-
# styling: colormap
52+
# purpose: reference
5853
# plot-type: line
5954
# level: beginner

0 commit comments

Comments
 (0)