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

Skip to content

Commit 74d7eb8

Browse files
committed
fix contour hatching drawing one by one, using PathCollection
1 parent 84ae1a7 commit 74d7eb8

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

lib/matplotlib/contour.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
455455

456456
idx_level_min, idx_vtx_min, proj = self._find_nearest_contour(
457457
(x, y), self.labelIndiceList)
458-
path = self._paths[idx_level_min]
458+
path = self._container.paths[idx_level_min]
459459
level = self.labelIndiceList.index(idx_level_min)
460460
label_width = self._get_nth_label_width(level)
461461
rotation, path = self._split_path_and_get_label_rotation(
@@ -464,7 +464,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
464464
self.labelCValueList[idx_level_min])
465465

466466
if inline:
467-
self._paths[idx_level_min] = path
467+
self._container.paths[idx_level_min] = path
468468

469469
def pop_label(self, index=-1):
470470
"""Defaults to removing last label, but any index can be supplied"""
@@ -481,7 +481,7 @@ def labels(self, inline, inline_spacing):
481481
trans = self.get_transform()
482482
label_width = self._get_nth_label_width(idx)
483483
additions = []
484-
for subpath in self._paths[icon]._iter_connected_components():
484+
for subpath in self._container.paths[icon]._iter_connected_components():
485485
screen_xys = trans.transform(subpath.vertices)
486486
# Check if long enough for a label
487487
if self.print_label(screen_xys, label_width):
@@ -497,7 +497,7 @@ def labels(self, inline, inline_spacing):
497497
# After looping over all segments on a contour, replace old path by new one
498498
# if inlining.
499499
if inline:
500-
self._paths[icon] = Path.make_compound_path(*additions)
500+
self._container.paths[icon] = Path.make_compound_path(*additions)
501501

502502
def remove(self):
503503
super().remove()
@@ -757,8 +757,8 @@ def __init__(self, ax, *args,
757757
self.norm._changed()
758758
self._process_colors()
759759

760-
if self._paths is None:
761-
self._paths = self._make_paths_from_contour_generator()
760+
if self._container.paths is None:
761+
self._container.paths = self._make_paths_from_contour_generator()
762762

763763
if self.filled:
764764
if linewidths is not None:
@@ -839,7 +839,7 @@ def legend_elements(self, variable_name='x', str_format=str):
839839

840840
if self.filled:
841841
lowers, uppers = self._get_lowers_and_uppers()
842-
n_levels = len(self._paths)
842+
n_levels = len(self._container.paths)
843843
for idx in range(n_levels):
844844
artists.append(mpatches.Rectangle(
845845
(0, 0), 1, 1,
@@ -905,15 +905,15 @@ def _process_args(self, *args, **kwargs):
905905
# pathcodes. However, kinds can also be None; in which case all paths in that
906906
# list are codeless (this case is normalized above). These lists are used to
907907
# construct paths, which then get concatenated.
908-
self._paths = [Path.make_compound_path(*map(Path, segs, kinds))
908+
self._container.paths = [Path.make_compound_path(*map(Path, segs, kinds))
909909
for segs, kinds in zip(allsegs, allkinds)]
910910

911911
return kwargs
912912

913913
def _make_paths_from_contour_generator(self):
914914
"""Compute ``paths`` using C extension."""
915-
if self._paths is not None:
916-
return self._paths
915+
if self._container.paths is not None:
916+
return self._container.paths
917917
cg = self._contour_generator
918918
empty_path = Path(np.empty((0, 2)))
919919
vertices_and_codes = (
@@ -1180,13 +1180,13 @@ def _find_nearest_contour(self, xy, indices=None):
11801180
raise ValueError("Method does not support filled contours")
11811181

11821182
if indices is None:
1183-
indices = range(len(self._paths))
1183+
indices = range(len(self._container.paths))
11841184

11851185
d2min = np.inf
11861186
idx_level_min = idx_vtx_min = proj_min = None
11871187

11881188
for idx_level in indices:
1189-
path = self._paths[idx_level]
1189+
path = self._container.paths[idx_level]
11901190
idx_vtx_start = 0
11911191
for subpath in path._iter_connected_components():
11921192
if not len(subpath.vertices):
@@ -1249,7 +1249,8 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
12491249

12501250
if i_level is not None:
12511251
cc_cumlens = np.cumsum(
1252-
[*map(len, self._paths[i_level]._iter_connected_components())])
1252+
[*map(len, self._container.paths[i_level]._iter_connected_components())]
1253+
)
12531254
segment = cc_cumlens.searchsorted(i_vtx, "right")
12541255
index = i_vtx if segment == 0 else i_vtx - cc_cumlens[segment - 1]
12551256
d2 = (xmin-x)**2 + (ymin-y)**2
@@ -1258,7 +1259,7 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
12581259

12591260
@artist.allow_rasterization
12601261
def draw(self, renderer):
1261-
paths = self._paths
1262+
paths = self._container.paths
12621263
n_paths = len(paths)
12631264
if not self.filled or all(hatch is None for hatch in self.hatches):
12641265
super().draw(renderer)
@@ -1268,14 +1269,24 @@ def draw(self, renderer):
12681269
if edgecolors.size == 0:
12691270
edgecolors = ("none",)
12701271
for idx in range(n_paths):
1271-
with cbook._setattr_cm(self, _paths=[paths[idx]]), self._cm_set(
1272-
hatch=self.hatches[idx % len(self.hatches)],
1273-
array=[self.get_array()[idx]],
1274-
linewidths=[self.get_linewidths()[idx % len(self.get_linewidths())]],
1275-
linestyles=[self.get_linestyles()[idx % len(self.get_linestyles())]],
1276-
edgecolors=edgecolors[idx % len(edgecolors)],
1277-
):
1278-
super().draw(renderer)
1272+
contour = mcoll.PathCollection(paths=[paths[idx]])
1273+
contour.update_from(self)
1274+
contour.set_linewidths(
1275+
[self.get_linewidths()[idx % len(self.get_linewidths())]]
1276+
)
1277+
contour.set_linestyles(
1278+
[self.get_linestyles()[idx % len(self.get_linestyles())]]
1279+
)
1280+
contour.set_edgecolors(edgecolors[idx % len(edgecolors)])
1281+
contour.set_hatch(self.hatches[idx % len(self.hatches)])
1282+
contour.set_array([self.get_array()[idx]])
1283+
contour.set_norm(self.norm)
1284+
contour.set_cmap(self.cmap)
1285+
1286+
contour.set_transform(self.get_transform())
1287+
contour.draw(renderer)
1288+
1289+
12791290

12801291

12811292
@_docstring.interpd

0 commit comments

Comments
 (0)