Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e116ff2

Browse files
committed
Add Figure.to_dataframe method.
1 parent c8cb566 commit e116ff2

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

plotly/graph_objs/graph_objs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,16 @@ def get_data(self, flatten=False):
10141014
return data['data']
10151015
Figure.get_data = get_data
10161016

1017+
def to_dataframe(self):
1018+
data = self.get_data(flatten=True)
1019+
try:
1020+
import pandas as pd
1021+
return pd.DataFrame(dict([(k, pd.Series(v))
1022+
for k, v in data.items()]))
1023+
except ImportError:
1024+
return data
1025+
Figure.to_dataframe = to_dataframe
1026+
10171027
def append_trace(self, trace, row, col):
10181028
""" Helper function to add a data traces to your figure
10191029
that is bound to axes at the row, col index.

plotly/tests/test_optional/test_graph_objs/__init__.py

Whitespace-only changes.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from __future__ import absolute_import
2+
3+
from unittest import TestCase
4+
5+
from plotly.graph_objs import *
6+
7+
8+
class TestToDataframe(TestCase):
9+
10+
fig = None
11+
12+
def setUp(self):
13+
super(TestToDataframe, self).setUp()
14+
self.fig = Figure(
15+
data=Data([
16+
Scatter(
17+
x=[52698, 43117],
18+
y=[53, 31],
19+
mode='markers',
20+
name='North America',
21+
text=['United States', 'Canada'],
22+
marker=Marker(
23+
color='rgb(164, 194, 244)',
24+
size=12,
25+
line=Line(
26+
color='white',
27+
width=0.5
28+
)
29+
)
30+
),
31+
Scatter(
32+
x=[39317, 37236, 35650, 30066, 29570, 27159, 23557, 21046,
33+
18007],
34+
y=[33, 20, 13, 19, 27, 19, 49, 44, 38],
35+
mode='markers',
36+
name='Europe',
37+
text=['Germany', 'Britain', 'France', 'Spain', 'Italy',
38+
'Czech Rep.', 'Greece', 'Poland'],
39+
marker=Marker(
40+
color='rgb(255, 217, 102)',
41+
size=12,
42+
line=Line(
43+
color='white',
44+
width=0.5
45+
)
46+
)
47+
),
48+
Scatter(
49+
x=[42952, 37037, 33106, 17478, 9813, 5253, 4692, 3899],
50+
y=[23, 42, 54, 89, 14, 99, 93, 70],
51+
mode='markers',
52+
name='Asia/Pacific',
53+
text=['Australia', 'Japan', 'South Korea', 'Malaysia',
54+
'China', 'Indonesia', 'Philippines', 'India'],
55+
marker=Marker(
56+
color='rgb(234, 153, 153)',
57+
size=12,
58+
line=Line(
59+
color='white',
60+
width=0.5
61+
)
62+
)
63+
),
64+
Scatter(
65+
x=[19097, 18601, 15595, 13546, 12026, 7434, 5419],
66+
y=[43, 47, 56, 80, 86, 93, 80],
67+
mode='markers',
68+
name='Latin America',
69+
text=['Chile', 'Argentina', 'Mexico', 'Venezuela',
70+
'Venezuela', 'El Salvador', 'Bolivia'],
71+
marker=Marker(
72+
color='rgb(142, 124, 195)',
73+
size=12,
74+
line=Line(
75+
color='white',
76+
width=0.5
77+
)
78+
)
79+
)
80+
]),
81+
layout=Layout(
82+
title='Quarter 1 Growth',
83+
autosize=False,
84+
width=500,
85+
height=500,
86+
xaxis=XAxis(
87+
title='GDP per Capita',
88+
showgrid=False,
89+
zeroline=False
90+
),
91+
yaxis=YAxis(
92+
title='Percent',
93+
showline=False
94+
),
95+
margin=Margin(
96+
l=65,
97+
r=50,
98+
b=65,
99+
t=90
100+
)
101+
)
102+
)
103+
104+
def test_figure_to_dataframe(self):
105+
df = self.fig.to_dataframe()
106+
self.assertEqual(len(df), 9)
107+
self.assertEqual(len(df.columns), 12)

0 commit comments

Comments
 (0)