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

Skip to content

Commit d63c95d

Browse files
authored
Merge pull request #7976 from tacaswell/fix_hatch_color
FIX: Restore hatch color tracking edge color
2 parents af37a47 + acda4ac commit d63c95d

17 files changed

+699
-112
lines changed

doc/api/api_changes.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ out what caused the breakage and how to fix it by updating your code.
1010
For new features that were added to Matplotlib, please see
1111
:ref:`whats-new`.
1212

13+
API Changes in 2.0.1
14+
====================
15+
16+
Extensions to `matplotlib.backend_bases.GraphicsContextBase`
17+
------------------------------------------------------------
18+
19+
To better support controlling the color of hatches, the method
20+
`matplotlib.backend_bases.GraphicsContextBase.set_hatch_color` was
21+
added to the expected API of ``GraphicsContext`` classes. Calls to
22+
this method are currently wrapped with a ``try:...except Attribute:``
23+
block to preserve back-compatibility with any third-party backends
24+
which do not extend `~matplotlib.backend_bases.GraphicsContextBase`.
25+
26+
This value can be accessed in the backends via
27+
`matplotlib.backend_bases.GraphicsContextBase.get_hatch_color` (which
28+
was added in 2.0 see :ref:`gc_get_hatch_color_wn`) and should be used
29+
to color the hatches.
30+
31+
In the future there may also be ``hatch_linewidth`` and
32+
``hatch_density`` related methods added. It is encouraged, but not
33+
required that third-party backends extend
34+
`~matplotlib.backend_bases.GraphicsContextBase` to make adapting to
35+
these changes easier.
36+
1337

1438
API Changes in 2.0.0
1539
====================

doc/users/dflt_style_changes.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -623,20 +623,24 @@ To restore the previous behavior explicitly pass the keyword argument
623623
Hatching
624624
========
625625

626-
The color and width of the lines in a hatch pattern are now configurable by the
627-
rcParams `hatch.color` and `hatch.linewidth`, with defaults of black and 1
628-
point, respectively. The old behaviour for the color was to apply the edge
629-
color or use black, depending on the artist; the old behavior for the line
630-
width was different depending on backend:
626+
627+
The color of the lines in the hatch is now determined by
628+
629+
- If an edge color is explicitly set, use that for the hatch color
630+
- If the edge color is not explicitly set, use ``rcParam['hatch.color']`` which
631+
is looked up at artist creation time.
632+
633+
The width of the lines in a hatch pattern is now configurable by the
634+
rcParams `hatch.linewidth`, which defaults to 1 point. The old
635+
behavior for the line width was different depending on backend:
631636

632637
- PDF: 0.1 pt
633638
- SVG: 1.0 pt
634639
- PS: 1 px
635640
- Agg: 1 px
636641

637-
The old color behavior can not be restored. The old line width behavior can not
638-
be restored across all backends simultaneously, but can be restored for a
639-
single backend by setting::
642+
The old line width behavior can not be restored across all backends
643+
simultaneously, but can be restored for a single backend by setting::
640644

641645
mpl.rcParams['hatch.linewidth'] = 0.1 # previous pdf hatch linewidth
642646
mpl.rcParams['hatch.linewidth'] = 1.0 # previous svg hatch linewidth
@@ -649,7 +653,7 @@ The behavior of the PS and Agg backends was DPI dependent, thus::
649653
mpl.rcParams['hatch.linewidth'] = 1.0 / dpi # previous ps and Agg hatch linewidth
650654

651655

652-
There is no API level control of the hatch color or linewidth.
656+
There is no direct API level control of the hatch color or linewidth.
653657

654658
Hatching patterns are now rendered at a consistent density, regardless of DPI.
655659
Formerly, high DPI figures would be more dense than the default, and low DPI

doc/users/whats_new.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,20 @@ value of ``None`` instead of ``2``. If ``None`` is given as ``zorder``,
313313
:func:`streamplot` has a default ``zorder`` of
314314
``matplotlib.lines.Line2D.zorder``.
315315

