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

Skip to content

Commit c5f3734

Browse files
committed
Fix the linespacing bug of multiline text (#1239682)
svn path=/trunk/matplotlib/; revision=7120
1 parent 567611c commit c5f3734

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-23
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2009-05-18 Fix the linespacing bug of multiline text (#1239682). See
2+
examples/pylab_examples/multiline.py -JJL
3+
14
2009-05-18 Add *annotation_clip* attr. for text.Annotation class.
25
If True, annotation is only drawn when the annotated point is
36
inside the axes area. -JJL

examples/pylab_examples/multiline.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
11
#!/usr/bin/env python
22
from pylab import *
3+
#from matplotlib.pyplot import *
4+
#from numpy import arange
35

4-
plot(arange(10))
5-
xlabel('this is a xlabel\n(with newlines!)')
6-
ylabel('this is vertical\ntest', multialignment='center')
7-
#ylabel('this is another!')
8-
text(2, 7,'this is\nyet another test',
9-
rotation=45,
10-
horizontalalignment = 'center',
11-
verticalalignment = 'top',
12-
multialignment = 'center')
13-
14-
grid(True)
6+
if 1:
7+
figure(figsize=(7, 4))
8+
ax = subplot(121)
9+
ax.set_aspect(1)
10+
plot(arange(10))
11+
xlabel('this is a xlabel\n(with newlines!)')
12+
ylabel('this is vertical\ntest', multialignment='center')
13+
#ylabel('this is another!')
14+
text(2, 7,'this is\nyet another test',
15+
rotation=45,
16+
horizontalalignment = 'center',
17+
verticalalignment = 'top',
18+
multialignment = 'center')
19+
20+
grid(True)
21+
22+
23+
24+
subplot(122)
25+
26+
text(0.29, 0.7, "Mat\nTTp\n123", size=18,
27+
va="baseline", ha="right", multialignment="left",
28+
bbox=dict(fc="none"))
29+
30+
text(0.34, 0.7, "Mag\nTTT\n123", size=18,
31+
va="baseline", ha="left", multialignment="left",
32+
bbox=dict(fc="none"))
33+
34+
text(0.95, 0.7, "Mag\nTTT$^{A^A}$\n123", size=18,
35+
va="baseline", ha="right", multialignment="left",
36+
bbox=dict(fc="none"))
37+
38+
xticks([0.2, 0.4, 0.6, 0.8, 1.],
39+
["Jan\n2009","Feb\n2009","Mar\n2009", "Apr\n2009", "May\n2009"])
40+
41+
axhline(0.7)
42+
title("test line spacing for multiline text")
43+
44+
subplots_adjust(bottom=0.25, top=0.8)
45+
draw()
1546
show()

lib/matplotlib/text.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
from matplotlib import cbook
1010
from matplotlib import rcParams
11-
import artist
12-
from artist import Artist
13-
from cbook import is_string_like, maxdict
14-
from font_manager import FontProperties
15-
from patches import bbox_artist, YAArrow, FancyBboxPatch, \
11+
import matplotlib.artist as artist
12+
from matplotlib.artist import Artist
13+
from matplotlib.cbook import is_string_like, maxdict
14+
from matplotlib.font_manager import FontProperties
15+
from matplotlib.patches import bbox_artist, YAArrow, FancyBboxPatch, \
1616
FancyArrowPatch, Rectangle
17-
import transforms as mtransforms
18-
from transforms import Affine2D, Bbox
19-
from lines import Line2D
17+
import matplotlib.transforms as mtransforms
18+
from matplotlib.transforms import Affine2D, Bbox
19+
from matplotlib.lines import Line2D
2020

2121
import matplotlib.nxutils as nxutils
2222

@@ -227,6 +227,11 @@ def update_from(self, other):
227227
self._linespacing = other._linespacing
228228

229229
def _get_layout(self, renderer):
230+
"""
231+
return the extent (bbox) of the text together with
232+
multile-alignment information. Note that it returns a extent
233+
of a rotated text when necessary.
234+
"""
230235
key = self.get_prop_tup()
231236
if key in self.cached: return self.cached[key]
232237

@@ -242,9 +247,9 @@ def _get_layout(self, renderer):
242247

243248
# Find full vertical extent of font,
244249
# including ascenders and descenders:
245-
tmp, heightt, bl = renderer.get_text_width_height_descent(
250+
tmp, lp_h, lp_bl = renderer.get_text_width_height_descent(
246251
'lp', self._fontproperties, ismath=False)
247-
offsety = heightt * self._linespacing
252+
offsety = lp_h * self._linespacing
248253

249254
baseline = None
250255
for i, line in enumerate(lines):
@@ -254,8 +259,22 @@ def _get_layout(self, renderer):
254259
if baseline is None:
255260
baseline = h - d
256261
whs[i] = w, h
257-
horizLayout[i] = thisx, thisy, w, h
258-
thisy -= offsety
262+
263+
# For general multiline text, we will have a fixed spacing
264+
# between the "baseline" of the upper line and "top" of
265+
# the lower line (instead of the "bottom" of the upper
266+
# line and "top" of the lower line)
267+
268+
# For multiline text, increase the line spacing when the
269+
# text net-height(excluding baseline) is larger than that
270+
# of a "l" (e.g., use of superscripts), which seems
271+
# what TeX does.
272+
273+
d_yoffset = max(0, (h-d)-(lp_h-lp_bl))
274+
275+
horizLayout[i] = thisx, thisy-(d + d_yoffset), \
276+
w, h
277+
thisy -= offsety + d_yoffset
259278
width = max(width, w)
260279

261280
ymin = horizLayout[-1][1]
@@ -1688,3 +1707,4 @@ def draw(self, renderer):
16881707

16891708

16901709
artist.kwdocd['Annotation'] = Annotation.__init__.__doc__
1710+

0 commit comments

Comments
 (0)