2
2
unicode_literals )
3
3
4
4
import six
5
+ import warnings
5
6
6
7
import numpy as np
8
+ from numpy .testing import assert_almost_equal
9
+ from nose import SkipTest
10
+ from nose .tools import assert_raises , eq_
11
+
7
12
import matplotlib
8
- from matplotlib .testing .decorators import image_comparison , knownfailureif , cleanup
9
13
import matplotlib .pyplot as plt
10
- import warnings
11
- from nose import SkipTest
12
- from nose .tools import with_setup , assert_raises , eq_ , ok_
14
+ from matplotlib .testing .decorators import image_comparison , cleanup
15
+ from matplotlib .figure import Figure
16
+ from matplotlib .text import Annotation , Text
17
+ from matplotlib .backends .backend_agg import RendererAgg
13
18
14
19
15
20
@image_comparison (baseline_images = ['font_styles' ])
@@ -23,79 +28,98 @@ def find_matplotlib_font(**kw):
23
28
return FontProperties (fname = path )
24
29
25
30
from matplotlib .font_manager import FontProperties , findfont
26
- warnings .filterwarnings ('ignore' , ('findfont: Font family \[u?\' Foo\' \] ' +
27
- 'not found. Falling back to .' ),
28
- UserWarning ,
29
- module = 'matplotlib.font_manager' )
30
- fig = plt .figure ()
31
+ warnings .filterwarnings (
32
+ 'ignore' ,
33
+ ('findfont: Font family \[u?\' Foo\' \] not found. Falling back to .' ),
34
+ UserWarning ,
35
+ module = 'matplotlib.font_manager' )
36
+
37
+ plt .figure ()
31
38
ax = plt .subplot (1 , 1 , 1 )
32
39
33
- normalFont = find_matplotlib_font (family = "sans-serif" ,
34
- style = "normal" ,
35
- variant = "normal" ,
36
- size = 14 ,
37
- )
38
- ax .annotate ("Normal Font" , (0.1 , 0.1 ), xycoords = 'axes fraction' ,
39
- fontproperties = normalFont )
40
-
41
- boldFont = find_matplotlib_font (family = "Foo" ,
42
- style = "normal" ,
43
- variant = "normal" ,
44
- weight = "bold" ,
45
- stretch = 500 ,
46
- size = 14 ,
47
- )
48
- ax .annotate ("Bold Font" , (0.1 , 0.2 ), xycoords = 'axes fraction' ,
49
- fontproperties = boldFont )
50
-
51
- boldItemFont = find_matplotlib_font (family = "sans serif" ,
52
- style = "italic" ,
53
- variant = "normal" ,
54
- weight = 750 ,
55
- stretch = 500 ,
56
- size = 14 ,
57
- )
58
- ax .annotate ("Bold Italic Font" , (0.1 , 0.3 ), xycoords = 'axes fraction' ,
59
- fontproperties = boldItemFont )
60
-
61
- lightFont = find_matplotlib_font (family = "sans-serif" ,
62
- style = "normal" ,
63
- variant = "normal" ,
64
- weight = 200 ,
65
- stretch = 500 ,
66
- size = 14 ,
67
- )
68
- ax .annotate ("Light Font" , (0.1 , 0.4 ), xycoords = 'axes fraction' ,
69
- fontproperties = lightFont )
70
-
71
- condensedFont = find_matplotlib_font (family = "sans-serif" ,
72
- style = "normal" ,
73
- variant = "normal" ,
74
- weight = 500 ,
75
- stretch = 100 ,
76
- size = 14 ,
77
- )
78
- ax .annotate ("Condensed Font" , (0.1 , 0.5 ), xycoords = 'axes fraction' ,
79
- fontproperties = condensedFont )
40
+ normalFont = find_matplotlib_font (
41
+ family = "sans-serif" ,
42
+ style = "normal" ,
43
+ variant = "normal" ,
44
+ size = 14 )
45
+ ax .annotate (
46
+ "Normal Font" ,
47
+ (0.1 , 0.1 ),
48
+ xycoords = 'axes fraction' ,
49
+ fontproperties = normalFont )
50
+
51
+ boldFont = find_matplotlib_font (
52
+ family = "Foo" ,
53
+ style = "normal" ,
54
+ variant = "normal" ,
55
+ weight = "bold" ,
56
+ stretch = 500 ,
57
+ size = 14 )
58
+ ax .annotate (
59
+ "Bold Font" ,
60
+ (0.1 , 0.2 ),
61
+ xycoords = 'axes fraction' ,
62
+ fontproperties = boldFont )
63
+
64
+ boldItemFont = find_matplotlib_font (
65
+ family = "sans serif" ,
66
+ style = "italic" ,
67
+ variant = "normal" ,
68
+ weight = 750 ,
69
+ stretch = 500 ,
70
+ size = 14 )
71
+ ax .annotate (
72
+ "Bold Italic Font" ,
73
+ (0.1 , 0.3 ),
74
+ xycoords = 'axes fraction' ,
75
+ fontproperties = boldItemFont )
76
+
77
+ lightFont = find_matplotlib_font (
78
+ family = "sans-serif" ,
79
+ style = "normal" ,
80
+ variant = "normal" ,
81
+ weight = 200 ,
82
+ stretch = 500 ,
83
+ size = 14 )
84
+ ax .annotate (
85
+ "Light Font" ,
86
+ (0.1 , 0.4 ),
87
+ xycoords = 'axes fraction' ,
88
+ fontproperties = lightFont )
89
+
90
+ condensedFont = find_matplotlib_font (
91
+ family = "sans-serif" ,
92
+ style = "normal" ,
93
+ variant = "normal" ,
94
+ weight = 500 ,
95
+ stretch = 100 ,
96
+ size = 14 )
97
+ ax .annotate (
98
+ "Condensed Font" ,
99
+ (0.1 , 0.5 ),
100
+ xycoords = 'axes fraction' ,
101
+ fontproperties = condensedFont )
80
102
81
103
ax .set_xticks ([])
82
104
ax .set_yticks ([])
83
105
84
106
85
107
@image_comparison (baseline_images = ['multiline' ])
86
108
def test_multiline ():
87
- fig = plt .figure ()
109
+ plt .figure ()
88
110
ax = plt .subplot (1 , 1 , 1 )
89
111
ax .set_title ("multiline\n text alignment" )
90
112
91
- plt .text (0.2 , 0.5 , "TpTpTp \n $M$ \n TpTpTp" , size = 20 ,
92
- ha = "center" , va = "top" )
113
+ plt .text (
114
+ 0.2 , 0.5 , "TpTpTp \n $M$ \n TpTpTp" , size = 20 , ha = "center" , va = "top" )
93
115
94
- plt .text (0.5 , 0.5 , "TpTpTp\n $M^{M^{M^{M}}}$\n TpTpTp" , size = 20 ,
95
- ha = "center" , va = "top" )
116
+ plt .text (
117
+ 0.5 , 0.5 , "TpTpTp\n $M^{M^{M^{M}}}$\n TpTpTp" , size = 20 ,
118
+ ha = "center" , va = "top" )
96
119
97
- plt .text (0.8 , 0.5 , "TpTpTp\n $M_{q_{q_{q}}}$\n TpTpTp" , size = 20 ,
98
- ha = "center" , va = "top" )
120
+ plt .text (
121
+ 0.8 , 0.5 , "TpTpTp\n $M_{q_{q_{q}}}$\n TpTpTp" , size = 20 ,
122
+ ha = "center" , va = "top" )
99
123
100
124
plt .xlim (0 , 1 )
101
125
plt .ylim (0 , 0.8 )
@@ -136,15 +160,15 @@ def test_contains():
136
160
fig = plt .figure ()
137
161
ax = plt .axes ()
138
162
139
- mevent = mbackend .MouseEvent ('button_press_event' , fig . canvas , 0.5 ,
140
- 0.5 , 1 , None )
163
+ mevent = mbackend .MouseEvent (
164
+ 'button_press_event' , fig . canvas , 0.5 , 0.5 , 1 , None )
141
165
142
166
xs = np .linspace (0.25 , 0.75 , 30 )
143
167
ys = np .linspace (0.25 , 0.75 , 30 )
144
168
xs , ys = np .meshgrid (xs , ys )
145
169
146
- txt = plt .text (0.48 , 0.52 , 'hello world' , ha = 'center' , fontsize = 30 ,
147
- rotation = 30 )
170
+ txt = plt .text (
171
+ 0.48 , 0.52 , 'hello world' , ha = 'center' , fontsize = 30 , rotation = 30 )
148
172
# uncomment to draw the text's bounding box
149
173
# txt.set_bbox(dict(edgecolor='black', facecolor='none'))
150
174
@@ -154,9 +178,7 @@ def test_contains():
154
178
155
179
for x , y in zip (xs .flat , ys .flat ):
156
180
mevent .x , mevent .y = plt .gca ().transAxes .transform_point ([x , y ])
157
-
158
181
contains , _ = txt .contains (mevent )
159
-
160
182
color = 'yellow' if contains else 'red'
161
183
162
184
# capture the viewLim, plot a point, and reset the viewLim
@@ -168,7 +190,7 @@ def test_contains():
168
190
@image_comparison (baseline_images = ['titles' ])
169
191
def test_titles ():
170
192
# left and right side titles
171
- fig = plt .figure ()
193
+ plt .figure ()
172
194
ax = plt .subplot (1 , 1 , 1 )
173
195
ax .set_title ("left title" , loc = "left" )
174
196
ax .set_title ("right title" , loc = "right" )
@@ -178,15 +200,17 @@ def test_titles():
178
200
179
201
@image_comparison (baseline_images = ['text_alignment' ])
180
202
def test_alignment ():
181
- fig = plt .figure ()
203
+ plt .figure ()
182
204
ax = plt .subplot (1 , 1 , 1 )
183
205
184
206
x = 0.1
185
207
for rotation in (0 , 30 ):
186
208
for alignment in ('top' , 'bottom' , 'baseline' , 'center' ):
187
- ax .text (x , 0.5 , alignment + " Tj" , va = alignment , rotation = rotation ,
188
- bbox = dict (boxstyle = 'round' , facecolor = 'wheat' , alpha = 0.5 ))
189
- ax .text (x , 1.0 , r'$\sum_{i=0}^{j}$' , va = alignment , rotation = rotation )
209
+ ax .text (
210
+ x , 0.5 , alignment + " Tj" , va = alignment , rotation = rotation ,
211
+ bbox = dict (boxstyle = 'round' , facecolor = 'wheat' , alpha = 0.5 ))
212
+ ax .text (
213
+ x , 1.0 , r'$\sum_{i=0}^{j}$' , va = alignment , rotation = rotation )
190
214
x += 0.1
191
215
192
216
ax .plot ([0 , 1 ], [0.5 , 0.5 ])
@@ -201,8 +225,8 @@ def test_alignment():
201
225
@image_comparison (baseline_images = ['axes_titles' ], extensions = ['png' ])
202
226
def test_axes_titles ():
203
227
# Related to issue #3327
204
- fig = plt .figure ()
205
- ax = plt .subplot (1 ,1 , 1 )
228
+ plt .figure ()
229
+ ax = plt .subplot (1 , 1 , 1 )
206
230
ax .set_title ('center' , loc = 'center' , fontsize = 20 , fontweight = 700 )
207
231
ax .set_title ('left' , loc = 'left' , fontsize = 12 , fontweight = 400 )
208
232
ax .set_title ('right' , loc = 'right' , fontsize = 12 , fontweight = 400 )
@@ -213,7 +237,8 @@ def test_set_position():
213
237
fig , ax = plt .subplots ()
214
238
215
239
# test set_position
216
- ann = ax .annotate ('test' , (0 , 0 ), xytext = (0 , 0 ), textcoords = 'figure pixels' )
240
+ ann = ax .annotate (
241
+ 'test' , (0 , 0 ), xytext = (0 , 0 ), textcoords = 'figure pixels' )
217
242
plt .draw ()
218
243
219
244
init_pos = ann .get_window_extent (fig .canvas .renderer )
@@ -226,7 +251,8 @@ def test_set_position():
226
251
assert a + shift_val == b
227
252
228
253
# test xyann
229
- ann = ax .annotate ('test' , (0 , 0 ), xytext = (0 , 0 ), textcoords = 'figure pixels' )
254
+ ann = ax .annotate (
255
+ 'test' , (0 , 0 ), xytext = (0 , 0 ), textcoords = 'figure pixels' )
230
256
plt .draw ()
231
257
232
258
init_pos = ann .get_window_extent (fig .canvas .renderer )
@@ -264,14 +290,9 @@ def test_annotation_negative_coords():
264
290
265
291
266
292
def test_text_annotation_get_window_extent ():
267
- from matplotlib .figure import Figure
268
- from matplotlib .text import Annotation , Text
269
- from matplotlib .backends .backend_agg import RendererAgg
270
-
271
293
figure = Figure (dpi = 100 )
272
294
renderer = RendererAgg (200 , 200 , 100 )
273
295
274
-
275
296
# Only text annotation
276
297
annotation = Annotation ('test' , xy = (0 , 0 ))
277
298
annotation .set_figure (figure )
@@ -300,10 +321,6 @@ def test_text_annotation_get_window_extent():
300
321
301
322
302
323
def test_text_with_arrow_annotation_get_window_extent ():
303
- from matplotlib .figure import Figure
304
- from matplotlib .text import Annotation , Text
305
- from matplotlib .backends .backend_agg import RendererAgg
306
-
307
324
figure = Figure (dpi = 100 )
308
325
renderer = RendererAgg (600 , 600 , 100 )
309
326
headwidth = 21
@@ -330,10 +347,6 @@ def test_text_with_arrow_annotation_get_window_extent():
330
347
331
348
332
349
def test_arrow_annotation_get_window_extent ():
333
- from matplotlib .figure import Figure
334
- from matplotlib .text import Annotation
335
- from matplotlib .backends .backend_agg import RendererAgg
336
-
337
350
figure = Figure (dpi = 100 )
338
351
figure .set_figwidth (2.0 )
339
352
figure .set_figheight (2.0 )
@@ -357,10 +370,6 @@ def test_arrow_annotation_get_window_extent():
357
370
358
371
359
372
def test_empty_annotation_get_window_extent ():
360
- from matplotlib .figure import Figure
361
- from matplotlib .text import Annotation
362
- from matplotlib .backends .backend_agg import RendererAgg
363
-
364
373
figure = Figure (dpi = 100 )
365
374
figure .set_figwidth (2.0 )
366
375
figure .set_figheight (2.0 )
0 commit comments