繁体   English   中英

Python Plotly 在同一个 scatter_geo 上表达两个气泡标记?

[英]Python Plotly express two bubble markers on the same scatter_geo?

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

目前我的代码如下:

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

产生以下 output: 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()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM