Skip to content

Commit cd755c4

Browse files
authored
Merge pull request #17236 from stefraynaud/default_contour_linewidth
Add the "contour.linewidths" configuration option
2 parents a57ea3d + f2f87e3 commit cd755c4

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Add :rc:`contour.linewidth` to rcParams
2+
---------------------------------------
3+
4+
The new config option :rc:`contour.linewidth` allows to control the default
5+
linewidth of contours as a float. When set to ``None``, the linewidths fall
6+
back to :rc:`lines.linewidth`. The config value is overidden as usual
7+
by the ``linewidths`` argument passed to `~.axes.Axes.contour` when
8+
it is not set to ``None``.
9+

lib/matplotlib/contour.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,10 @@ def _process_linewidths(self):
12571257
linewidths = self.linewidths
12581258
Nlev = len(self.levels)
12591259
if linewidths is None:
1260-
tlinewidths = [(mpl.rcParams['lines.linewidth'],)] * Nlev
1260+
default_linewidth = mpl.rcParams['contour.linewidth']
1261+
if default_linewidth is None:
1262+
default_linewidth = mpl.rcParams['lines.linewidth']
1263+
tlinewidths = [(default_linewidth,)] * Nlev
12611264
else:
12621265
if not np.iterable(linewidths):
12631266
linewidths = [linewidths] * Nlev
@@ -1749,7 +1752,7 @@ def _initialize_x_y(self, z):
17491752
however introduce rendering artifacts at chunk boundaries depending
17501753
on the backend, the *antialiased* flag and value of *alpha*.
17511754
1752-
linewidths : float or sequence of float, default: :rc:`lines.linewidth`
1755+
linewidths : float or array-like, default: :rc:`contour.linewidth`
17531756
*Only applies to* `.contour`.
17541757
17551758
The line width of the contour lines.
@@ -1759,6 +1762,8 @@ def _initialize_x_y(self, z):
17591762
If a sequence, the levels in ascending order will be plotted with
17601763
the linewidths in the order specified.
17611764
1765+
If None, this falls back to :rc:`lines.linewidth`.
1766+
17621767
linestyles : {*None*, 'solid', 'dashed', 'dashdot', 'dotted'}, optional
17631768
*Only applies to* `.contour`.
17641769

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,7 @@ def _convert_validator_spec(key, conv):
12031203
# contour props
12041204
'contour.negative_linestyle': ['dashed', _validate_linestyle],
12051205
'contour.corner_mask': [True, validate_bool],
1206+
'contour.linewidth': [None, validate_float_or_None],
12061207

12071208
# errorbar props
12081209
'errorbar.capsize': [0, validate_float],

lib/matplotlib/tests/test_contour.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from numpy.testing import assert_array_almost_equal
66
import matplotlib as mpl
77
from matplotlib.testing.decorators import image_comparison
8-
from matplotlib import pyplot as plt
8+
from matplotlib import pyplot as plt, rc_context
99
from matplotlib.colors import LogNorm
1010
import pytest
1111

@@ -375,3 +375,20 @@ def test_contour_uneven():
375375
ax = axs[1]
376376
cs = ax.contourf(z, levels=[2, 4, 6, 10, 20])
377377
fig.colorbar(cs, ax=ax, spacing='uniform')
378+
379+
380+
@pytest.mark.parametrize(
381+
"rc_lines_linewidth, rc_contour_linewidth, call_linewidths, expected", [
382+
(1.23, None, None, 1.23),
383+
(1.23, 4.24, None, 4.24),
384+
(1.23, 4.24, 5.02, 5.02)
385+
])
386+
def test_contour_linewidth(
387+
rc_lines_linewidth, rc_contour_linewidth, call_linewidths, expected):
388+
389+
with rc_context(rc={"lines.linewidth": rc_lines_linewidth,
390+
"contour.linewidth": rc_contour_linewidth}):
391+
fig, ax = plt.subplots()
392+
X = np.arange(4*3).reshape(4, 3)
393+
cs = ax.contour(X, linewidths=call_linewidths)
394+
assert cs.tlinewidths[0][0] == expected

matplotlibrc.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@
576576
## ***************************************************************************
577577
#contour.negative_linestyle: dashed # string or on-off ink sequence
578578
#contour.corner_mask: True # {True, False, legacy}
579+
#contour.linewidth: None # {float, None} Size of the contour
580+
# linewidths. If set to None,
581+
# it falls back to `line.linewidth`.
579582

580583

581584
## ***************************************************************************

0 commit comments

Comments
 (0)