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

Skip to content

Commit dff87ce

Browse files
authored
Merge pull request #23266 from andrew-fennell/negative_linestyles
negative_linestyles kwarg in contour.py
2 parents 75afc13 + 1b3b390 commit dff87ce

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

lib/matplotlib/contour.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ def __init__(self, ax, *args,
701701
hatches=(None,), alpha=None, origin=None, extent=None,
702702
cmap=None, colors=None, norm=None, vmin=None, vmax=None,
703703
extend='neither', antialiased=None, nchunk=0, locator=None,
704-
transform=None,
704+
transform=None, negative_linestyles=None,
705705
**kwargs):
706706
"""
707707
Draw contour lines or filled regions, depending on
@@ -786,6 +786,13 @@ def __init__(self, ax, *args,
786786

787787
self._transform = transform
788788

789+
self.negative_linestyles = negative_linestyles
790+
# If negative_linestyles was not defined as a kwarg,
791+
# define negative_linestyles with rcParams
792+
if self.negative_linestyles is None:
793+
self.negative_linestyles = \
794+
mpl.rcParams['contour.negative_linestyle']
795+
789796
kwargs = self._process_args(*args, **kwargs)
790797
self._process_levels()
791798

@@ -1276,11 +1283,10 @@ def _process_linestyles(self):
12761283
if linestyles is None:
12771284
tlinestyles = ['solid'] * Nlev
12781285
if self.monochrome:
1279-
neg_ls = mpl.rcParams['contour.negative_linestyle']
12801286
eps = - (self.zmax - self.zmin) * 1e-15
12811287
for i, lev in enumerate(self.levels):
12821288
if lev < eps:
1283-
tlinestyles[i] = neg_ls
1289+
tlinestyles[i] = self.negative_linestyles
12841290
else:
12851291
if isinstance(linestyles, str):
12861292
tlinestyles = [linestyles] * Nlev
@@ -1751,6 +1757,18 @@ def _initialize_x_y(self, z):
17511757
iterable is shorter than the number of contour levels
17521758
it will be repeated as necessary.
17531759
1760+
negative_linestyles : {*None*, 'solid', 'dashed', 'dashdot', 'dotted'}, \
1761+
optional
1762+
*Only applies to* `.contour`.
1763+
1764+
If *negative_linestyles* is None, the default is 'dashed' for
1765+
negative contours.
1766+
1767+
*negative_linestyles* can also be an iterable of the above
1768+
strings specifying a set of linestyles to be used. If this
1769+
iterable is shorter than the number of contour levels
1770+
it will be repeated as necessary.
1771+
17541772
hatches : list[str], optional
17551773
*Only applies to* `.contourf`.
17561774

lib/matplotlib/tests/test_contour.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,80 @@ def test_subfigure_clabel():
605605
CS = ax.contour(X, Y, Z)
606606
ax.clabel(CS, inline=True, fontsize=10)
607607
ax.set_title("Simplest default with labels")
608+
609+
610+
@pytest.mark.parametrize(
611+
"style", ['solid', 'dashed', 'dashdot', 'dotted'])
612+
def test_linestyles(style):
613+
delta = 0.025
614+
x = np.arange(-3.0, 3.0, delta)
615+
y = np.arange(-2.0, 2.0, delta)
616+
X, Y = np.meshgrid(x, y)
617+
Z1 = np.exp(-X**2 - Y**2)
618+
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
619+
Z = (Z1 - Z2) * 2
620+
621+
# Positive contour defaults to solid
622+
fig1, ax1 = plt.subplots()
623+
CS1 = ax1.contour(X, Y, Z, 6, colors='k')
624+
ax1.clabel(CS1, fontsize=9, inline=True)
625+
ax1.set_title('Single color - positive contours solid (default)')
626+
assert CS1.linestyles is None # default
627+
628+
# Change linestyles using linestyles kwarg
629+
fig2, ax2 = plt.subplots()
630+
CS2 = ax2.contour(X, Y, Z, 6, colors='k', linestyles=style)
631+
ax2.clabel(CS2, fontsize=9, inline=True)
632+
ax2.set_title(f'Single color - positive contours {style}')
633+
assert CS2.linestyles == style
634+
635+
# Ensure linestyles do not change when negative_linestyles is defined
636+
fig3, ax3 = plt.subplots()
637+
CS3 = ax3.contour(X, Y, Z, 6, colors='k', linestyles=style,
638+
negative_linestyles='dashdot')
639+
ax3.clabel(CS3, fontsize=9, inline=True)
640+
ax3.set_title(f'Single color - positive contours {style}')
641+
assert CS3.linestyles == style
642+
643+
644+
@pytest.mark.parametrize(
645+
"style", ['solid', 'dashed', 'dashdot', 'dotted'])
646+
def test_negative_linestyles(style):
647+
delta = 0.025
648+
x = np.arange(-3.0, 3.0, delta)
649+
y = np.arange(-2.0, 2.0, delta)
650+
X, Y = np.meshgrid(x, y)
651+
Z1 = np.exp(-X**2 - Y**2)
652+
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
653+
Z = (Z1 - Z2) * 2
654+
655+
# Negative contour defaults to dashed
656+
fig1, ax1 = plt.subplots()
657+
CS1 = ax1.contour(X, Y, Z, 6, colors='k')
658+
ax1.clabel(CS1, fontsize=9, inline=True)
659+
ax1.set_title('Single color - negative contours dashed (default)')
660+
assert CS1.negative_linestyles == 'dashed' # default
661+
662+
# Change negative_linestyles using rcParams
663+
plt.rcParams['contour.negative_linestyle'] = style
664+
fig2, ax2 = plt.subplots()
665+
CS2 = ax2.contour(X, Y, Z, 6, colors='k')
666+
ax2.clabel(CS2, fontsize=9, inline=True)
667+
ax2.set_title(f'Single color - negative contours {style}'
668+
'(using rcParams)')
669+
assert CS2.negative_linestyles == style
670+
671+
# Change negative_linestyles using negative_linestyles kwarg
672+
fig3, ax3 = plt.subplots()
673+
CS3 = ax3.contour(X, Y, Z, 6, colors='k', negative_linestyles=style)
674+
ax3.clabel(CS3, fontsize=9, inline=True)
675+
ax3.set_title(f'Single color - negative contours {style}')
676+
assert CS3.negative_linestyles == style
677+
678+
# Ensure negative_linestyles do not change when linestyles is defined
679+
fig4, ax4 = plt.subplots()
680+
CS4 = ax4.contour(X, Y, Z, 6, colors='k', linestyles='dashdot',
681+
negative_linestyles=style)
682+
ax4.clabel(CS4, fontsize=9, inline=True)
683+
ax4.set_title(f'Single color - negative contours {style}')
684+
assert CS4.negative_linestyles == style

0 commit comments

Comments
 (0)