Reputation: 23
I am working to plot some data in a Bubble Chart using plotly. I have written this code: (based on Plotly's examples)
trace0 = go.Scatter(
x=output['mean'],
y=output['count'],
text=output.index,
mode='markers',
marker=dict(
color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)', 'rgb(44, 160, 101)', 'rgb(255, 65, 54)'],
size=output['total'],
)
)
data = [trace0]
plot_url = py.plot_mpl(trace0, filename='mpl-7d-bubble')
I am trying to plot output['mean']
on the x axis and output['count']
on the y. I would like to plot the size on output['total'].
When it is run, this error is received:
Traceback (most recent call last):
File "..data.py", line 45, in <module>
plot_url = py.plot_mpl(data, filename='mpl-7d-bubble')
File "...anaconda/lib/python3.5/site-packages/plotly/plotly/plotly.py", line 307, in plot_mpl
fig = tools.mpl_to_plotly(fig, resize=resize, strip_style=strip_style)
File ".../anaconda/lib/python3.5/site-packages/plotly/tools.py", line 507, in mpl_to_plotly
matplotlylib.Exporter(renderer).run(fig)
File ".../anaconda/lib/python3.5/site-packages/plotly/matplotlylib/mplexporter/exporter.py", line 45, in run
fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi)
AttributeError: 'list' object has no attribute 'savefig'
EDIT: Contents of output
pandas dataframe:
count location mean total
StartLocation
? 6 ? 9.527778 57.166667
bathroom 14 bathroom 20.409524 285.733333
bedroom 61 bedroom 96.837432 5907.083333
bedroom2 22 bedroom2 165.262121 3635.766667
hallway 6 hallway 0.394444 2.366667
kitchen 79 kitchen 8.646624 683.083333
living room 56 living room 93.855655 5255.916667
outside 6 outside 325.991667 1955.950000
setup 1 setup 0.050000 0.050000
study 18 study 18.099074 325.783333
toilet 51 toilet 7.198693 367.133333
Upvotes: 2
Views: 2032
Reputation: 31709
There a couple of small issues preventing you from getting the plot.
plotly expects data
to be a plotly.graph_objs.Data
object
data = go.Data([trace0])
Your size markers are too big, you need to minimize them to something reasonable
size=[i / 100 for i in output['total']]
The complete code for the image above.
import pandas as pd
import plotly.plotly as py
import plotly.graph_objs as go
import io
txt = """room count location mean total
? 6 ? 9.527778 57.166667
bathroom 14 bathroom 20.409524 285.733333
bedroom 61 bedroom 96.837432 5907.083333
bedroom2 22 bedroom2 165.262121 3635.766667
hallway 6 hallway 0.394444 2.366667
kitchen 79 kitchen 8.646624 683.083333
livingroom 56 livingroom 93.855655 5255.916667
outside 6 outside 325.991667 1955.950000
setup 1 setup 0.050000 0.050000
study 18 study 18.099074 325.783333
toilet 51 toilet 7.198693 367.133333"""
t = io.StringIO(txt)
output = pd.read_csv(t, sep=' ')
trace0 = go.Scatter(
x=output['mean'],
y=output['count'],
text=output.room,
mode='markers',
marker=dict(
color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)', 'rgb(44, 160, 101)', 'rgb(255, 65, 54)'],
size=[i / 100 for i in output['total']],
)
)
data = go.Data([trace0])
plot_url = py.plot(data, filename='mpl-7d-bubble')
Upvotes: 0