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

Skip to content

Commit 5868627

Browse files
committed
API: fill_between and fill_betweenx color cycle
They follow the patch property cycle (but only use the color). Closes #7157
1 parent 0370181 commit 5868627

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

doc/users/dflt_style_changes.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,37 @@ or by setting::
473473
in your :file:`matplotlibrc` file.
474474

475475

476+
``fill_between`` and ``fill_betweenx``
477+
--------------------------------------
478+
479+
`~matplotlib.axes.Axes.fill_between` and
480+
`~matplotlib.axes.Axes.fill_betweenx` both follow the patch color
481+
cycle.
482+
483+
.. plot::
484+
485+
import matplotlib.pyplot as plt
486+
import numpy as np
487+
488+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))
489+
th = np.linspace(0, 2*np.pi, 128)
490+
N = 5
491+
492+
def demo(ax, extra_kwargs, title):
493+
ax.set_title(title)
494+
return [ax.fill_between(th, np.sin((j / N) * np.pi + th), alpha=.5, **extra_kwargs)
495+
for j in range(N)]
496+
497+
demo(ax1, {}, '2.x')
498+
demo(ax2, {'facecolor': 'C0'}, 'non-cycled')
499+
500+
If the facecolor is set via the ``facecolors`` or ``color`` keyword argument,
501+
then the color is not cycled.
502+
503+
To restore the previous behavior, explicitly pass the keyword argument
504+
``facecolors='C0'`` to the method call.
505+
506+
476507
Patch edges and color
477508
---------------------
478509

lib/matplotlib/axes/_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,6 +4764,14 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
47644764
for filling between two sets of x-values
47654765
47664766
"""
4767+
if not rcParams['_internal.classic_mode']:
4768+
color_aliases = mcoll._color_aliases
4769+
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
4770+
4771+
if not any(c in kwargs for c in ('color', 'facecolors')):
4772+
fc = self._get_patches_for_fill.get_next_color()
4773+
kwargs['facecolors'] = fc
4774+
47674775
# Handle united data, such as dates
47684776
self._process_unit_info(xdata=x, ydata=y1, kwargs=kwargs)
47694777
self._process_unit_info(ydata=y2)
@@ -4914,6 +4922,13 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49144922
for filling between two sets of y-values
49154923
49164924
"""
4925+
if not rcParams['_internal.classic_mode']:
4926+
color_aliases = mcoll._color_aliases
4927+
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
4928+
4929+
if not any(c in kwargs for c in ('color', 'facecolors')):
4930+
fc = self._get_patches_for_fill.get_next_color()
4931+
kwargs['facecolors'] = fc
49174932
# Handle united data, such as dates
49184933
self._process_unit_info(ydata=y, xdata=x1, kwargs=kwargs)
49194934
self._process_unit_info(xdata=x2)

lib/matplotlib/collections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
CIRCLE_AREA_FACTOR = 1.0 / np.sqrt(np.pi)
4040

4141

42+
_color_aliases = {'facecolors': ['facecolor'],
43+
'edgecolors': ['edgecolor']}
44+
45+
4246
class Collection(artist.Artist, cm.ScalarMappable):
4347
"""
4448
Base class for Collections. Must be subclassed to be usable.

lib/matplotlib/tests/test_axes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import matplotlib.pyplot as plt
2828
import matplotlib.markers as mmarkers
2929
import matplotlib.patches as mpatches
30+
import matplotlib.colors as mcolors
3031
from numpy.testing import assert_allclose, assert_array_equal
3132
from matplotlib.cbook import IgnoredKeywordWarning
3233
import matplotlib.colors as mcolors
@@ -4613,6 +4614,34 @@ def test_bar_color_cycle():
46134614
assert ccov(ln.get_color()) == ccov(br.get_facecolor())
46144615

46154616

4617+
@cleanup(style='default')
4618+
def test_fillbetween_cycle():
4619+
fig, ax = plt.subplots()
4620+
4621+
for j in range(3):
4622+
cc = ax.fill_between(range(3), range(3))
4623+
target = mcolors.to_rgba('C{}'.format(j))
4624+
assert tuple(cc.get_facecolors().squeeze()) == tuple(target)
4625+
4626+
for j in range(3, 6):
4627+
cc = ax.fill_betweenx(range(3), range(3))
4628+
target = mcolors.to_rgba('C{}'.format(j))
4629+
assert tuple(cc.get_facecolors().squeeze()) == tuple(target)
4630+
4631+
target = mcolors.to_rgba('k')
4632+
4633+
for al in ['facecolor', 'facecolors', 'color']:
4634+
cc = ax.fill_between(range(3), range(3), **{al: 'k'})
4635+
assert tuple(cc.get_facecolors().squeeze()) == tuple(target)
4636+
4637+
edge_target = mcolors.to_rgba('k')
4638+
for j, el in enumerate(['edgecolor', 'edgecolors'], start=6):
4639+
cc = ax.fill_between(range(3), range(3), **{el: 'k'})
4640+
face_target = mcolors.to_rgba('C{}'.format(j))
4641+
assert tuple(cc.get_facecolors().squeeze()) == tuple(face_target)
4642+
assert tuple(cc.get_edgecolors().squeeze()) == tuple(edge_target)
4643+
4644+
46164645
if __name__ == '__main__':
46174646
import nose
46184647
import sys

0 commit comments

Comments
 (0)