Plotly,为什么我的泡泡都是一样的颜色?

我的数据中有一个名为 Pizza Shops 的列,其中包含按州划分的数字,范围从 10k 到超过 100 万(数字是由组成的)。出于某种原因,每个气泡虽然看起来大小合适,但都显示为相同的颜色(红色)。


我的代码


import plotly.graph_objects as go

import pandas as pd

import os


xl_path = "path to XLSX file"


df = pd.read_excel(open(xl_path, 'rb'), sheet_name='Data')

df.head()

scale = 5000

limits = [(0,15000),(15000,50000),(50000,100000),(100000,500000),(500000,2000000)]

colors = ["red","orange","yellow","green","blue"]

df['Text'] = df['State'] + '<br>Number of Pizza Shops ' + (df['Pizza Shops']).astype(str)


fig = go.Figure()


for i in range(len(limits)):

    lim = limits[i]

    df_sub = df[lim[0]:lim[1]]

    fig.add_trace(go.Scattergeo(

        locationmode = 'USA-states',

        locations=df['State Code'],

        text = df_sub['Text'],

        marker = dict(

            size = df_sub['Pizza Shops']/scale,

            color = colors[i],

            line_color='rgb(40,40,40)',

            line_width=0.5,

            sizemode = 'area'

        ),

        name = '{0} - {1}'.format(lim[0],lim[1])))


fig.update_layout(

        title_text = '2019 US Number of Pizza Shops<br>(Click legend to toggle traces)',

        showlegend = True,

        geo = dict(

            scope = 'usa',

            landcolor = 'rgb(217, 217, 217)',

        )

    )


fig.show()

样本数据:


| State     | State Code | Pizza Shops |

----------------------------------------

  Texas           TX         13256

  California      CA         500235

  Idaho           ID         4000

  ....           ....        .... and so on


慕桂英4014372
浏览 94回答 1
1回答

一只萌萌小番薯

问题在于,df_sub = df[lim[0]:lim[1]]您是根据行索引而不是根据商店数量对数据框进行子集化。如果您的数据框的行数少于 15,000,则所有数据点都将落入第一个存储桶中,并显示为红色。如果要根据应替换为的商店数量对数据框进行子集df_sub = df[lim[0]:lim[1]]化 df_sub = df[(df["Pizza Shops"] >= lim[0]) & (df["Pizza Shops"] < lim[1])]。import plotly.graph_objects as goimport pandas as pddf = pd.DataFrame({"State": ["Texas", "California", "Idaho", "Alabama", "Arizona", "Georgia", "Washington"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"State Code": ["TX", "CA", "ID", "AL", "AZ", "GA", "WA"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Pizza Shops": [12500, 25000, 75000, 250000, 1000000, 15000, 100000]})df["Text"] = df["State"] + "<br>Number of Pizza Shops " + (df["Pizza Shops"]).astype(str)scale = 2000limits = [(0,15000),(15000,50000),(50000,100000),(100000,500000),(500000,2000000)]colors = ["red", "orange", "yellow", "green", "blue"]fig = go.Figure()for i in range(len(limits)):&nbsp; &nbsp; lim = limits[i]&nbsp; &nbsp; df_sub = df[(df["Pizza Shops"] >= lim[0]) & (df["Pizza Shops"] < lim[1])]&nbsp; &nbsp; fig.add_trace(go.Scattergeo(&nbsp; &nbsp; &nbsp; &nbsp; locationmode="USA-states",&nbsp; &nbsp; &nbsp; &nbsp; locations=df_sub["State Code"],&nbsp; &nbsp; &nbsp; &nbsp; text=df_sub["Text"],&nbsp; &nbsp; &nbsp; &nbsp; marker=dict(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size=df_sub["Pizza Shops"]/scale,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color=colors[i],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line_color="rgb(40,40,40)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line_width=0.5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sizemode="area"),&nbsp; &nbsp; &nbsp; &nbsp; name="{0} - {1}".format(lim[0],lim[1])))fig.update_layout(&nbsp; &nbsp; title_text="2019 US Number of Pizza Shops<br>(Click legend to toggle traces)",&nbsp; &nbsp; showlegend=True,&nbsp; &nbsp; geo=dict(scope="usa", landcolor="rgb(217, 217, 217)"))fig.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python