Skip to content

Commit 12bc928

Browse files
committed
Add test for dates in mpl.
1 parent 1f1eb15 commit 12bc928

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import absolute_import
2+
import matplotlib
3+
# Force matplotlib to not use any Xwindows backend.
4+
matplotlib.use('Agg')
5+
import matplotlib.pyplot as plt
6+
import datetime
7+
from matplotlib.dates import date2num
8+
import plotly.tools as tls
9+
from unittest import TestCase
10+
11+
from plotly.tests.test_optional.optional_utils import compare_dict, run_fig
12+
13+
14+
class TestDateTimes(TestCase):
15+
def test_normal_mpl_dates(self):
16+
datetime_format = '%Y-%m-%d %H:%M:%S'
17+
y = [1, 2, 3, 4]
18+
date_strings = ['2010-01-04 00:00:00',
19+
'2010-01-04 10:00:00',
20+
'2010-01-04 23:00:59',
21+
'2010-01-05 00:00:00']
22+
23+
# 1. create datetimes from the strings
24+
dates = [datetime.datetime.strptime(date_string, datetime_format)
25+
for date_string in date_strings]
26+
27+
# 2. create the mpl_dates from these datetimes
28+
mpl_dates = date2num(dates)
29+
30+
# make a figure in mpl
31+
fig, ax = plt.subplots()
32+
ax.plot_date(mpl_dates, y)
33+
34+
# convert this figure to plotly's graph_objs
35+
pfig = tls.mpl_to_plotly(fig)
36+
37+
print date_strings
38+
print pfig['data'][0]['x']
39+
# we use the same format here, so we expect equality here
40+
self.assertEqual(pfig['data'][0]['x'], date_strings)

0 commit comments

Comments
 (0)