Python 在同一个 scatter_geo 上绘制两个气泡标记?

Python Plotly express two bubble markers on the same scatter_geo?

您好,是否可以使用两种不同的气泡类型来表示同一数据帧中的两个不同值?

目前我的代码如下:

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

产生以下输出: Map output

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

好的!您可以使用 现有 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()

请告诉我这对你有何影响。