8000 added `hatches` param to other backends · matplotlib/matplotlib@e1fe14d · GitHub
[go: up one dir, main page]

Skip to content

Commit e1fe14d

Browse files
committed
added hatches param to other backends
1 parent d8cf3a0 commit e1fe14d

File tree

6 files changed

+55
-29
lines changed

6 files changed

+55
-29
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
250250

251251
def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
252252
coordinates, offsets, offsetTrans, facecolors,
253-
antialiased, edgecolors 8000 ):
253+
antialiased, edgecolors, hatches):
254254
"""
255255
Draw a quadmesh.
256256
@@ -267,7 +267,7 @@ def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
267267

268268
return self.draw_path_collection(
269269
gc, master_transform, paths, [], offsets, offsetTrans, facecolors,
270-
edgecolors, linewidths, [], [antialiased], [None], 'screen')
270+
edgecolors, linewidths, [], [antialiased], hatches, [None], 'screen')
271271

272272
def draw_gouraud_triangles(self, gc, triangles_array, colors_array,
273273
transform):
@@ -365,8 +365,8 @@ def _iter_collection(self, gc, path_ids, offsets, offset_trans, facecolors,
365365
Nedgecolors = len(edgecolors)
366366
Nlinewidths = len(linewidths)
367367
Nlinestyles = len(linestyles)
368-
Nurls = len(urls)
369368
Nhatches = len(hatches)
369+
Nurls = len(urls)
370370

371371
if (Nfacecolors == 0 and Nedgecolors == 0) or Npaths == 0:
372372
return
@@ -386,8 +386,8 @@ def cycle_or_default(seq, default=None):
386386
lws = cycle_or_default(linewidths)
387387
lss = cycle_or_default(linestyles)
388388
aas = cycle_or_default(antialiaseds)
389-
urls = cycle_or_default(urls)
390389
hchs = cycle_or_default(hatches)
390+
urls = cycle_or_default(urls)
391391

392392
if Nedgecolors == 0:
393393
gc0.set_linewidth(0.0)
@@ -408,10 +408,10 @@ def cycle_or_default(seq, default=None):
408408
if fc is not None and len(fc) == 4 and fc[3] == 0:
409409
fc = None
410410
gc0.set_antialiased(aa)
411-
if Nurls:
412-
gc0.set_url(url)
413411
if Nhatches:
414412
gc0.set_hatch(hch)
413+
if Nurls:
414+
gc0.set_url(url)
415415
yield xo, yo, pathid, gc0, fc
416416
gc0.restore()
417417

lib/matplotlib/backends/backend_ps.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def draw_markers(
514514
@_log_if_debug_on
515515
def draw_path_collection(self, gc, master_transform, paths, all_transforms,
516516
offsets, offset_trans, facecolors, edgecolors,
517-
linewidths, linestyles, antialiaseds, urls,
517+
linewidths, linestyles, antialiaseds, hatches, urls,
518518
offset_position):
519519
# Is the optimization worth it? Rough calculation:
520520
# cost of emitting a path in-line is
@@ -530,7 +530,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
530530
return RendererBase.draw_path_collection(
531531
self, gc, master_transform, paths, all_transforms,
532532
offsets, offset_trans, facecolors, edgecolors,
533-
linewidths, linestyles, antialiaseds, urls,
533+
linewidths, linestyles, antialiaseds, hatches, urls,
534534
offset_position)
535535

536536
path_codes = []
@@ -550,7 +550,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
550550
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
551551
gc, path_codes, offsets, offset_trans,
552552
facecolors, edgecolors, linewidths, linestyles,
553-
antialiaseds, urls, offset_position):
553+
antialiaseds, hatches, urls, offset_position):
554554
ps = f"{xo:g} {yo:g} {path_id}"
555555
self._draw_ps(ps, gc0, rgbFace)
556556

lib/matplotlib/backends/backend_svg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def draw_markers(
714714

715715
def draw_path_collection(self, gc, master_transform, paths, all_transforms,
716716
offsets, offset_trans, facecolors, edgecolors,
717-
linewidths, linestyles, antialiaseds, urls,
717+
linewidths, linestyles, antialiaseds, hatches, urls,
718718
offset_position):
719719
# Is the optimization worth it? Rough calculation:
720720
# cost of emitting a path in-line is
@@ -730,7 +730,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
730730
return super().draw_path_collection(
731731
gc, master_transform, paths, all_transforms,
732732
offsets, offset_trans, facecolors, edgecolors,
733-
linewidths, linestyles, antialiaseds, urls,
733+
linewidths, linestyles, antialiaseds, hatches, urls,
734734
offset_position)
735735

736736
writer = self.writer
@@ -749,7 +749,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
749749
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
750750
gc, path_codes, offsets, offset_trans,
751751
facecolors, edgecolors, linewidths, linestyles,
752-
antialiaseds, urls, offset_position):
752+
antialiaseds, hatches, urls, offset_position):
753753
url = gc0.get_url()
754754
if url is not None:
755755
writer.start('a', attrib={'xlink:href': url})

lib/matplotlib/collections.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ def draw(self, renderer):
360360
self._set_gc_clip(gc)
361361
gc.set_snap(self.get_snap())
362362

363-
if self._hatch:
364-
gc.set_hatch_color(self._hatch_color)
363+
gc.set_hatch_color(self._hatch_color)
365364

366365
if self.get_sketch_params() is not None:
367366
gc.set_sketch_params(*self.get_sketch_params())
@@ -411,6 +410,10 @@ def draw(self, renderer):
411410
gc, paths[0], combined_transform.frozen(),
412411
mpath.Path(offsets), offset_trf, tuple(facecolors[0]))
413412
else:
413+
hatches = self.get_hatch()
414+
if isinstance(renderer, mpl.backends.backend_agg.RendererAgg):
415+
hatches = [mpath.Path.hatch(h) for h in hatches]
416+
414417
if self._gapcolor is not None:
415418
# First draw paths within the gaps.
416419
ipaths, ilinestyles = self._get_inverse_paths_linestyles()
@@ -419,15 +422,15 @@ def draw(self, renderer):
419422
self.get_transforms(), offsets, offset_trf,
420423
[mcolors.to_rgba("none")], self._gapcolor,
421424
self._linewidths, ilinestyles,
422-
self._antialiaseds, self.get_hatch(), self._urls,
425+
self._antialiaseds, hatches, self._urls,
423426
"screen")
424427

425428
renderer.draw_path_collection(
426429
gc, transform.frozen(), paths,
427430
self.get_transforms(), offsets, offset_trf,
428431
self.get_facecolor(), self.get_edgecolor(),
429432
self._linewidths, self._linestyles,
430-
self._antialiaseds, self.get_hatch(), self._urls,
433+
self._antialiaseds, hatches, self._urls,
431434
"screen") # offset_position, kept for backcompat.
432435

433436
gc.restore()
@@ -532,7 +535,8 @@ def set_hatch(self, hatch):
532535
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
533536
"""
534537
# Use validate_hatch(list) after deprecation.
535-
for h in np.atleast_1d(hatch):
538+
hatch = np.atleast_1d(hatch)
539+
for h in hatch:
536540
mhatch._validate_hatch_pattern(h)
537541
self._hatch = hatch
538542
self.stale = True
@@ -2203,7 +2207,8 @@ def draw(self, renderer):
22032207
coordinates, offsets, offset_trf,
22042208
# Backends expect flattened rgba arrays (n*m, 4) for fc and ec
22052209
self.get_facecolor().reshape((-1, 4)),
2206-
self._antialiased, self.get_edgecolors().reshape((-1, 4)))
2210+
self._antialiased, self.get_edgecolors().reshape((-1, 4)),
2211+
self.get_hatch())
22072212
gc.restore()
22082213
renderer.close_group(self.__class__.__name__)
22092214
self.stale = False

