Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c59fb0

Browse files
committed
Fix errorbar property cycling to match plot.
It would not cycle if a color were specified. However, this does not match `plot`, which does not advance the cycle only if _all_ properties in the cycle are specified. Notably, this means if your property cycle was for line style, specifying a color would ignore the cycle in `errorbar`, but not in `plot`. Fixes #7074.
1 parent 187e87b commit 1c59fb0

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
``Axes.errorbar`` cycles non-color properties correctly
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Formerly, `.Axes.errorbar` incorrectly skipped the Axes property cycle if a
5+
color was explicitly specified, even if the property cycler was for other
6+
properties (such as line style). Now, `.Axes.errorbar` will advance the Axes
7+
property cycle as done for `.Axes.plot`, i.e., as long as all properties in the
8+
cycler are not explicitly passed.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
``Axes.errorbar`` cycles non-color properties correctly
2+
-------------------------------------------------------
3+
4+
Formerly, `.Axes.errorbar` incorrectly skipped the Axes property cycle if a
5+
color was explicitly specified, even if the property cycler was for other
6+
properties (such as line style). Now, `.Axes.errorbar` will advance the Axes
7+
property cycle as done for `.Axes.plot`, i.e., as long as all properties in the
8+
cycler are not explicitly passed.
9+
10+
For example, the following will cycle through the line styles:
11+
12+
.. plot::
13+
:include-source: True
14+
15+
x = np.arange(0.1, 4, 0.5)
16+
y = np.exp(-x)
17+
offsets = [0, 1]
18+
19+
plt.rcParams['axes.prop_cycle'] = plt.cycler('linestyle', ['-', '--'])
20+
21+
fig, ax = plt.subplots()
22+
for offset in offsets:
23+
ax.errorbar(x, y + offset, xerr=0.1, yerr=0.3, fmt='tab:blue')

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,13 +3277,10 @@ def errorbar(self, x, y, yerr=None, xerr=None,
32773277
# Remove alpha=0 color that _process_plot_format returns
32783278
fmt_style_kwargs.pop('color')
32793279

3280-
if ('color' in kwargs or 'color' in fmt_style_kwargs):
3281-
base_style = {}
3282-
if 'color' in kwargs:
3283-
base_style['color'] = kwargs.pop('color')
3284-
else:
3285-
base_style = next(self._get_lines.prop_cycler)
3286-
3280+
base_style = self._get_lines._getdefaults(
3281+
set(), {**fmt_style_kwargs, **kwargs})
3282+
if 'color' in kwargs:
3283+
base_style['color'] = kwargs.pop('color')
32873284
base_style['label'] = '_nolegend_'
32883285
base_style.update(fmt_style_kwargs)
32893286
if 'color' not in base_style:
Binary file not shown.

lib/matplotlib/tests/test_axes.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,14 +3118,25 @@ def test_errobar_nonefmt():
31183118
assert np.all(errbar.get_color() == mcolors.to_rgba('C0'))
31193119

31203120

3121-
@image_comparison(['errorbar_with_prop_cycle.png'],
3122-
style='mpl20', remove_text=True)
3123-
def test_errorbar_with_prop_cycle():
3124-
_cycle = cycler(ls=['--', ':'], marker=['s', 's'], mfc=['k', 'w'])
3121+
@check_figures_equal(extensions=['png'])
3122+
def test_errorbar_with_prop_cycle(fig_test, fig_ref):
3123+
ax = fig_ref.subplots()
3124+
ax.errorbar(x=[2, 4, 10], y=[0, 1, 2], yerr=0.5,
3125+
ls='--', marker='s', mfc='k')
3126+
ax.errorbar(x=[2, 4, 10], y=[2, 3, 4], yerr=0.5, color='tab:green',
3127+
ls=':', marker='s', mfc='y')
3128+
ax.errorbar(x=[2, 4, 10], y=[4, 5, 6], yerr=0.5, fmt='tab:blue',
3129+
ls='-.', marker='o', mfc='c')
3130+
ax.set_xlim(1, 11)
3131+
3132+
_cycle = cycler(ls=['--', ':', '-.'], marker=['s', 's', 'o'],
3133+
mfc=['k', 'y', 'c'], color=['b', 'g', 'r'])
31253134
plt.rc("axes", prop_cycle=_cycle)
3126-
fig, ax = plt.subplots()
3127-
ax.errorbar(x=[2, 4, 10], y=[3, 2, 4], yerr=0.5)
3128-
ax.errorbar(x=[2, 4, 10], y=[6, 4, 2], yerr=0.5)
3135+
ax = fig_test.subplots()
3136+
ax.errorbar(x=[2, 4, 10], y=[0, 1, 2], yerr=0.5)
3137+
ax.errorbar(x=[2, 4, 10], y=[2, 3, 4], yerr=0.5, color='tab:green')
3138+
ax.errorbar(x=[2, 4, 10], y=[4, 5, 6], yerr=0.5, fmt='tab:blue')
3139+
ax.set_xlim(1, 11)
31293140

31303141

31313142
@check_figures_equal()

0 commit comments

Comments
 (0)