Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3f2f117

Browse filesBrowse files
authored
Merge pull request #7173 from tacaswell/api_fill_between_cycle
[MRG+1] API: fill_between and fill_betweenx color cycle
2 parents 422e78a + 5868627 commit 3f2f117
Copy full SHA for 3f2f117

File tree

Expand file treeCollapse file tree

4 files changed

+79
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+79
-0
lines changed

‎doc/users/dflt_style_changes.rst

Copy file name to clipboardExpand all lines: doc/users/dflt_style_changes.rst
+31Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4768,6 +4768,14 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
47684768
for filling between two sets of x-values
47694769
47704770
"""
4771+
if not rcParams['_internal.classic_mode']:
4772+
color_aliases = mcoll._color_aliases
4773+
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
4774+
4775+
if not any(c in kwargs for c in ('color', 'facecolors')):
4776+
fc = self._get_patches_for_fill.get_next_color()
4777+
kwargs['facecolors'] = fc
4778+
47714779
# Handle united data, such as dates
47724780
self._process_unit_info(xdata=x, ydata=y1, kwargs=kwargs)
47734781
self._process_unit_info(ydata=y2)
@@ -4918,6 +4926,13 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49184926
for filling between two sets of y-values
49194927
49204928
"""
4929+
if not rcParams['_internal.classic_mode']:
4930+
color_aliases = mcoll._color_aliases
4931+
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
4932+
4933+
if not any(c in kwargs for c in ('color', 'facecolors')):
4934+
fc = self._get_patches_for_fill.get_next_color()
4935+
kwargs['facecolors'] = fc
49214936
# Handle united data, such as dates
49224937
self._process_unit_info(ydata=y, xdata=x1, kwargs=kwargs)
49234938
self._process_unit_info(xdata=x2)

‎lib/matplotlib/collections.py

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.py
+4Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+29Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.