使用plotly绘制气泡图

2024-05-31 23:35:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用plotly在气泡图中绘制一些数据。我写了以下代码:(基于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')

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

^{pr2}$

编辑:熊猫数据帧的内容:

             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

Tags: 数据outputplotcount绘制rgbmeanmpl
1条回答
网友
1楼 · 发布于 2024-05-31 23:35:22

有几个小问题让你无法理解剧情。

  • plotly期望dataplotly.graph_objs.Data对象

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

    size=[i / 100 for i in output['total']]
    

Plotly plot

上面图片的完整代码。

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

相关问题 更多 >