def mpl_dates_to_datestrings(dates, mpl_formatter):
"""Convert matplotlib dates to iso-formatted-like time strings.
Plotly's accepted format: "YYYY-MM-DD HH:MM:SS" (e.g., 2001-01-01 00:00:00)
Info on mpl dates: http://matplotlib.org/api/dates_api.html
"""
_dates = dates
# this is a pandas datetime formatter, times show up in floating point days
# since the epoch (1970-01-01T00:00:00+00:00)
if mpl_formatter == "TimeSeries_DateFormatter":
try:
dates = matplotlib.dates.epoch2num(
[date*24*60*60 for date in dates]
)
dates = matplotlib.dates.num2date(dates, tz=pytz.utc)
except:
return _dates
# the rest of mpl dates are in floating point days since
# (0001-01-01T00:00:00+00:00) + 1. I.e., (0001-01-01T00:00:00+00:00) == 1.0
# according to mpl --> try num2date(1)
else:
try:
dates = matplotlib.dates.num2date(dates, tz=pytz.utc)
except:
return _dates
time_stings = [' '.join(date.isoformat().split('+')[0].split('T'))
for date in dates]
return time_stings
评论列表
文章目录