forked from matplotlib/matplotlib
/
test_text.py
479 lines (382 loc) · 14.4 KB
/
test_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import six
import warnings
import numpy as np
from numpy.testing import assert_almost_equal
from nose.tools import eq_, assert_raises
from nose.plugins.skip import SkipTest
from matplotlib.transforms import Bbox
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import image_comparison, cleanup
from matplotlib.figure import Figure
from matplotlib.text import Annotation, Text
from matplotlib.backends.backend_agg import RendererAgg
@image_comparison(baseline_images=['font_styles'])
def test_font_styles():
from matplotlib import _get_data_path
data_path = _get_data_path()
def find_matplotlib_font(**kw):
prop = FontProperties(**kw)
path = findfont(prop, directory=data_path)
return FontProperties(fname=path)
from matplotlib.font_manager import FontProperties, findfont
warnings.filterwarnings(
'ignore',
('findfont: Font family \[u?\'Foo\'\] not found. Falling back to .'),
UserWarning,
module='matplotlib.font_manager')
plt.figure()
ax = plt.subplot(1, 1, 1)
normalFont = find_matplotlib_font(
family="sans-serif",
style="normal",
variant="normal",
size=14)
ax.annotate(
"Normal Font",
(0.1, 0.1),
xycoords='axes fraction',
fontproperties=normalFont)
boldFont = find_matplotlib_font(
family="Foo",
style="normal",
variant="normal",
weight="bold",
stretch=500,
size=14)
ax.annotate(
"Bold Font",
(0.1, 0.2),
xycoords='axes fraction',
fontproperties=boldFont)
boldItemFont = find_matplotlib_font(
family="sans serif",
style="italic",
variant="normal",
weight=750,
stretch=500,
size=14)
ax.annotate(
"Bold Italic Font",
(0.1, 0.3),
xycoords='axes fraction',
fontproperties=boldItemFont)
lightFont = find_matplotlib_font(
family="sans-serif",
style="normal",
variant="normal",
weight=200,
stretch=500,
size=14)
ax.annotate(
"Light Font",
(0.1, 0.4),
xycoords='axes fraction',
fontproperties=lightFont)
condensedFont = find_matplotlib_font(
family="sans-serif",
style="normal",
variant="normal",
weight=500,
stretch=100,
size=14)
ax.annotate(
"Condensed Font",
(0.1, 0.5),
xycoords='axes fraction',
fontproperties=condensedFont)
ax.set_xticks([])
ax.set_yticks([])
@image_comparison(baseline_images=['multiline'])
def test_multiline():
plt.figure()
ax = plt.subplot(1, 1, 1)
ax.set_title("multiline\ntext alignment")
plt.text(
0.2, 0.5, "TpTpTp\n$M$\nTpTpTp", size=20, ha="center", va="top")
plt.text(
0.5, 0.5, "TpTpTp\n$M^{M^{M^{M}}}$\nTpTpTp", size=20,
ha="center", va="top")
plt.text(
0.8, 0.5, "TpTpTp\n$M_{q_{q_{q}}}$\nTpTpTp", size=20,
ha="center", va="top")
plt.xlim(0, 1)
plt.ylim(0, 0.8)
ax.set_xticks([])
ax.set_yticks([])
@image_comparison(baseline_images=['antialiased'], extensions=['png'])
def test_antialiasing():
matplotlib.rcParams['text.antialiased'] = True
fig = plt.figure(figsize=(5.25, 0.75))
fig.text(0.5, 0.75, "antialiased", horizontalalignment='center',
verticalalignment='center')
fig.text(0.5, 0.25, "$\sqrt{x}$", horizontalalignment='center',
verticalalignment='center')
# NOTE: We don't need to restore the rcParams here, because the
# test cleanup will do it for us. In fact, if we do it here, it
# will turn antialiasing back off before the images are actually
# rendered.
def test_afm_kerning():
from matplotlib.afm import AFM
from matplotlib.font_manager import findfont
fn = findfont("Helvetica", fontext="afm")
with open(fn, 'rb') as fh:
afm = AFM(fh)
assert afm.string_width_height('VAVAVAVAVAVA') == (7174.0, 718)
@image_comparison(baseline_images=['text_contains'], extensions=['png'])
def test_contains():
import matplotlib.backend_bases as mbackend
fig = plt.figure()
ax = plt.axes()
mevent = mbackend.MouseEvent(
'button_press_event', fig.canvas, 0.5, 0.5, 1, None)
xs = np.linspace(0.25, 0.75, 30)
ys = np.linspace(0.25, 0.75, 30)
xs, ys = np.meshgrid(xs, ys)
txt = plt.text(
0.48, 0.52, 'hello world', ha='center', fontsize=30, rotation=30)
# uncomment to draw the text's bounding box
# txt.set_bbox(dict(edgecolor='black', facecolor='none'))
# draw the text. This is important, as the contains method can only work
# when a renderer exists.
plt.draw()
for x, y in zip(xs.flat, ys.flat):
mevent.x, mevent.y = plt.gca().transAxes.transform_point([x, y])
contains, _ = txt.contains(mevent)
color = 'yellow' if contains else 'red'
# capture the viewLim, plot a point, and reset the viewLim
vl = ax.viewLim.frozen()
ax.plot(x, y, 'o', color=color)
ax.viewLim.set(vl)
@image_comparison(baseline_images=['titles'])
def test_titles():
# left and right side titles
plt.figure()
ax = plt.subplot(1, 1, 1)
ax.set_title("left title", loc="left")
ax.set_title("right title", loc="right")
ax.set_xticks([])
ax.set_yticks([])
@image_comparison(baseline_images=['text_alignment'])
def test_alignment():
plt.figure()
ax = plt.subplot(1, 1, 1)
x = 0.1
for rotation in (0, 30):
for alignment in ('top', 'bottom', 'baseline', 'center'):
ax.text(
x, 0.5, alignment + " Tj", va=alignment, rotation=rotation,
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))
ax.text(
x, 1.0, r'$\sum_{i=0}^{j}$', va=alignment, rotation=rotation)
x += 0.1
ax.plot([0, 1], [0.5, 0.5])
ax.plot([0, 1], [1.0, 1.0])
ax.set_xlim([0, 1])
ax.set_ylim([0, 1.5])
ax.set_xticks([])
ax.set_yticks([])
@image_comparison(baseline_images=['axes_titles'], extensions=['png'])
def test_axes_titles():
# Related to issue #3327
plt.figure()
ax = plt.subplot(1, 1, 1)
ax.set_title('center', loc='center', fontsize=20, fontweight=700)
ax.set_title('left', loc='left', fontsize=12, fontweight=400)
ax.set_title('right', loc='right', fontsize=12, fontweight=400)
@cleanup
def test_set_position():
fig, ax = plt.subplots()
# test set_position
ann = ax.annotate(
'test', (0, 0), xytext=(0, 0), textcoords='figure pixels')
plt.draw()
init_pos = ann.get_window_extent(fig.canvas.renderer)
shift_val = 15
ann.set_position((shift_val, shift_val))
plt.draw()
post_pos = ann.get_window_extent(fig.canvas.renderer)
for a, b in zip(init_pos.min, post_pos.min):
assert a + shift_val == b
# test xyann
ann = ax.annotate(
'test', (0, 0), xytext=(0, 0), textcoords='figure pixels')
plt.draw()
init_pos = ann.get_window_extent(fig.canvas.renderer)
shift_val = 15
ann.xyann = (shift_val, shift_val)
plt.draw()
post_pos = ann.get_window_extent(fig.canvas.renderer)
for a, b in zip(init_pos.min, post_pos.min):
assert a + shift_val == b
def test_get_rotation_string():
from matplotlib import text
assert text.get_rotation('horizontal') == 0.
assert text.get_rotation('vertical') == 90.
assert text.get_rotation('15.') == 15.
def test_get_rotation_float():
from matplotlib import text
for i in [15., 16.70, 77.4]:
assert text.get_rotation(i) == i
def test_get_rotation_int():
from matplotlib import text
for i in [67, 16, 41]:
assert text.get_rotation(i) == float(i)
def test_get_rotation_raises():
from matplotlib import text
import sys
if sys.version_info[:2] < (2, 7):
raise SkipTest("assert_raises as context manager "
"not supported with Python < 2.7")
with assert_raises(ValueError):
text.get_rotation('hozirontal')
def test_get_rotation_none():
from matplotlib import text
assert text.get_rotation(None) == 0.0
def test_get_rotation_mod360():
from matplotlib import text
for i, j in zip([360., 377., 720+177.2], [0., 17., 177.2]):
assert_almost_equal(text.get_rotation(i), j)
@image_comparison(baseline_images=['text_bboxclip'])
def test_bbox_clipping():
plt.text(0.9, 0.2, 'Is bbox clipped?', backgroundcolor='r', clip_on=True)
t = plt.text(0.9, 0.5, 'Is fancy bbox clipped?', clip_on=True)
t.set_bbox({"boxstyle": "round, pad=0.1"})
@image_comparison(baseline_images=['annotation_negative_coords'],
extensions=['png'])
def test_annotation_negative_coords():
fig = plt.figure()
ax = plt.subplot(1, 1, 1)
ax.annotate("+fpt", (15, 40), xycoords="figure points")
ax.annotate("+fpx", (25, 30), xycoords="figure pixels")
ax.annotate("+apt", (35, 20), xycoords="axes points")
ax.annotate("+apx", (45, 10), xycoords="axes pixels")
ax.annotate("-fpt", (-55, -40), xycoords="figure points")
ax.annotate("-fpx", (-45, -30), xycoords="figure pixels")
ax.annotate("-apt", (-35, -20), xycoords="axes points")
ax.annotate("-apx", (-25, -10), xycoords="axes pixels")
@cleanup
def test_text_annotation_get_window_extent():
figure = Figure(dpi=100)
renderer = RendererAgg(200, 200, 100)
# Only text annotation
annotation = Annotation('test', xy=(0, 0))
annotation.set_figure(figure)
text = Text(text='test', x=0, y=0)
text.set_figure(figure)
bbox = annotation.get_window_extent(renderer=renderer)
text_bbox = text.get_window_extent(renderer=renderer)
eq_(bbox.width, text_bbox.width)
eq_(bbox.height, text_bbox.height)
_, _, d = renderer.get_text_width_height_descent(
'text', annotation._fontproperties, ismath=False)
_, _, lp_d = renderer.get_text_width_height_descent(
'lp', annotation._fontproperties, ismath=False)
below_line = max(d, lp_d)
# These numbers are specific to the current implementation of Text
points = bbox.get_points()
eq_(points[0, 0], 0.0)
eq_(points[1, 0], text_bbox.width)
eq_(points[0, 1], -below_line)
eq_(points[1, 1], text_bbox.height - below_line)
@cleanup
def test_text_with_arrow_annotation_get_window_extent():
headwidth = 21
fig, ax = plt.subplots(dpi=100)
txt = ax.text(s='test', x=0, y=0)
ann = ax.annotate(
'test',
xy=(0.0, 50.0),
xytext=(50.0, 50.0), xycoords='figure pixels',
arrowprops={
'facecolor': 'black', 'width': 2,
'headwidth': headwidth, 'shrink': 0.0})
plt.draw()
renderer = fig.canvas.renderer
# bounding box of text
text_bbox = txt.get_window_extent(renderer=renderer)
# bounding box of annotation (text + arrow)
bbox = ann.get_window_extent(renderer=renderer)
# bounding box of arrow
arrow_bbox = ann.arrow.get_window_extent(renderer)
# bounding box of annotation text
ann_txt_bbox = Text.get_window_extent(ann)
# make sure annotation with in 50 px wider than
# just the text
eq_(bbox.width, text_bbox.width + 50.0)
# make sure the annotation text bounding box is same size
# as the bounding box of the same string as a Text object
eq_(ann_txt_bbox.height, text_bbox.height)
eq_(ann_txt_bbox.width, text_bbox.width)
# compute the expected bounding box of arrow + text
expected_bbox = Bbox.union([ann_txt_bbox, arrow_bbox])
assert_almost_equal(bbox.height, expected_bbox.height)
@cleanup
def test_arrow_annotation_get_window_extent():
figure = Figure(dpi=100)
figure.set_figwidth(2.0)
figure.set_figheight(2.0)
renderer = RendererAgg(200, 200, 100)
# Text annotation with arrow
annotation = Annotation(
'', xy=(0.0, 50.0), xytext=(50.0, 50.0), xycoords='figure pixels',
arrowprops={
'facecolor': 'black', 'width': 8, 'headwidth': 10, 'shrink': 0.0})
annotation.set_figure(figure)
annotation.draw(renderer)
bbox = annotation.get_window_extent()
points = bbox.get_points()
eq_(bbox.width, 50.0)
assert_almost_equal(bbox.height, 10.0 / 0.72)
eq_(points[0, 0], 0.0)
eq_(points[0, 1], 50.0 - 5 / 0.72)
@cleanup
def test_empty_annotation_get_window_extent():
figure = Figure(dpi=100)
figure.set_figwidth(2.0)
figure.set_figheight(2.0)
renderer = RendererAgg(200, 200, 100)
# Text annotation with arrow
annotation = Annotation(
'', xy=(0.0, 50.0), xytext=(0.0, 50.0), xycoords='figure pixels')
annotation.set_figure(figure)
annotation.draw(renderer)
bbox = annotation.get_window_extent()
points = bbox.get_points()
eq_(points[0, 0], 0.0)
eq_(points[1, 0], 0.0)
eq_(points[1, 1], 50.0)
eq_(points[0, 1], 50.0)
@image_comparison(baseline_images=['basictext_wrap'],
extensions=['png'])
def test_basic_wrap():
fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = "This is a really long string that I'd rather have wrapped so that" \
" it doesn't go outside of the figure, but if it's long enough it" \
" will go off the top or bottom!"
plt.text(4, 1, t, ha='left', rotation=15, wrap=True)
plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
plt.text(5, 5, t, ha='right', rotation=-15, wrap=True)
plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',
va='top', wrap=True)
plt.text(3, 4, t, family='serif', style='italic', ha='right', wrap=True)
plt.text(-1, 0, t, ha='left', rotation=-15, wrap=True)
@image_comparison(baseline_images=['fonttext_wrap'],
extensions=['png'])
def test_font_wrap():
fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = "This is a really long string that I'd rather have wrapped so that" \
" it doesn't go outside of the figure, but if it's long enough it" \
" will go off the top or bottom!"
plt.text(4, -1, t, fontsize=18, family='serif', ha='left', rotation=15,
wrap=True)
plt.text(6, 5, t, family='sans serif', ha='left', rotation=15, wrap=True)
plt.text(5, 5, t, weight='light', ha='right', rotation=-15, wrap=True)
plt.text(5, 10, t, weight='heavy', ha='center', va='top', wrap=True)
plt.text(3, 4, t, family='monospace', ha='right', wrap=True)
plt.text(-1, 0, t, fontsize=14, style='italic', ha='left', rotation=-15,
wrap=True)