1
1
from __future__ import print_function
2
2
import datetime
3
- import numpy as np
4
- from matplotlib .testing .decorators import image_comparison , knownfailureif , cleanup
3
+ from matplotlib . testing . decorators import image_comparison
4
+ from matplotlib .testing .decorators import knownfailureif , cleanup
5
5
import matplotlib .pyplot as plt
6
6
from nose .tools import assert_raises , assert_equal
7
7
import warnings
8
8
9
+
9
10
@image_comparison (baseline_images = ['date_empty' ])
10
11
def test_date_empty ():
11
12
# make sure mpl does the right thing when told to plot dates even
12
13
# if no date data has been presented, cf
13
14
# http://sourceforge.net/tracker/?func=detail&aid=2850075&group_id=80706&atid=560720
14
15
fig = plt .figure ()
15
- ax = fig .add_subplot (1 ,1 , 1 )
16
+ ax = fig .add_subplot (1 , 1 , 1 )
16
17
ax .xaxis_date ()
17
18
19
+
18
20
@image_comparison (baseline_images = ['date_axhspan' ])
19
21
def test_date_axhspan ():
20
22
# test ax hspan with date inputs
21
23
t0 = datetime .datetime (2009 , 1 , 20 )
22
24
tf = datetime .datetime (2009 , 1 , 21 )
23
25
fig = plt .figure ()
24
- ax = fig .add_subplot (1 ,1 , 1 )
25
- ax .axhspan ( t0 , tf , facecolor = "blue" , alpha = 0.25 )
26
- ax .set_ylim (t0 - datetime .timedelta (days = 5 ),
27
- tf + datetime .timedelta (days = 5 ))
26
+ ax = fig .add_subplot (1 , 1 , 1 )
27
+ ax .axhspan (t0 , tf , facecolor = "blue" , alpha = 0.25 )
28
+ ax .set_ylim (t0 - datetime .timedelta (days = 5 ),
29
+ tf + datetime .timedelta (days = 5 ))
28
30
fig .subplots_adjust (left = 0.25 )
29
31
32
+
30
33
@image_comparison (baseline_images = ['date_axvspan' ])
31
34
def test_date_axvspan ():
32
35
# test ax hspan with date inputs
33
36
t0 = datetime .datetime (2000 , 1 , 20 )
34
37
tf = datetime .datetime (2010 , 1 , 21 )
35
38
fig = plt .figure ()
36
- ax = fig .add_subplot (1 ,1 , 1 )
37
- ax .axvspan ( t0 , tf , facecolor = "blue" , alpha = 0.25 )
38
- ax .set_xlim (t0 - datetime .timedelta (days = 720 ),
39
- tf + datetime .timedelta (days = 720 ))
39
+ ax = fig .add_subplot (1 , 1 , 1 )
40
+ ax .axvspan (t0 , tf , facecolor = "blue" , alpha = 0.25 )
41
+ ax .set_xlim (t0 - datetime .timedelta (days = 720 ),
42
+ tf + datetime .timedelta (days = 720 ))
40
43
fig .autofmt_xdate ()
41
44
45
+
42
46
@image_comparison (baseline_images = ['date_axhline' ])
43
47
def test_date_axhline ():
44
48
# test ax hline with date inputs
45
49
t0 = datetime .datetime (2009 , 1 , 20 )
46
50
tf = datetime .datetime (2009 , 1 , 31 )
47
51
fig = plt .figure ()
48
- ax = fig .add_subplot (1 ,1 , 1 )
49
- ax .axhline ( t0 , color = "blue" , lw = 3 )
50
- ax .set_ylim (t0 - datetime .timedelta (days = 5 ),
51
- tf + datetime .timedelta (days = 5 ))
52
+ ax = fig .add_subplot (1 , 1 , 1 )
53
+ ax .axhline (t0 , color = "blue" , lw = 3 )
54
+ ax .set_ylim (t0 - datetime .timedelta (days = 5 ),
55
+ tf + datetime .timedelta (days = 5 ))
52
56
fig .subplots_adjust (left = 0.25 )
53
57
58
+
54
59
@image_comparison (baseline_images = ['date_axvline' ])
55
60
def test_date_axvline ():
56
61
# test ax hline with date inputs
57
62
t0 = datetime .datetime (2000 , 1 , 20 )
58
63
tf = datetime .datetime (2000 , 1 , 21 )
59
64
fig = plt .figure ()
60
- ax = fig .add_subplot (1 ,1 , 1 )
61
- ax .axvline ( t0 , color = "red" , lw = 3 )
62
- ax .set_xlim (t0 - datetime .timedelta (days = 5 ),
63
- tf + datetime .timedelta (days = 5 ))
65
+ ax = fig .add_subplot (1 , 1 , 1 )
66
+ ax .axvline (t0 , color = "red" , lw = 3 )
67
+ ax .set_xlim (t0 - datetime .timedelta (days = 5 ),
68
+ tf + datetime .timedelta (days = 5 ))
64
69
fig .autofmt_xdate ()
65
70
71
+
66
72
@cleanup
67
73
def test_too_many_date_ticks ():
68
74
# Attempt to test SF 2715172, see
69
75
# https://sourceforge.net/tracker/?func=detail&aid=2715172&group_id=80706&atid=560720
70
76
# setting equal datetimes triggers and expander call in
71
77
# transforms.nonsingular which results in too many ticks in the
72
78
# DayLocator. This should trigger a Locator.MAXTICKS RuntimeError
73
- warnings .filterwarnings ('ignore' ,
74
- 'Attempting to set identical left==right results\\ nin singular transformations; automatically expanding.\\ nleft=\d*\.\d*, right=\d*\.\d*' ,
79
+ warnings .filterwarnings (
80
+ 'ignore' ,
81
+ 'Attempting to set identical left==right results\\ nin singular '
82
+ 'transformations; automatically expanding.\\ nleft=\d*\.\d*, '
83
+ 'right=\d*\.\d*' ,
75
84
UserWarning , module = 'matplotlib.axes' )
76
85
t0 = datetime .datetime (2000 , 1 , 20 )
77
86
tf = datetime .datetime (2000 , 1 , 20 )
78
87
fig = plt .figure ()
79
- ax = fig .add_subplot (1 ,1 , 1 )
80
- ax .set_xlim ((t0 ,tf ), auto = True )
81
- ax .plot ([],[])
82
- from matplotlib .dates import DayLocator , DateFormatter , HourLocator
88
+ ax = fig .add_subplot (1 , 1 , 1 )
89
+ ax .set_xlim ((t0 , tf ), auto = True )
90
+ ax .plot ([], [])
91
+ from matplotlib .dates import DayLocator
83
92
ax .xaxis .set_major_locator (DayLocator ())
84
93
assert_raises (RuntimeError , fig .savefig , 'junk.png' )
85
94
95
+
86
96
@image_comparison (baseline_images = ['RRuleLocator_bounds' ])
87
97
def test_RRuleLocator ():
88
98
import pylab
@@ -95,22 +105,23 @@ def test_RRuleLocator():
95
105
# This will cause the RRuleLocator to go out of bounds when it tries
96
106
# to add padding to the limits, so we make sure it caps at the correct
97
107
# boundary values.
98
- t0 = datetime ( 1000 , 1 , 1 )
99
- tf = datetime ( 6000 , 1 , 1 )
108
+ t0 = datetime (1000 , 1 , 1 )
109
+ tf = datetime (6000 , 1 , 1 )
100
110
101
111
fig = pylab .figure ()
102
- ax = pylab .subplot ( 111 )
103
- ax .set_autoscale_on ( True )
104
- ax .plot ( [t0 , tf ], [0.0 , 1.0 ], marker = 'o' )
112
+ ax = pylab .subplot (111 )
113
+ ax .set_autoscale_on (True )
114
+ ax .plot ([t0 , tf ], [0.0 , 1.0 ], marker = 'o' )
105
115
106
- rrule = mpldates .rrulewrapper ( dateutil .rrule .YEARLY , interval = 500 )
107
- locator = mpldates .RRuleLocator ( rrule )
108
- ax .xaxis .set_major_locator ( locator )
109
- ax .xaxis .set_major_formatter ( mpldates .AutoDateFormatter (locator ) )
116
+ rrule = mpldates .rrulewrapper (dateutil .rrule .YEARLY , interval = 500 )
117
+ locator = mpldates .RRuleLocator (rrule )
118
+ ax .xaxis .set_major_locator (locator )
119
+ ax .xaxis .set_major_formatter (mpldates .AutoDateFormatter (locator ))
110
120
111
121
ax .autoscale_view ()
112
122
fig .autofmt_xdate ()
113
123
124
+
114
125
@image_comparison (baseline_images = ['DateFormatter_fractionalSeconds' ])
115
126
def test_DateFormatter ():
116
127
import pylab
@@ -121,13 +132,13 @@ def test_DateFormatter():
121
132
# Lets make sure that DateFormatter will allow us to have tick marks
122
133
# at intervals of fractional seconds.
123
134
124
- t0 = datetime ( 2001 , 1 , 1 , 0 , 0 , 0 )
125
- tf = datetime ( 2001 , 1 , 1 , 0 , 0 , 1 )
135
+ t0 = datetime (2001 , 1 , 1 , 0 , 0 , 0 )
136
+ tf = datetime (2001 , 1 , 1 , 0 , 0 , 1 )
126
137
127
138
fig = pylab .figure ()
128
- ax = pylab .subplot ( 111 )
129
- ax .set_autoscale_on ( True )
130
- ax .plot ( [t0 , tf ], [0.0 , 1.0 ], marker = 'o' )
139
+ ax = pylab .subplot (111 )
140
+ ax .set_autoscale_on (True )
141
+ ax .plot ([t0 , tf ], [0.0 , 1.0 ], marker = 'o' )
131
142
132
143
# rrule = mpldates.rrulewrapper( dateutil.rrule.YEARLY, interval=500 )
133
144
# locator = mpldates.RRuleLocator( rrule )
@@ -137,36 +148,42 @@ def test_DateFormatter():
137
148
ax .autoscale_view ()
138
149
fig .autofmt_xdate ()
139
150
151
+
140
152
def test_drange ():
141
- '''This test should check if drange works as expected, and if all the rounding errors
142
- are fixed'''
153
+ """
154
+ This test should check if drange works as expected, and if all the
155
+ rounding errors are fixed
156
+ """
143
157
from matplotlib import dates
144
- start = datetime .datetime (2011 , 1 ,1 , tzinfo = dates .UTC )
158
+ start = datetime .datetime (2011 , 1 , 1 , tzinfo = dates .UTC )
145
159
end = datetime .datetime (2011 , 1 , 2 , tzinfo = dates .UTC )
146
160
delta = datetime .timedelta (hours = 1 )
147
- #We expect 24 values in drange(start, end, delta), because drange returns dates from
148
- #an half open interval [start, end)
161
+ # We expect 24 values in drange(start, end, delta), because drange returns
162
+ # dates from an half open interval [start, end)
149
163
assert_equal (24 , len (dates .drange (start , end , delta )))
150
164
151
- #if end is a little bit later, we expect the range to contain one element more
152
- end = end + datetime .timedelta (microseconds = 1 )
165
+ # if end is a little bit later, we expect the range to contain one element
166
+ # more
167
+ end = end + datetime .timedelta (microseconds = 1 )
153
168
assert_equal (25 , len (dates .drange (start , end , delta )))
154
169
155
- #reset end
170
+ # reset end
156
171
end = datetime .datetime (2011 , 1 , 2 , tzinfo = dates .UTC )
157
172
158
- #and tst drange with "complicated" floats:
173
+ # and tst drange with "complicated" floats:
159
174
# 4 hours = 1/6 day, this is an "dangerous" float
160
175
delta = datetime .timedelta (hours = 4 )
161
176
daterange = dates .drange (start , end , delta )
162
177
assert_equal (6 , len (daterange ))
163
- assert_equal (dates .num2date (daterange [- 1 ]), end - delta )
178
+ assert_equal (dates .num2date (daterange [- 1 ]), end - delta )
179
+
164
180
165
181
#@image_comparison(baseline_images=['empty_date_bug'])
166
182
@cleanup
167
183
@knownfailureif (True )
168
184
def test_empty_date_with_year_formatter ():
169
- # exposes sf bug 2861426: https://sourceforge.net/tracker/?func=detail&aid=2861426&group_id=80706&atid=560720
185
+ # exposes sf bug 2861426:
186
+ # https://sourceforge.net/tracker/?func=detail&aid=2861426&group_id=80706&atid=560720
170
187
171
188
# update: I am no loner believe this is a bug, as I commented on
172
189
# the tracker. The question is now: what to do with this test
@@ -181,6 +198,7 @@ def test_empty_date_with_year_formatter():
181
198
182
199
fig .savefig ('empty_date_bug' )
183
200
184
- if __name__ == '__main__' :
201
+
202
+ if __name__ == '__main__' :
185
203
import nose
186
- nose .runmodule (argv = ['-s' ,'--with-doctest' ], exit = False )
204
+ nose .runmodule (argv = ['-s' , '--with-doctest' ], exit = False )
0 commit comments