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

Skip to content

Commit f25b5d2

Browse files
committed
Cleanup demo_text_path.
1 parent ae93d77 commit f25b5d2

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

examples/text_labels_and_annotations/demo_text_path.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@
99
"""
1010

1111
import matplotlib.pyplot as plt
12-
from matplotlib.image import BboxImage
13-
import numpy as np
14-
from matplotlib.transforms import IdentityTransform
15-
16-
import matplotlib.patches as mpatches
17-
18-
from matplotlib.offsetbox import AnnotationBbox,\
19-
AnchoredOffsetbox, AuxTransformBox
20-
2112
from matplotlib.cbook import get_sample_data
22-
13+
from matplotlib.image import BboxImage
14+
from matplotlib.offsetbox import (
15+
AnnotationBbox, AnchoredOffsetbox, AuxTransformBox)
16+
from matplotlib.patches import PathPatch, Shadow
2317
from matplotlib.text import TextPath
18+
from matplotlib.transforms import IdentityTransform
19+
import numpy as np
2420

2521

26-
class PathClippedImagePatch(mpatches.PathPatch):
22+
class PathClippedImagePatch(PathPatch):
2723
"""
2824
The given image is used to draw the face of the patch. Internally,
2925
it uses BboxImage whose clippath set to the path of the patch.
@@ -32,31 +28,20 @@ class PathClippedImagePatch(mpatches.PathPatch):
3228
"""
3329

3430
def __init__(self, path, bbox_image, **kwargs):
35-
mpatches.PathPatch.__init__(self, path, **kwargs)
36-
self._init_bbox_image(bbox_image)
31+
super().__init__(path, **kwargs)
32+
self.bbox_image = BboxImage(
33+
self.get_window_extent, norm=None, origin=None)
34+
self.bbox_image.set_data(bbox_image)
3735

3836
def set_facecolor(self, color):
3937
"""Simply ignore facecolor."""
40-
mpatches.PathPatch.set_facecolor(self, "none")
41-
42-
def _init_bbox_image(self, im):
43-
44-
bbox_image = BboxImage(self.get_window_extent,
45-
norm=None,
46-
origin=None,
47-
)
48-
bbox_image.set_transform(IdentityTransform())
49-
50-
bbox_image.set_data(im)
51-
self.bbox_image = bbox_image
38+
super().set_facecolor("none")
5239

5340
def draw(self, renderer=None):
54-
5541
# the clip path must be updated every draw. any solution? -JJ
5642
self.bbox_image.set_clip_path(self._path, self.get_transform())
5743
self.bbox_image.draw(renderer)
58-
59-
mpatches.PathPatch.draw(self, renderer)
44+
super().draw(renderer)
6045

6146

6247
if __name__ == "__main__":
@@ -93,8 +78,7 @@ def draw(self, renderer=None):
9378
else:
9479
r = r"textpath supports mathtext & TeX"
9580

96-
text_path = TextPath((0, 0), r,
97-
size=20, usetex=usetex)
81+
text_path = TextPath((0, 0), r, size=20, usetex=usetex)
9882

9983
p1 = PathPatch(text_path, ec="w", lw=3, fc="w", alpha=0.9,
10084
transform=IdentityTransform())
@@ -121,7 +105,7 @@ def draw(self, renderer=None):
121105

122106
ax = plt.subplot(212)
123107

124-
arr = np.arange(256).reshape(1, 256)/256.
108+
arr = np.arange(256).reshape(1, 256) / 256
125109

126110
if usetex:
127111
s = (r"$\displaystyle\left[\sum_{n=1}^\infty"
@@ -132,10 +116,8 @@ def draw(self, renderer=None):
132116
text_patch = PathClippedImagePatch(text_path, arr, ec="none",
133117
transform=IdentityTransform())
134118

135-
shadow1 = mpatches.Shadow(text_patch, 1, -1,
136-
props=dict(fc="none", ec="0.6", lw=3))
137-
shadow2 = mpatches.Shadow(text_patch, 1, -1,
138-
props=dict(fc="0.3", ec="none"))
119+
shadow1 = Shadow(text_patch, 1, -1, props=dict(fc="none", ec="0.6", lw=3))
120+
shadow2 = Shadow(text_patch, 1, -1, props=dict(fc="0.3", ec="none"))
139121

140122
# make offset box
141123
offsetbox = AuxTransformBox(IdentityTransform())

0 commit comments

Comments
 (0)