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

Skip to content

Commit d930503

Browse files
Keerushtacaswell
authored andcommitted
Fix incorrect text line spacing. (#8495)
Fixed issue with line spacing being ignored by text closes #7523
1 parent d9b1468 commit d930503

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,24 @@ def test_font_scaling():
421421

422422
for i, fs in enumerate(range(4, 43, 2)):
423423
ax.text(0.1, i*30, "{fs} pt font size".format(fs=fs), fontsize=fs)
424+
425+
426+
@pytest.mark.parametrize('spacing1, spacing2', [(0.4, 2), (2, 0.4), (2, 2)])
427+
def test_two_2line_texts(spacing1, spacing2):
428+
text_string = 'line1\nline2'
429+
fig = plt.figure()
430+
renderer = fig.canvas.get_renderer()
431+
432+
text1 = plt.text(0.25, 0.5, text_string, linespacing=spacing1)
433+
text2 = plt.text(0.25, 0.5, text_string, linespacing=spacing2)
434+
fig.canvas.draw()
435+
436+
box1 = text1.get_window_extent(renderer=renderer)
437+
box2 = text2.get_window_extent(renderer=renderer)
438+
439+
# line spacing only affects height
440+
assert box1.width == box2.width
441+
if (spacing1 == spacing2):
442+
assert box1.height == box2.height
443+
else:
444+
assert box1.height != box2.height

lib/matplotlib/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ def get_prop_tup(self, renderer=None):
912912
hash(self._fontproperties),
913913
self._rotation, self._rotation_mode,
914914
self.figure.dpi, id(renderer or self._renderer),
915+
self._linespacing
915916
)
916917

917918
def get_text(self):

0 commit comments

Comments
 (0)