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

Skip to content

Commit 149e7fb

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 5703ac4 commit 149e7fb

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
@@ -3329,13 +3329,10 @@ def errorbar(self, x, y, yerr=None, xerr=None,
33293329
# Remove alpha=0 color that _process_plot_format returns
33303330
fmt_style_kwargs.pop('color')
33313331

3332-
if ('color' in kwargs or 'color' in fmt_style_kwargs):
3333-
base_style = {}
3334-
if 'color' in kwargs:
3335-
base_style['color'] = kwargs.pop('color')
3336-
else:
3337-
base_style = next(self._get_lines.prop_cycler)
3338-
3332+
base_style = self._get_lines._getdefaults(
3333+
set(), {**fmt_style_kwargs, **kwargs})
3334+
if 'color' in kwargs:
3335+
base_style['color'] = kwargs.pop('color')
33393336
base_style['label'] = '_nolegend_'
33403337
base_style.update(fmt_style_kwargs)
33413338
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
@@ -3147,14 +3147,25 @@ def test_errobar_nonefmt():
31473147
assert np.all(errbar.get_color() == mcolors.to_rgba('C0'))
31483148

31493149

3150-
@image_comparison(['errorbar_with_prop_cycle.png'],
3151-
style='mpl20', remove_text=True)
3152-
def test_errorbar_with_prop_cycle():
3153-
_cycle = cycler(ls=['--', ':'], marker=['s', 's'], mfc=['k', 'w'])
3150+
@check_figures_equal(extensions=['png'])
3151+
def test_errorbar_with_prop_cycle(fig_test, fig_ref):
3152+
ax = fig_ref.subplots()
3153+
ax.errorbar(x=[2, 4, 10], y=[0, 1, 2], yerr=0.5,
3154+
ls='--', marker='s', mfc='k')
3155+
ax.errorbar(x=[2, 4, 10], y=[2, 3, 4], yerr=0.5, color='tab:green',
3156+
ls=':', marker='s', mfc='y')
3157+
ax.errorbar(x=[2, 4, 10], y=[4, 5, 6], yerr=0.5, fmt='tab:blue',
3158+
ls='-.', marker='o', mfc='c')
3159+
ax.set_xlim(1, 11)
3160+
3161+
_cycle = cycler(ls=['--', ':', '-.'], marker=['s', 's', 'o'],
3162+
mfc=['k', 'y', 'c'], color=['b', 'g', 'r'])
31543163
plt.rc("axes", prop_cycle=_cycle)
3155-
fig, ax = plt.subplots()
3156-
ax.errorbar(x=[2, 4, 10], y=[3, 2, 4], yerr=0.5)
3157-
ax.errorbar(x=[2, 4, 10], y=[6, 4, 2], yerr=0.5)
3164+
ax = fig_test.subplots()
3165+
ax.errorbar(x=[2, 4, 10], y=[0, 1, 2], yerr=0.5)
3166+
ax.errorbar(x=[2, 4, 10], y=[2, 3, 4], yerr=0.5, color='tab:green')
3167+
ax.errorbar(x=[2, 4, 10], y=[4, 5, 6], yerr=0.5, fmt='tab:blue')
3168+
ax.set_xlim(1, 11)
31583169

31593170

31603171
@check_figures_equal()

0 commit comments

Comments
 (0)