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

Skip to content

Commit b3ef54f

Browse files
committed
Fix path_effects to work on text with spaces only
When a line of text contains no visual output (spaces only or empty newlines), there is no path to create for the effects. So, the data array for this non-path does not have the commonly expected shape for a path, so the logic bails with an error. The solution is, do not try to draw a path when the text string has only spaces and newlines. closes #22687
1 parent 11737d0 commit b3ef54f

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

lib/matplotlib/patheffects.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ def draw_path_collection(self, gc, master_transform, paths, *args,
138138
*args, **kwargs)
139139

140140
def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):
141+
if not s.strip():
142+
return
143+
141144
# Implements the naive text drawing as is found in RendererBase.
142145
path, transform = self._get_text_path_transform(x, y, s, prop,
143146
angle, ismath)

lib/matplotlib/tests/test_patheffects.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,16 @@ def test_tickedstroke():
189189

190190
ax3.set_xlim(0, 4)
191191
ax3.set_ylim(0, 4)
192+
193+
194+
@image_comparison(['spaces_and_newlines.png'], remove_text=True)
195+
def test_patheffects_spaces_and_newlines():
196+
ax = plt.subplot()
197+
s1 = " "
198+
s2 = "\nNewline also causes problems"
199+
text1 = ax.text(0.5, 0.75, s1, ha='center', va='center', size=20,
200+
bbox={'color': 'salmon'})
201+
text2 = ax.text(0.5, 0.25, s2, ha='center', va='center', size=20,
202+
bbox={'color': 'thistle'})
203+
text1.set_path_effects([path_effects.Normal()])
204+
text2.set_path_effects([path_effects.Normal()])

0 commit comments

Comments
 (0)