Skip to content

Commit 2fa2cae

Browse files
committed
Merge pull request #130 from plotly/andrew-fix-pandas-date-formatter
Andrew fix pandas date formatter
2 parents ec636c0 + 8f41972 commit 2fa2cae

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

plotly/matplotlylib/mpltools.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
import math
99
import warnings
10-
import datetime
1110
import matplotlib.dates
11+
import pytz
12+
1213

1314
def check_bar_match(old_bar, new_bar):
1415
"""Check if two bars belong in the same collection (bar chart).
@@ -418,7 +419,7 @@ def prep_ticks(ax, index, ax_type, props):
418419
return dict()
419420
# get tick label formatting information
420421
formatter = axis.get_major_formatter().__class__.__name__
421-
if ax_type == 'x' and formatter == 'DateFormatter':
422+
if ax_type == 'x' and 'DateFormatter' in formatter:
422423
axis_dict['type'] = 'date'
423424
try:
424425
axis_dict['tick0'] = mpl_dates_to_datestrings(axis_dict['tick0'])
@@ -455,13 +456,15 @@ def prep_xy_axis(ax, props, x_bounds, y_bounds):
455456
yaxis.update(prep_ticks(ax, 1, 'y', props))
456457
return xaxis, yaxis
457458

459+
458460
def mpl_dates_to_datestrings(mpl_dates, format_string="%Y-%m-%d %H:%M:%S"):
461+
"""Convert matplotlib dates to formatted datestrings for plotly.
462+
463+
Info on mpl dates: http://matplotlib.org/api/dates_api.html
464+
465+
"""
459466
try:
460-
# make sure we have a list
461-
mpl_date_list = list(mpl_dates)
462-
epoch_times = matplotlib.dates.num2epoch(mpl_date_list)
463-
date_times = [datetime.datetime.utcfromtimestamp(epoch_time)
464-
for epoch_time in epoch_times]
467+
date_times = matplotlib.dates.num2date(mpl_dates, tz=pytz.utc)
465468
time_strings = [date_time.strftime(format_string)
466469
for date_time in date_times]
467470
if len(time_strings) > 1:
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)