Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Plotly express two bubble markers on the same scatter_geo?

Hi is it possible to have two different bubble types representing two different values from the same dataframe?

Currently my code is as follows:

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

Which produces the following output: Map output

Is it possible to get it to show two different bubbles, one for confirmed cases and another for tweets? The data frame I'm working with is shown here: Dataframe

like image 637
Rob13563656 Avatar asked Jan 28 '26 07:01

Rob13563656


1 Answers

Sure! You can freely add another dataset from px.scatter_geo() on an existing px.scatter_geo() using:

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

Where fig1._data comes from a setup similar to yours in:

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

Since you haven't provided a dataset I'll use px.data.gapminder() and use the columns pop and gdpPercap, where the color of the latter is set to 'rgba(255,0,0,0.1)' which is a transparent red:

enter image description here

Complete code:

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

Please let me know how this works out for you.

like image 151
vestland Avatar answered Jan 29 '26 19:01

vestland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!