Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added hatches param to other backends
  • Loading branch information
r3kste committed Aug 16, 2024
commit e1fe14d81510e5bd6f331e2d4fbd2bae01c02075
12 changes: 6 additions & 6 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,

def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
coordinates, offsets, offsetTrans, facecolors,
antialiased, edgecolors):
antialiased, edgecolors, hatches):
"""
Draw a quadmesh.

Expand All @@ -267,7 +267,7 @@ def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,

return self.draw_path_collection(
gc, master_transform, paths, [], offsets, offsetTrans, facecolors,
edgecolors, linewidths, [], [antialiased], [None], 'screen')
edgecolors, linewidths, [], [antialiased], hatches, [None], 'screen')

def draw_gouraud_triangles(self, gc, triangles_array, colors_array,
transform):
Expand Down Expand Up @@ -365,8 +365,8 @@ def _iter_collection(self, gc, path_ids, offsets, offset_trans, facecolors,
Nedgecolors = len(edgecolors)
Nlinewidths = len(linewidths)
Nlinestyles = len(linestyles)
Nurls = len(urls)
Nhatches = len(hatches)
Nurls = len(urls)

if (Nfacecolors == 0 and Nedgecolors == 0) or Npaths == 0:
return
Expand All @@ -386,8 +386,8 @@ def cycle_or_default(seq, default=None):
lws = cycle_or_default(linewidths)
lss = cycle_or_default(linestyles)
aas = cycle_or_default(antialiaseds)
urls = cycle_or_default(urls)
hchs = cycle_or_default(hatches)
urls = cycle_or_default(urls)

if Nedgecolors == 0:
gc0.set_linewidth(0.0)
Expand All @@ -408,10 +408,10 @@ def cycle_or_default(seq, default=None):
if fc is not None and len(fc) == 4 and fc[3] == 0:
fc = None
gc0.set_antialiased(aa)
if Nurls:
gc0.set_url(url)
if Nhatches:
gc0.set_hatch(hch)
if Nurls:
gc0.set_url(url)
yield xo, yo, pathid, gc0, fc
gc0.restore()

Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def draw_markers(
@_log_if_debug_on
def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
linewidths, linestyles, antialiaseds, hatches, urls,
offset_position):
# Is the optimization worth it? Rough calculation:
# cost of emitting a path in-line is
Expand All @@ -530,7 +530,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
return RendererBase.draw_path_collection(
self, gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
linewidths, linestyles, antialiaseds, hatches, urls,
offset_position)

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

Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def draw_markers(

def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
linewidths, linestyles, antialiaseds, hatches, urls,
offset_position):
# Is the optimization worth it? Rough calculation:
# cost of emitting a path in-line is
Expand All @@ -730,7 +730,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
return super().draw_path_collection(
gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
linewidths, linestyles, antialiaseds, hatches, urls,
offset_position)

writer = self.writer
Expand All @@ -749,7 +749,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
gc, path_codes, offsets, offset_trans,
facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):
antialiaseds, hatches, urls, offset_position):
url = gc0.get_url()
if url is not None:
writer.start('a', attrib={'xlink:href': url})
Expand Down
17 changes: 11 additions & 6 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ def draw(self, renderer):
self._set_gc_clip(gc)
gc.set_snap(self.get_snap())

if self._hatch:
gc.set_hatch_color(self._hatch_color)
gc.set_hatch_color(self._hatch_color)

if self.get_sketch_params() is not None:
gc.set_sketch_params(*self.get_sketch_params())
Expand Down Expand Up @@ -411,6 +410,10 @@ def draw(self, renderer):
gc, paths[0], combined_transform.frozen(),
mpath.Path(offsets), offset_trf, tuple(facecolors[0]))
else:
hatches = self.get_hatch()
if isinstance(renderer, mpl.backends.backend_agg.RendererAgg):
hatches = [mpath.Path.hatch(h) for h in hatches]

if self._gapcolor is not None:
# First draw paths within the gaps.
ipaths, ilinestyles = self._get_inverse_paths_linestyles()
Expand All @@ -419,15 +422,15 @@ def draw(self, renderer):
self.get_transforms(), offsets, offset_trf,
[mcolors.to_rgba("none")], self._gapcolor,
self._linewidths, ilinestyles,
self._antialiaseds, self.get_hatch(), self._urls,
self._antialiaseds, hatches, self._urls,
"screen")

renderer.draw_path_collection(
gc, transform.frozen(), paths,
self.get_transforms(), offsets, offset_trf,
self.get_facecolor(), self.get_edgecolor(),
self._linewidths, self._linestyles,
self._antialiaseds, self.get_hatch(), self._urls,
self._antialiaseds, hatches, self._urls,
"screen") # offset_position, kept for backcompat.

gc.restore()
Expand Down Expand Up @@ -532,7 +535,8 @@ def set_hatch(self, hatch):
hatch : {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
"""
# Use validate_hatch(list) after deprecation.
for h in np.atleast_1d(hatch):
hatch = np.atleast_1d(hatch)
for h in hatch:
mhatch._validate_hatch_pattern(h)
self._hatch = hatch
self.stale = True
Expand Down Expand Up @@ -2203,7 +2207,8 @@ def draw(self, renderer):
coordinates, offsets, offset_trf,
# Backends expect flattened rgba arrays (n*m, 4) for fc and ec
self.get_facecolor().reshape((-1, 4)),
self._antialiased, self.get_edgecolors().reshape((-1, 4)))
self._antialiased, self.get_edgecolors().reshape((-1, 4)),
self.get_hatch())
gc.restore()
renderer.close_group(self.__class__.__name__)
self.stale = False
Expand Down
25 changes: 19 additions & 6 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ class RendererAgg
ColorArray &edgecolors,
LineWidthArray &linewidths,
DashesVector &linestyles,
AntialiasedArray &antialiaseds);
AntialiasedArray &antialiaseds,
PathGenerator &hatch_paths);

