@@ -386,7 +386,7 @@ def prep_ticks(ax, index, ax_type, props):
386386 for i in range (1 , len (dticks ) - 1 )]):
387387 dtick = tickvalues [1 ] - tickvalues [0 ]
388388 else :
389- warnings .warn ("'linear' {}-axis tick spacing not even, "
389+ warnings .warn ("'linear' {0 }-axis tick spacing not even, "
390390 "ignoring mpl tick formatting." .format (ax_type ))
391391 raise TypeError
392392 except (IndexError , TypeError ):
@@ -413,7 +413,7 @@ def prep_ticks(ax, index, ax_type, props):
413413 math .log10 (props ['ylim' ][1 ])]
414414 else :
415415 axis_dict = dict (range = None , type = 'linear' )
416- warnings .warn ("Converted non-base10 {}-axis log scale to 'linear'"
416+ warnings .warn ("Converted non-base10 {0 }-axis log scale to 'linear'"
417417 "" .format (ax_type ))
418418 else :
419419 return dict ()
@@ -422,13 +422,17 @@ def prep_ticks(ax, index, ax_type, props):
422422 if ax_type == 'x' and 'DateFormatter' in formatter :
423423 axis_dict ['type' ] = 'date'
424424 try :
425- axis_dict ['tick0' ] = mpl_dates_to_datestrings (axis_dict ['tick0' ])
425+ axis_dict ['tick0' ] = mpl_dates_to_datestrings (
426+ axis_dict ['tick0' ], formatter
427+ )
426428 except KeyError :
427429 pass
428430 finally :
429431 axis_dict .pop ('dtick' , None )
430432 axis_dict .pop ('autotick' , None )
431- axis_dict ['range' ] = mpl_dates_to_datestrings (props ['xlim' ])
433+ axis_dict ['range' ] = mpl_dates_to_datestrings (
434+ props ['xlim' ], formatter
435+ )
432436
433437 if formatter == 'LogFormatterMathtext' :
434438 axis_dict ['exponentformat' ] = 'e'
@@ -457,22 +461,39 @@ def prep_xy_axis(ax, props, x_bounds, y_bounds):
457461 return xaxis , yaxis
458462
459463
460- def mpl_dates_to_datestrings (mpl_dates , format_string = "%Y-%m-%d %H:%M:%S" ):
461- """Convert matplotlib dates to formatted datestrings for plotly.
464+ def mpl_dates_to_datestrings (dates , mpl_formatter ):
465+ """Convert matplotlib dates to iso-formatted-like time strings.
466+
467+ Plotly's accepted format: "YYYY-MM-DD HH:MM:SS" (e.g., 2001-01-01 00:00:00)
462468
463469 Info on mpl dates: http://matplotlib.org/api/dates_api.html
464470
465471 """
466- try :
467- date_times = matplotlib .dates .num2date (mpl_dates , tz = pytz .utc )
468- time_strings = [date_time .strftime (format_string )
469- for date_time in date_times ]
470- if len (time_strings ) > 1 :
471- return time_strings
472- else :
473- return time_strings [0 ]
474- except TypeError :
475- return mpl_dates
472+ _dates = dates
473+
474+ # this is a pandas datetime formatter, times show up in floating point days
475+ # since the epoch (1970-01-01T00:00:00+00:00)
476+ if mpl_formatter == "TimeSeries_DateFormatter" :
477+ try :
478+ dates = matplotlib .dates .epoch2num (
479+ [date * 24 * 60 * 60 for date in dates ]
480+ )
481+ dates = matplotlib .dates .num2date (dates , tz = pytz .utc )
482+ except :
483+ return _dates
484+
485+ # the rest of mpl dates are in floating point days since
486+ # (0001-01-01T00:00:00+00:00) + 1. I.e., (0001-01-01T00:00:00+00:00) == 1.0
487+ # according to mpl --> try num2date(1)
488+ else :
489+ try :
490+ dates = matplotlib .dates .num2date (dates , tz = pytz .utc )
491+ except :
492+ return _dates
493+
494+ time_stings = [' ' .join (date .isoformat ().split ('+' )[0 ].split ('T' ))
495+ for date in dates ]
496+ return time_stings
476497
477498
478499DASH_MAP = {
0 commit comments