3

嗨,是否可以有两种不同的气泡类型代表来自同一数据帧的两个不同值?

目前我的代码如下:

covid = pd.read_csv('covid_19_data.csv')

fig = px.scatter_geo(covid, locations="Country/Region", locationmode="country names",animation_frame = "ObservationDate", hover_name = "Country/Region", size = "Confirmed", size_max = 100, projection= "natural earth")

产生以下输出: 地图输出

是否有可能让它显示两个不同的气泡,一个用于确诊病例,另一个用于推文?我正在使用的数据框如下所示: Dataframe

4

1 回答 1

1

当然!您可以使用以下方法从现有px.scatter_geo()数据集自由添加另一个数据集: px.scatter_geo()

fig=px.scatter_geo()
fig.add_traces(fig1._data)
fig.add_traces(fig2._data)

与您类似的设置fig1._data来自:

fig = px.scatter_geo(covid, locations="Country/Region", locationmode="country names",animation_frame = "ObservationDate", hover_name = "Country/Region", size = "Confirmed", size_max = 100, projection= "natural earth")

由于您没有提供数据集,我将使用px.data.gapminder()和使用列popgdpPercap,后者的颜色设置为'rgba(255,0,0,0.1)'透明红色:

在此处输入图像描述

完整代码:

import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig1 = px.scatter_geo(df, locations="iso_alpha",
                     size="pop", # size of markers, "pop" is one of the columns of gapminder
                     )
fig2 = px.scatter_geo(df, locations="iso_alpha",
                     size="gdpPercap", # size of markers, "pop" is one of the columns of gapminder
                     )

# fig1.add_traces(fig2._data)
# fig1.show()
fig=px.scatter_geo()
fig.add_traces(fig1._data)
fig.add_traces(fig2._data)

fig.data[1].marker.color = 'rgba(255,0,0,0.1)'

f = fig.full_figure_for_development(warn=False)
fig.show()

请让我知道这对你有什么影响。

于 2021-03-03T23:56:42.587 回答