Skip to content

Commit 43b1496

Browse files
committed
Merge branch 'master' of github.com:plotly/python-api into fix-pandas-serialization
2 parents e477f07 + b9ebee5 commit 43b1496

File tree

21 files changed

+189
-89
lines changed

21 files changed

+189
-89
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ License: MIT
107107
.. _Plotly: https://plot.ly
108108
.. _Quickstart: https://plot.ly/python
109109
.. _GitHub repo: https://github.com/plotly/python-api
110-
.. _Plotly and mpld3: http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s6_matplotlylib/s6_matplotlylib.ipynb
111-
.. _Plotly and Python: http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s0_getting-started/s0_getting-started.ipynb
112-
.. _set of notebooks: http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s00_homepage/s00_homepage.ipynb
110+
.. _Plotly and mpld3: https://plot.ly/python/matplotlib-to-plotly-tutorial/
111+
.. _Plotly and Python: https://plot.ly/python/overview/
112+
.. _set of notebooks: https://plot.ly/python/user-guide/
113113
.. _plotly profile: https://plot.ly/~mpld3/
114114
.. _@plotlygraphs: https://twitter.com/plotlygraphs
115115

optional-requirements.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
### ###
66
###################################################################
77

8+
## numpy (technically, this is covered by matplotlib's deps) ##
9+
numpy
10+
811
## matplotlylib dependencies ##
912
matplotlib==1.3.1
1013

1114
## testing dependencies ##
1215
nose==1.3.3
1316

1417
## ipython dependencies ##
15-
ipython[all]
18+
ipython[all]
19+
20+
## pandas deps for some matplotlib functionality ##
21+
pandas

plotly/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
"""
1818
import json
19+
import six
1920

2021
## Base Plotly Error ##
2122

@@ -36,7 +37,9 @@ def __init__(self, requests_exception):
3637
if 'json' in content_type:
3738
content = requests_exception.response.content
3839
if content != '':
39-
res_payload = json.loads(requests_exception.response.content)
40+
res_payload = json.loads(
41+
requests_exception.response.content.decode('utf8')
42+
)
4043
if 'detail' in res_payload:
4144
self.message = res_payload['detail']
4245
else:

plotly/graph_objs/graph_objs.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def to_string(self, level=0, indent=4, eol='\n',
215215
216216
Example:
217217
218-
print obj.to_string()
218+
print(obj.to_string())
219219
220220
Keyword arguments:
221221
level (default = 0) -- set number of indentations to start with
@@ -323,14 +323,14 @@ def __setitem__(self, key, value):
323323
return super(PlotlyDict, self).__setitem__(key, value)
324324

325325
def _assign_id_to_src(self, src_name, src_value):
326-
if isinstance(src_value, basestring):
326+
if isinstance(src_value, six.string_types):
327327
src_id = src_value
328328
else:
329329
try:
330330
src_id = src_value.id
331331
except:
332-
err = ("{} does not have an `id` property. "
333-
"{} needs to be assigned to either an "
332+
err = ("{0} does not have an `id` property. "
333+
"{1} needs to be assigned to either an "
334334
"object with an `id` (like a "
335335
"plotly.grid_objs.Column) or a string. "
336336
"The `id` is a unique identifier "
@@ -430,7 +430,7 @@ def strip_style(self):
430430
elif not hasattr(self[key], '__iter__'):
431431
del self[key]
432432
except KeyError: # TODO: Update the JSON
433-
# print "'type' not in {0} for {1}".format(obj_key, key)
433+
# print("'type' not in {0} for {1}".format(obj_key, key))
434434
pass
435435

436436
def get_data(self):
@@ -605,7 +605,7 @@ def to_string(self, level=0, indent=4, eol='\n',
605605
606606
Example:
607607
608-
print obj.to_string()
608+
print(obj.to_string())
609609
610610
Keyword arguments:
611611
level (default = 0) -- set number of indentations to start with
@@ -759,7 +759,7 @@ def to_string(self, level=0, indent=4, eol='\n',
759759
760760
Example:
761761
762-
print obj.to_string()
762+
print(obj.to_string())
763763
764764
Keyword arguments:
765765
level (default = 0) -- set number of indentations to start with
@@ -962,7 +962,7 @@ def to_string(self, level=0, indent=4, eol='\n',
962962
963963
Example:
964964
965-
print obj.to_string()
965+
print(obj.to_string())
966966
967967
Keyword arguments:
968968
level (default = 0) -- set number of indentations to start with

plotly/matplotlylib/mpltools.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

478499
DASH_MAP = {

plotly/matplotlylib/renderer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ def draw_bar(self, coll):
258258
[bar['x1'] for bar in trace])
259259
if self.x_is_mpl_date:
260260
x = [bar['x0'] for bar in trace]
261-
x = mpltools.mpl_dates_to_datestrings(x)
261+
formatter = (self.current_mpl_ax.get_xaxis()
262+
.get_major_formatter().__class__.__name__)
263+
x = mpltools.mpl_dates_to_datestrings(x, formatter)
262264
else:
263265
self.msg += " Attempting to draw a horizontal bar chart\n"
264266
old_rights = [bar_props['x1'] for bar_props in trace]
@@ -367,8 +369,10 @@ def draw_marked_line(self, **props):
367369
line=line,
368370
marker=marker)
369371
if self.x_is_mpl_date:
372+
formatter = (self.current_mpl_ax.get_xaxis()
373+
.get_major_formatter().__class__.__name__)
370374
marked_line['x'] = mpltools.mpl_dates_to_datestrings(
371-
marked_line['x']
375+
marked_line['x'], formatter
372376
)
373377
self.plotly_fig['data'] += marked_line,
374378
self.msg += " Heck yeah, I drew that line\n"

0 commit comments

Comments
 (0)