如何使用 Plotly Choropleth 显示分类变量的所有值
How to display all the values of the categorical variable using Plotly Choropleth
任务是在鼠标悬停在州上时显示州类别,但它只显示所有州的最后一个选项,即蔬菜。
这是我试过的代码。
fig = px.choropleth(data,
locations="id",
geojson=state,
color="Category",
hover_name="State",
scope="usa",
hover_data = ["Year", "Dollars"]
)
fig.show()
我现在得到的结果。
如何显示类别变量中的所有值?
- 您的数据似乎不太适合您要实现的目标。有de-duped 总结
- 创建了 ColumnList state
中的所有类别
- 创建了 类别 状态中的类别数
- 修改 plotly express
choropleth()
以使用此汇总数据
- 为了使颜色正确,通常情况下您需要定义 featureidkey 到 link 数据框列以在 GeoJSON[=27] 中适当 属性 =]
import requests
import pandas as pd
import numpy as np
import plotly.express as px
state = requests.get(
"https://raw.githubusercontent.com/satyam2829/Choropleth/main/us-states.json"
).json()
data = pd.read_csv(
"https://raw.githubusercontent.com/satyam2829/Choropleth/main/WeeklyFoodPrices.csv"
)
data["Date"] = pd.to_datetime(data["Date"])
# 1. de-dup data, take just last date for each state
# 2. summarise data for state as each state has many categories
data = (
data.merge(
data.groupby("State", as_index=False).agg({"Date": "max"}),
on=["State", "Date"],
how="inner",
)
.groupby(["State", "Date"], as_index=False)
.agg(
Category=("Category", "last"),
Dollars=("Dollars", "last"),
LastYear=("LastYear", "last"),
CategoryList=("Category", list),
Categories=("Category","size")
)
)
fig = px.choropleth(
data,
locations="State",
featureidkey="properties.name", # needed as this is the common property
geojson=state,
color="Category",
hover_name="State",
scope="usa",
hover_data=["LastYear", "Dollars", "Categories", "CategoryList"],
)
fig
任务是在鼠标悬停在州上时显示州类别,但它只显示所有州的最后一个选项,即蔬菜。
这是我试过的代码。
fig = px.choropleth(data,
locations="id",
geojson=state,
color="Category",
hover_name="State",
scope="usa",
hover_data = ["Year", "Dollars"]
)
fig.show()
我现在得到的结果。
如何显示类别变量中的所有值?
- 您的数据似乎不太适合您要实现的目标。有de-duped 总结
- 创建了 ColumnList state 中的所有类别
- 创建了 类别 状态中的类别数
- 修改 plotly express
choropleth()
以使用此汇总数据 - 为了使颜色正确,通常情况下您需要定义 featureidkey 到 link 数据框列以在 GeoJSON[=27] 中适当 属性 =]
import requests
import pandas as pd
import numpy as np
import plotly.express as px
state = requests.get(
"https://raw.githubusercontent.com/satyam2829/Choropleth/main/us-states.json"
).json()
data = pd.read_csv(
"https://raw.githubusercontent.com/satyam2829/Choropleth/main/WeeklyFoodPrices.csv"
)
data["Date"] = pd.to_datetime(data["Date"])
# 1. de-dup data, take just last date for each state
# 2. summarise data for state as each state has many categories
data = (
data.merge(
data.groupby("State", as_index=False).agg({"Date": "max"}),
on=["State", "Date"],
how="inner",
)
.groupby(["State", "Date"], as_index=False)
.agg(
Category=("Category", "last"),
Dollars=("Dollars", "last"),
LastYear=("LastYear", "last"),
CategoryList=("Category", list),
Categories=("Category","size")
)
)
fig = px.choropleth(
data,
locations="State",
featureidkey="properties.name", # needed as this is the common property
geojson=state,
color="Category",
hover_name="State",
scope="usa",
hover_data=["LastYear", "Dollars", "Categories", "CategoryList"],
)
fig