Skip to content

Commit 45d4056

Browse files
committed
Added hatch handling in get_draw_options
1 parent d445bf8 commit 45d4056

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

tikzplotlib/_path.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ._axes import _mpl_cmap2pgf_cmap
77
from ._markers import _mpl_marker2pgfp_marker
88
from ._util import get_legend_text, has_legend
9+
from ._hatches import _mpl_hatch2pgfp_pattern
910

1011

1112
def draw_path(data, path, draw_options=None, simplify=None):
@@ -233,7 +234,7 @@ def draw_pathcollection(data, obj):
233234
return data, content
234235

235236

236-
def get_draw_options(data, obj, ec, fc, ls, lw):
237+
def get_draw_options(data, obj, ec, fc, ls, lw, hatch=None):
237238
"""Get the draw options for a given (patch) object.
238239
Get the draw options for a given (patch) object.
239240
Input:
@@ -243,6 +244,7 @@ def get_draw_options(data, obj, ec, fc, ls, lw):
243244
fc - face color
244245
ls - linestyle
245246
lw - linewidth
247+
hatch=None - hatch, i.e., pattern within closed path
246248
Output:
247249
draw_options - list, to be ",".join(draw_options) to produce the
248250
draw options passed to PGF
@@ -284,6 +286,35 @@ def get_draw_options(data, obj, ec, fc, ls, lw):
284286
if ls_ is not None and ls_ != "solid":
285287
draw_options.append(ls_)
286288

289+
if hatch is not None:
290+
# In matplotlib hatches are rendered with edge color and linewidth
291+
# In PGFPlots patterns are rendered in 'pattern color' which defaults to
292+
# black and according to opacity fill.
293+
# No 'pattern line width' option exist.
294+
# This can be achieved with custom patterns, see _hatches.py
295+
296+
# There exist an obj.get_hatch_color() method in the mpl API,
297+
# but it seems to be unused
298+
try:
299+
hc = obj._hatch_color
300+
except AttributeError: # Fallback to edge color
301+
if ec is None or ec_rgba[3] == 0.0:
302+
# Assuming that a hatch marker indicates that hatches are wanted, also
303+
# when the edge color is (0, 0, 0, 0), i.e., the edge is invisible
304+
h_col, h_rgba = "black", numpy.array([0, 0, 0, 1])
305+
else:
306+
h_col, h_rgba = ec_col, ec_rgba
307+
else:
308+
data, h_col, h_rgba = _color.mpl_color2xcolor(data, hc)
309+
finally:
310+
if h_rgba[3] > 0:
311+
data, pattern = _mpl_hatch2pgfp_pattern(data, hatch, h_col, h_rgba)
312+
if ec_rgba[3] == 0.0:
313+
# 'draw=none' must be specified for the border to be omitted
314+
# when a pattern is drawn using \draw.
315+
draw_options.append("draw={}".format(ec_col))
316+
draw_options += pattern
317+
287318
return data, draw_options
288319

289320

0 commit comments

Comments
 (0)