template <class CoordinateArray, class OffsetArray, class ColorArray>
template <class CoordinateArray, class OffsetArray, class ColorArray, class PathGenerator>
void draw_quad_mesh(GCAgg &gc,
agg::trans_affine &master_transform,
unsigned int mesh_width,
Expand All @@ -185,7 +186,8 @@ class RendererAgg
agg::trans_affine &offset_trans,
ColorArray &facecolors,
bool antialiased,
ColorArray &edgecolors);
ColorArray &edgecolors,
PathGenerator &hatch_paths);

template <class PointArray, class ColorArray>
void draw_gouraud_triangles(GCAgg &gc,
Expand Down Expand Up @@ -267,6 +269,7 @@ class RendererAgg
LineWidthArray &linewidths,
DashesVector &linestyles,
AntialiasedArray &antialiaseds,
mpl::PathGenerator &hatch_paths,
bool check_snap,
bool has_codes);

Expand Down Expand Up @@ -904,6 +907,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
LineWidthArray &linewidths,
DashesVector &linestyles,
AntialiasedArray &antialiaseds,
mpl::PathGenerator &hatch_paths,
bool check_snap,
bool has_codes)
{
Expand All @@ -923,6 +927,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
size_t Nedgecolors = safe_first_shape(edgecolors);
size_t Nlinewidths = safe_first_shape(linewidths);
size_t Nlinestyles = std::min(linestyles.size(), N);
size_t Nhatchs = hatch_paths.num_paths();
size_t Naa = safe_first_shape(antialiaseds);

if ((Nfacecolors == 0 && Nedgecolors == 0) || Npaths == 0) {
Expand Down Expand Up @@ -988,6 +993,10 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
}
}

if (Nhatchs) {
gc.hatchpath = hatch_paths(i % Nhatchs);
}

if (check_snap) {
gc.isaa = antialiaseds(i % Naa);

Expand Down Expand Up @@ -1034,7 +1043,8 @@ inline void RendererAgg::draw_path_collection(GCAgg &gc,
ColorArray &edgecolors,
LineWidthArray &linewidths,
DashesVector &linestyles,
AntialiasedArray &antialiaseds)
AntialiasedArray &antialiaseds,
PathGenerator &hatch_paths)
{
_draw_path_collection_generic(gc,
master_transform,
Expand All @@ -1050,6 +1060,7 @@ inline void RendererAgg::draw_path_collection(GCAgg &gc,
linewidths,
linestyles,
antialiaseds,
hatch_paths,
true,
true);
}
Expand Down Expand Up @@ -1127,7 +1138,7 @@ class QuadMeshGenerator
}
};

template <class CoordinateArray, class OffsetArray, class ColorArray>
template <class CoordinateArray, class OffsetArray, class ColorArray, class PathGenerator>
inline void RendererAgg::draw_quad_mesh(GCAgg &gc,
agg::trans_affine &master_transform,
unsigned int mesh_width,
Expand All @@ -1137,7 +1148,8 @@ inline void RendererAgg::draw_quad_mesh(GCAgg &gc,
agg::trans_affine &offset_trans,
ColorArray &facecolors,
bool antialiased,
ColorArray &edgecolors)
ColorArray &edgecolors,
PathGenerator &hatch_paths)
{
QuadMeshGenerator<CoordinateArray> path_generator(mesh_width, mesh_height, coordinates);

Expand All @@ -1160,6 +1172,7 @@ inline void RendererAgg::draw_quad_mesh(GCAgg &gc,
linewidths,
linestyles,
antialiaseds,
hatch_paths,
true, // check_snap
false);
}
Expand Down
18 changes: 13 additions & 5 deletions src/_backend_agg_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,12 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args)
numpy::array_view<const double, 1> linewidths;
DashesVector dashes;
numpy::array_view<const uint8_t, 1> antialiaseds;
mpl::PathGenerator hatch_paths;
PyObject *ignored;
PyObject *offset_position; // offset position is no longer used

if (!PyArg_ParseTuple(args,
"O&O&O&O&O&O&O&O&O&O&O&OO:draw_path_collection",
"O&O&O&O&O&O&O&O&O&O&O&O&OO:draw_path_collection",
&convert_gcagg,
&gc,
&convert_trans_affine,
Expand All @@ -328,6 +329,8 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args)
&dashes,
&antialiaseds.converter,
&antialiaseds,
&convert_pathgen,
&hatch_paths,
&ignored,
&offset_position)) {
return NULL;
Expand All @@ -344,7 +347,8 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args)
edgecolors,
linewidths,
dashes,
antialiaseds)));
antialiaseds,
hatch_paths)));

Py_RETURN_NONE;
}
Expand All @@ -361,9 +365,10 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
numpy::array_view<const double, 2> facecolors;
bool antialiased;
numpy::array_view<const double, 2> edgecolors;
mpl::PathGenerator hatch_paths;

if (!PyArg_ParseTuple(args,
"O&O&IIO&O&O&O&O&O&:draw_quad_mesh",
"O&O&IIO&O&O&O&O&O&O&:draw_quad_mesh",
&convert_gcagg,
&gc,
&convert_trans_affine,
Expand All @@ -381,7 +386,9 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
&convert_bool,
&antialiased,
&convert_colors,
&edgecolors)) {
&edgecolors,
&convert_pathgen,
&hatch_paths)) {
return NULL;
}

Expand All @@ -395,7 +402,8 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
offset_trans,
facecolors,
antialiased,
edgecolors)));
edgecolors,
hatch_paths)));

Py_RETURN_NONE;
}
Expand Down