src/_backend_agg.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,10 @@ class RendererAgg
173173
ColorArray &edgecolors,
174174
LineWidthArray &linewidths,
175175
DashesVector &linestyles,
176-
AntialiasedArray &antialiaseds);
176+
AntialiasedArray &antialiaseds,
177+
PathGenerator &hatch_paths);
177178

178-
template <class CoordinateArray, class OffsetArray, class ColorArray>
179+
template <class CoordinateArray, class OffsetArray, class ColorArray, class PathGenerator>
179180
void draw_quad_mesh(GCAgg &gc,
180181
agg::trans_affine &master_transform,
181182
unsigned int mesh_width,
@@ -185,7 +186,8 @@ class RendererAgg
185186
agg::trans_affine &offset_trans,
186187
ColorArray &facecolors,
187188
bool antialiased,
188-
ColorArray &edgecolors);
189+
ColorArray &edgecolors,
190+
PathGenerator &hatch_paths);
189191

190192
template <class PointArray, class ColorArray>
191193
void draw_gouraud_triangles(GCAgg &gc,
@@ -267,6 +269,7 @@ class RendererAgg
267269
LineWidthArray &linewidths,
268270
DashesVector &linestyles,
269271
AntialiasedArray &antialiaseds,
272+
mpl::PathGenerator &hatch_paths,
270273
bool check_snap,
271274
bool has_codes);
272275

@@ -904,6 +907,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
904907
LineWidthArray &linewidths,
905908
DashesVector &linestyles,
906909
AntialiasedArray &antialiaseds,
910+
mpl::PathGenerator &hatch_paths,
907911
bool check_snap,
908912
bool has_codes)
909913
{
@@ -923,6 +927,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
923927
size_t Nedgecolors = safe_first_shape(edgecolors);
924928
size_t Nlinewidths = safe_first_shape(linewidths);
925929
size_t Nlinestyles = std::min(linestyles.size(), N);
930+
size_t Nhatchs = hatch_paths.num_paths();
926931
size_t Naa = safe_first_shape(antialiaseds);
927932

928933
if ((Nfacecolors == 0 && Nedgecolors == 0) || Npaths == 0) {
@@ -988,6 +993,10 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
988993
}
989994
}
990995

