Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用plotly绘制气泡图_Python_Data Visualization_Plotly - Fatal编程技术网

Python 使用plotly绘制气泡图

Python 使用plotly绘制气泡图,python,data-visualization,plotly,Python,Data Visualization,Plotly,我正在使用plotly在气泡图中绘制一些数据。我已经编写了以下代码:(基于) 我试图在x轴上绘制output['mean'],在y轴上绘制output['count']。我想在输出['total']上绘制大小。运行时,会收到以下错误: Traceback (most recent call last): File "..data.py", line 45, in <module> plot_url = py.plot_mpl(data, filename='mpl-7d-

我正在使用plotly在气泡图中绘制一些数据。我已经编写了以下代码:(基于)

我试图在x轴上绘制
output['mean']
,在y轴上绘制
output['count']
。我想在
输出['total']上绘制大小。
运行时,会收到以下错误:

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'

有两个小问题阻碍了你理解情节

  • plotly希望
    data
    成为
    plotly.graph\u objs.data
    对象

    data = go.Data([trace0])
    
  • 您的尺寸标记太大,您需要将其最小化到合理的程度

    size=[i / 100 for i in output['total']]
    
上面图像的完整代码

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')

请打印output@eyllanesc对不起,打印什么输出?您在output['mean']@eyllanesc中使用的输出变量已打印。输出变量是dataframe吗?
size=[i / 100 for i in output['total']]
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')