10000 Merge pull request #28786 from rcomer/contour-extend · timhoffm/matplotlib@f8592cb · GitHub
[go: up one dir, main page]

Skip to content

Commit f8592cb

Browse files
authored
Merge pull request matplotlib#28786 from rcomer/contour-extend
Handle single color in ContourSet
2 parents 42b88d0 + 5db8071 commit f8592cb

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Specifying a single color in ``contour`` and ``contourf``
2+
---------------------------------------------------------
3+
4+
`~.Axes.contour` and `~.Axes.contourf` previously accepted a single color
5+
provided it was expressed as a string. This restriction has now been removed
6+
and a single color in any format described in the :ref:`colors_def` tutorial
7+
may be passed.
8+
9+
.. plot::
10+
:include-source: true
11+
:alt: Two-panel example contour plots. The left panel has all transparent red contours. The right panel has all dark blue contours.
12+
13+
import matplotlib.pyplot as plt
14+
15+
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(6, 3))
16+
z = [[0, 1], [1, 2]]
17+
18+
ax1.contour(z, colors=('r', 0.4))
19+
ax2.contour(z, colors=(0.1, 0.2, 0.5))
20+
21+
plt.show()

lib/matplotlib/contour.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,11 @@ def __init__(self, ax, *args,
703703
self._extend_min = self.extend in ['min', 'both']
704704
self._extend_max = self.extend in ['max', 'both']
705705
if self.colors is not None:
706+
if mcolors.is_color_like(self.colors):
707+
color_sequence = [self.colors]
708+
else:
709+
color_sequence = self.colors
710+
706711
ncolors = len(self.levels)
707712
if self.filled:
708713
ncolors -= 1
@@ -719,19 +724,19 @@ def __init__(self, ax, *args,
719724
total_levels = (ncolors +
720725
int(self._extend_min) +
721726
int(self._extend_max))
722-
if (len(self.colors) == total_levels and
727+
if (len(color_sequence) == total_levels and
723728
(self._extend_min or self._extend_max)):
724729
use_set_under_over = True
725730
if self._extend_min:
726731
i0 = 1
727732

728-
cmap = mcolors.ListedColormap(self.colors[i0:None], N=ncolors)
733+
cmap = mcolors.ListedColormap(color_sequence[i0:None], N=ncolors)
729734

730735
if use_set_under_over:
731736
if self._extend_min:
732-
cmap.set_under(self.colors[0])
737+
cmap.set_under(color_sequence[0])
733738
if self._extend_max:
734-
cmap.set_over(self.colors[-1])
739+
cmap.set_over(color_sequence[-1])
735740

736741
# label lists must be initialized here
737742
self.labelTexts = []
@@ -1499,10 +1504,12 @@ def _initialize_x_y(self, z):
14991504
The sequence is cycled for the levels in ascending order. If the
15001505
sequence is shorter than the number of levels, it's repeated.
15011506
1502-
As a shortcut, single color strings may be used in place of
1503-
one-element lists, i.e. ``'red'`` instead of ``['red']`` to color
1504-
all levels with the same color. This shortcut does only work for
1505-
color strings, not for other ways of specifying colors.
1507+
As a shortcut, a single color may be used in place of one-element lists, i.e.
1508+
``'red'`` instead of ``['red']`` to color all levels with the same color.
1509+
1510+
.. versionchanged:: 3.10
1511+
Previously a single color had to be expressed as a string, but now any
1512+
valid color format may be passed.
15061513
15071514
By default (value *None*), the colormap specified by *cmap*
15081515
will be used.
@@ -1570,10 +1577,10 @@ def _initialize_x_y(self, z):
15701577
15711578
An existing `.QuadContourSet` does not get notified if
15721579
properties of its colormap are changed. Therefore, an explicit
1573-
call `.QuadContourSet.changed()` is needed after modifying the
1580+
call ``QuadContourSet.changed()`` is needed after modifying the
15741581
colormap. The explicit call can be left out, if a colorbar is
15751582
assigned to the `.QuadContourSet` because it internally calls
1576-
`.QuadContourSet.changed()`.
1583+
``QuadContourSet.changed()``.
15771584
15781585
Example::
15791586

lib/matplotlib/tests/test_contour.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ def test_given_colors_levels_and_extends():
171171
plt.colorbar(c, ax=ax)
172172

173173

174+
@pytest.mark.parametrize('color, extend', [('darkred', 'neither'),
175+
('darkred', 'both'),
176+
(('r', 0.5), 'neither'),
177+
((0.1, 0.2, 0.5, 0.3), 'neither')])
178+
def test_single_color_and_extend(color, extend):
179+
z = [[0, 1], [1, 2]]
180+
181+
_, ax = plt.subplots()
182+
levels = [0.5, 0.75, 1, 1.25, 1.5]
183+
cs = ax.contour(z, levels=levels, colors=color, extend=extend)
184+
for c in cs.get_edgecolors():
185+
assert same_color(c, color)
186+
187+
174188
@image_comparison(['contour_log_locator.svg'], style='mpl20', remove_text=False)
175189
def test_log_locator_levels():
176190

0 commit comments

Comments
 (0)
0