996+
if (Nhatchs) {
997+
gc.hatchpath = hatch_paths(i % Nhatchs);
998+
}
999+
9911000
if (check_snap) {
9921001
gc.isaa = antialiaseds(i % Naa);
9931002

@@ -1034,7 +1043,8 @@ inline void RendererAgg::draw_path_collection(GCAgg &gc,
10341043
ColorArray &edgecolors,
10351044
LineWidthArray &linewidths,
10361045
DashesVector &linestyles,
1037-
AntialiasedArray &antialiaseds)
1046+
AntialiasedArray &antialiaseds,
1047+
PathGenerator &hatch_paths)
10381048
{
10391049
_draw_path_collection_generic(gc,
10401050
master_transform,
@@ -1050,6 +1060,7 @@ inline void RendererAgg::draw_path_collection(GCAgg &gc,
10501060
linewidths,
10511061
linestyles,
10521062
antialiaseds,
1063+
hatch_paths,
10531064
true,
10541065
true);
10551066
}
@@ -1127,7 +1138,7 @@ class QuadMeshGenerator
11271138
}
11281139
};
11291140

1130-
template <class CoordinateArray, class OffsetArray, class ColorArray>
1141+
template <class CoordinateArray, class OffsetArray, class ColorArray, class PathGenerator>
11311142
inline void RendererAgg::draw_quad_mesh(GCAgg &gc,
11321143
agg::trans_affine &master_transform,
11331144
unsigned int mesh_width,
@@ -1137,7 +1148,8 @@ inline void RendererAgg::draw_quad_mesh(GCAgg &gc,
11371148
agg::trans_affine &offset_trans,
11381149
ColorArray &facecolors,
11391150
bool antialiased,
1140-
ColorArray &edgecolors)
1151+
ColorArray &edgecolors,
1152+
PathGenerator &hatch_paths)
11411153
{
11421154
QuadMeshGenerator<CoordinateArray> path_generator(mesh_width, mesh_height, coordinates);
11431155

@@ -1160,6 +1172,7 @@ inline void RendererAgg::draw_quad_mesh(GCAgg &gc,
11601172
linewidths,
11611173
linestyles,
11621174
antialiaseds,
1175+
hatch_paths,
11631176
true, // check_snap
11641177
false);
11651178
}

src/_backend_agg_wrapper.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,12 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args)
301301
numpy::array_view<const double, 1> linewidths;
302302
DashesVector dashes;
303303
numpy::array_view<const uint8_t, 1> antialiaseds;
304+
mpl::PathGenerator hatch_paths;
304305
PyObject *ignored;
305306
PyObject *offset_position; // offset position is no longer used
306307

307308
if (!PyArg_ParseTuple(args,
308-
"O&O&O&O&O&O&O&O&O&O&O&OO:draw_path_collection",
309+
"O&O&O&O&O&O&O&O&O&O&O&O&OO:draw_path_collection",
309310
&convert_gcagg,
310311
&gc,
311312
&convert_trans_affine,
@@ -328,6 +329,8 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args)
328329
&dashes,
329330
&antialiaseds.converter,
330331
&antialiaseds,
332+
&convert_pathgen,
333+
&hatch_paths,
331334
&ignored,
332335
&offset_position)) {
333336
return NULL;
@@ -344,7 +347,8 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args)
344347
edgecolors,
345348
linewidths,
346349
dashes,
347-
antialiaseds)));
350+
antialiaseds,
351+
hatch_paths)));
348352

349353
Py_RETURN_NONE;
350354
}
@@ -361,9 +365,10 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
361365
numpy::array_view<const double, 2> facecolors;
362366
bool antialiased;
363367
numpy::array_view<const double, 2> edgecolors;
368+
mpl::PathGenerator hatch_paths;
364369

365370
if (!PyArg_ParseTuple(args,
366-
"O&O&IIO&O&O&O&O&O&:draw_quad_mesh",
371+
"O&O&IIO&O&O&O&O&O&O&:draw_quad_mesh",
367372
&convert_gcagg,
368373
&gc,
369374
&convert_trans_affine,
@@ -381,7 +386,9 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
381386
&convert_bool,
382387
&antialiased,
383388
&convert_colors,
384-
&edgecolors)) {
389+
&edgecolors,
390+
&convert_pathgen,
391+
&hatch_paths)) {
385392
return NULL;
386393
}
387394

@@ -395,7 +402,8 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
395402
offset_trans,
396403
facecolors,
397404
antialiased,
398-
edgecolors)));
405+
edgecolors,
406+
hatch_paths)));
399407

400408
Py_RETURN_NONE;
401409
}

0 commit comments

Comments
 (0)
0