Skip to content

Commit 8f41972

Browse files
committed
More stable mpl_date conversion, just needs a timezone. (see below).
pytz is a dependency of matplotlib, so this isn't adding bloat to our optional dependency list.
1 parent e095900 commit 8f41972

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

plotly/matplotlylib/mpltools.py

Lines changed: 9 additions & 6 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).
@@ -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:

0 commit comments

Comments
 (0)