316+
.. _gc_get_hatch_color_wn:
317+
318+
Extension to `matplotlib.backend_bases.GraphicsContextBase`
319+
-----------------------------------------------------------
320+
321+
To support standardizing hatch behavior across the backends we ship
322+
the `matplotlib.backend_bases.GraphicsContextBase.get_hatch_color`
323+
method as added to `matplotlib.backend_bases.GraphicsContextBase`.
324+
This is only used during the render process in the backends we ship so
325+
will not break any third-party backends.
326+
327+
If you maintain a third-party backend which extends
328+
`~matplotlib.backend_bases.GraphicsContextBase` this method is now
329+
available to you and should be used to color hatch patterns.
316330

317331
Previous Whats New
318332
==================

lib/matplotlib/backend_bases.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ def copy_properties(self, gc):
817817
self._linewidth = gc._linewidth
818818
self._rgb = gc._rgb
819819
self._hatch = gc._hatch
820+
self._hatch_color = gc._hatch_color
821+
self._hatch_linewidth = gc._hatch_linewidth
820822
self._url = gc._url
821823
self._gid = gc._gid
822824
self._snap = gc._snap
@@ -1111,6 +1113,12 @@ def get_hatch_color(self):
11111113
"""
11121114
return self._hatch_color
11131115

1116+
def set_hatch_color(self, hatch_color):
1117+
"""
1118+
sets the color to use for hatching.
1119+
"""
1120+
self._hatch_color = hatch_color
1121+
11141122
def get_hatch_linewidth(self):
11151123
"""
11161124
Gets the linewidth to use for hatching.

lib/matplotlib/backends/backend_pdf.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,14 +2211,14 @@ def alpha_cmd(self, alpha, forced, effective_alphas):
22112211
name = self.file.alphaState(effective_alphas)
22122212
return [name, Op.setgstate]
22132213

2214-
def hatch_cmd(self, hatch):
2214+
def hatch_cmd(self, hatch, hatch_color):
22152215
if not hatch:
22162216
if self._fillcolor is not None:
22172217
return self.fillcolor_cmd(self._fillcolor)
22182218
else:
22192219
return [Name('DeviceRGB'), Op.setcolorspace_nonstroke]
22202220
else:
2221-
hatch_style = (self._hatch_color, self._fillcolor, hatch)
2221+
hatch_style = (hatch_color, self._fillcolor, hatch)
22222222
name = self.file.hatchPattern(hatch_style)
22232223
return [Name('Pattern'), Op.setcolorspace_nonstroke,
22242224
name, Op.setcolor_nonstroke]
@@ -2282,7 +2282,8 @@ def clip_cmd(self, cliprect, clippath):
22822282
(('_linewidth',), linewidth_cmd),
22832283
(('_dashes',), dash_cmd),
22842284
(('_rgb',), rgb_cmd),
2285-
(('_hatch',), hatch_cmd), # must come after fillcolor and rgb
2285+
# must come after fillcolor and rgb
2286+
(('_hatch', '_hatch_color'), hatch_cmd),
22862287
)
22872288

22882289
# TODO: _linestyle
@@ -2313,7 +2314,7 @@ def delta(self, other):
23132314
break
23142315

23152316
# Need to update hatching if we also updated fillcolor
2316-
if params == ('_hatch',) and fill_performed:
2317+
if params == ('_hatch', '_hatch_color') and fill_performed:
23172318
different = True
23182319

23192320
if different:

lib/matplotlib/collections.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def __init__(self,
136136
self._linewidths = [0]
137137
self._is_filled = True # May be modified by set_facecolor().
138138

139+
self._hatch_color = mcolors.to_rgba(mpl.rcParams['hatch.color'])
139140
self.set_facecolor(facecolors)
140141
self.set_edgecolor(edgecolors)
141142
self.set_linewidth(linewidths)
@@ -293,6 +294,12 @@ def draw(self, renderer):
293294

294295
if self._hatch:
295296
gc.set_hatch(self._hatch)
297+
try:
298+
gc.set_hatch_color(self._hatch_color)
299+
except AttributeError:
300+
# if we end up with a GC that does not have this method
301+
warnings.warn("Your backend does not support setting the "
302+
"hatch color.")
296303

297304
if self.get_sketch_params() is not None:
298305
gc.set_sketch_params(*self.get_sketch_params())
@@ -690,12 +697,15 @@ def get_edgecolor(self):
690697
get_edgecolors = get_edgecolor
691698

692699
def _set_edgecolor(self, c):
700+
set_hatch_color = True
693701
if c is None:
694702
if (mpl.rcParams['patch.force_edgecolor'] or
695703
not self._is_filled or self._edge_default):
696704
c = mpl.rcParams['patch.edgecolor']
697705
else:
698706
c = 'none'
707+
set_hatch_color = False
708+
699709
self._is_stroked = True
700710
try:
701711
if c.lower() == 'none':
@@ -710,6 +720,8 @@ def _set_edgecolor(self, c):
710720
except AttributeError:
711721
pass
712722
self._edgecolors = mcolors.to_rgba_array(c, self._alpha)
723+
if set_hatch_color and len(self._edgecolors):
724+
self._hatch_color = tuple(self._edgecolors[0])
713725
self.stale = True
714726

715727
def set_edgecolor(self, c):

lib/matplotlib/patches.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import six
77
from six.moves import map, zip
8+
import warnings
89

910
import math
1011

@@ -113,10 +114,10 @@ def __init__(self,
113114
if antialiased is None:
114115
antialiased = mpl.rcParams['patch.antialiased']
115116

117+
self._hatch_color = colors.to_rgba(mpl.rcParams['hatch.color'])
116118
self._fill = True # needed for set_facecolor call
117119
if color is not None:
118120
if (edgecolor is not None or facecolor is not None):
119-
import warnings
120121
warnings.warn("Setting the 'color' property will override"
121122
"the edgecolor or facecolor properties. ")
122123
self.set_color(color)
@@ -288,13 +289,18 @@ def set_aa(self, aa):
288289
return self.set_antialiased(aa)
289290

290291
def _set_edgecolor(self, color):
292+
set_hatch_color = True
291293
if color is None:
292294
if (mpl.rcParams['patch.force_edgecolor'] or
293295
not self._fill or self._edge_default):
294296
color = mpl.rcParams['patch.edgecolor']
295297
else:
296298
color = 'none'
299+
set_hatch_color = False
300+
297301
self._edgecolor = colors.to_rgba(color, self._alpha)
302+
if set_hatch_color:
303+
self._hatch_color = self._edgecolor
298304
self.stale = True
299305

300306
def set_edgecolor(self, color):
@@ -545,6 +551,12 @@ def draw(self, renderer):
545551

546552
if self._hatch:
547553
gc.set_hatch(self._hatch)
554+
try:
555+
gc.set_hatch_color(self._hatch_color)
556+
except AttributeError:
557+
# if we end up with a GC that does not have this method
558+
warnings.warn("Your backend does not have support for "
559+
"setting the hatch color.")
548560

549561
if self.get_sketch_params() is not None:
550562
gc.set_sketch_params(*self.get_sketch_params())
@@ -4286,6 +4298,13 @@ def draw(self, renderer):
42864298

42874299
if self._hatch:
42884300
gc.set_hatch(self._hatch)
4301+
if self._hatch_color is not None:
4302+
try:
4303+
gc.set_hatch_color(self._hatch_color)
4304+
except AttributeError:
4305+
# if we end up with a GC that does not have this method
4306+
warnings.warn("Your backend does not support setting the "
4307+
"hatch color.")
42894308

42904309
if self.get_sketch_params() is not None:
42914310
gc.set_sketch_params(*self.get_sketch_params())
Binary file not shown.

0 commit comments

Comments
 (0)