同时使用两个变量的绘图气泡图,图例,颜色,可见性问题

问题描述 投票:1回答:1

[我正在尝试使用气泡图在美国地图上同时绘制两个变量:比萨饼店和冰淇淋店的数量。

但是,图例似乎是交错的,而不是单独保存图例。而且很难区分气泡,是否有一种更简单的方法让我一次看到两个气泡,也许是通过不透明度?有没有一种更好的方法可以自动缩放颜色而不使用显式数组?例如,如果我想对比萨饼使用一种渐变,对冰淇淋使用另一种渐变?

我的代码:

import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({"State": ["Texas", "California", "Idaho", "Alabama", "Arizona", "Georgia", "Washington"],
                   "State Code": ["TX", "CA", "ID", "AL", "AZ", "GA", "WA"],
                   "Pizza Shops": [12500, 25000, 75000, 250000, 1000000, 15000, 100000],
                   "Ice Cream Shops": [9000, 150000, 75000, 300000, 4000000, 15000, 30000]})

df["PizzaText"] = df["State"] + "<br>Pizza Shops: " + (df["Pizza Shops"]).astype(str)
df["IceCreamText"] = df["State"] + "<br>Ice Cream Shops: " + (df["Ice Cream Shops"]).astype(str)

scale = 2000

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

pizza_colors = ["red"]
ice_cream_colors = ["blue"]

fig = go.Figure()

for i in range(len(limits)):

    lim = limits[i]

    df_sub_pizza = df[(df["Pizza Shops"] >= lim[0]) & (df["Pizza Shops"] < lim[1])]
    df_sub_ice_cream = df[(df["Ice Cream Shops"] >= lim[0]) & (df["Ice Cream Shops"] < lim[1])]

    fig.add_trace(go.Scattergeo(
        locationmode="USA-states",
        locations=df_sub_pizza["State Code"],
        text=df_sub_pizza["PizzaText"],
        marker=dict(
            size=df_sub_pizza["Pizza Shops"]/scale,
            color=pizza_colors[0],
            line_color="rgb(40,40,40)",
            line_width=0.5,
            sizemode="area"),
        name="{0} - {1}".format(lim[0],lim[1])))

    fig.add_trace(go.Scattergeo(
        locationmode="USA-states",
        locations=df_sub_ice_cream["State Code"],
        text=df_sub_ice_cream["IceCreamText"],
        marker=dict(
            size=df_sub_ice_cream["Ice Cream Shops"] / scale,
            color=ice_cream_colors[0],
            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 and Ice Cream Shops<br>(Click legend to toggle traces)",
    showlegend=True,
    geo=dict(scope="usa", landcolor="rgb(217, 217, 217)")
)

fig.show()

图片:enter image description here

python pandas graph plotly bubble-chart
1个回答
0
投票

请不要将此作为答案(尚未)。它正在进行中。我发现您的代码过于复杂,您是否考虑过使用整洁的格式和plotly.express的数据?


df = pd.DataFrame({"State": ["Texas", "California", "Idaho", "Alabama", "Arizona", "Georgia", "Washington"],
                   "State Code": ["TX", "CA", "ID", "AL", "AZ", "GA", "WA"],
                   "Pizza Shops": [12500, 25000, 75000, 250000, 1000000, 15000, 100000],
                   "Ice Cream Shops": [9000, 150000, 75000, 300000, 4000000, 15000, 30000]})

df["PizzaText"] = df["State"] + "<br>Pizza Shops: " + (df["Pizza Shops"]).astype(str)
df["IceCreamText"] = df["State"] + "<br>Ice Cream Shops: " + (df["Ice Cream Shops"]).astype(str)

scale = 2000

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

pizza_colors = ["red"]
ice_cream_colors = ["blue"]

整洁的数据框

df["Text"] = df.apply(lambda x: [x["PizzaText"], x["IceCreamText"]], axis=1)
df = df.explode("Text")
df["Shops"] = np.where(df["Text"].str.contains("Pizza"), df["Pizza Shops"],  df["Ice Cream Shops"])
df["Type"] = np.where(df["Text"].str.contains("Pizza"), "Pizza",  "Ice Cream")
df["Size"] = df["Shops"]/scale
print(df[['State', 'State Code', 'Shops', 'Type', 'Size']].head())
        State State Code   Shops       Type   Size
0       Texas         TX   12500      Pizza   6.25
0       Texas         TX    9000  Ice Cream   4.50
1  California         CA   25000      Pizza  12.50
1  California         CA  150000  Ice Cream  75.00
2       Idaho         ID   75000      Pizza  37.50

使用plotly.express

fig = px.scatter_geo(df,
               locations="State Code",
               locationmode="USA-states",
               text="Text",
               color="Type",
               size="Size",
               opacity=0.8
               )
fig.update_layout(
    title_text="2019 US Number of Pizza and Ice Cream Shops<br>(Click legend to toggle traces)",
    title_x=0.5,
    showlegend=True,
    geo=dict(scope="usa", landcolor="rgb(217, 217, 217)")
)

fig.show()

enter image description here

待办事项

将不胜感激地检查是否可以

  • 将气泡分开。至少在像大叶一样发生缩放时
  • 添加气泡大小图例
© www.soinside.com 2019 - 2024. All rights reserved.