Home > Enterprise >  Plotly: How to adjust size of markers in a scatter geo map so that differences become more visible?
Plotly: How to adjust size of markers in a scatter geo map so that differences become more visible?

Time:01-28

I used in Python library Plotly and plot: Bubble-Map to display average_score of countries. However, average_score has values between 2-4 and therefore the size of the bubbles in the bubble-map plot does not differentiate much (the size of bubbles is very similar). How can I achieve a bigger difference among bubbles with values 2 and 3 and 4?

Here is my piece of code:

plot = px.scatter_geo(df, locations="country_code", color="country_code", size_max=20,hover_name="country_code", size="avg_score", animation_frame="year", projection="natural earth",title="Bubble Map",labels={"country_code": "Country"})

Thank you in advance for any hints! :)

CodePudding user response:

I would raise your raw data to the power of a number that suits the visualization you're aiming to build. This makes larger numbers look disproportionally larger than sall numbers. Compare the two plots below where the first is the px.scatter_geo() example from the enter image description here

1. Data for df['pop'] raised to the power of 1.6

enter image description here

Of course these numbers have no other business in the figure, so you will have to include the following in order to keep the correct hoverinfo:

fig.update_traces(hovertemplate = 'pop=%{text}<br>iso_alpha=%{location}<extra></extra>', text = df['pop'])

enter image description here

Complete code:

import plotly.express as px
df = px.data.gapminder().query("year == 2007")
df['pop_display'] = df['pop']**1.6
fig = px.scatter_geo(df, locations="iso_alpha",
                     size="pop_display", 
                     )
fig.update_traces(hovertemplate = 'pop=%{text}<br>iso_alpha=%{location}<extra></extra>', text = df['pop'])
fig.show()

CodePudding user response:

What you're asking is not really a Plotly question but a general math question.

Given the inputs [2,3,4] step=1. return corresponding integers that have step > 1. There are multiple ways you can accomplish this: One way is to multiply all items by an integer.

[2,3,4] * 2 = [4, 6, 8] # step=2
[2,3,4] * 3 = [6, 9, 12] # step=3
...

In this case the difference between new values will rise linearly. Meaning that the step between all values will remain constant. If you want step to grow in a non linear way you can square the items:

[2,3,4]^2  = [4, 9, 16] # step=5, 7...
[2,3,4]^3= [8, 27, 64] # step=19,37...
...

Possibilities are really endless. It all depends on what kind of difference you want between the bubbles. In code, quick and dirty solution will look something like this:


plot = px.scatter_geo(df,
                      locations="country_code",
                      color="country_code", 
                      size_max=20,
                      hover_name="country_code",
                      size=df["avg_score"]**2,
                      animation_frame="year",
                      projection="natural earth",
                      title="Bubble Map",
                      labels={"country_code": "Country"}
)
  •  Tags:  
  • Related