Search code examples
pythonplotlyplotly-python

How do I stop the marker size from changing location point in Scattergeo?


I'm trying to set a bubble map with go.scattergeo, everything is good so far but when I change the size of the bubbles (each one represents a number), the biggest one "absorbs" the smallest one, see pictures:

https://i.sstatic.net/qgnIG.png https://i.sstatic.net/z5IrD.png

As you can see I have commented out two more options that I tried, but both ended with the same result. Changing the color doesn't affect the position, and I've checked the whole documentation and found nothing about it.

Does anyone know what am I doing wrong?

Thank you in advance.

This is my code so far

This is a 10 row sample of the DF i'm using (essentially it's the titanic database with assigned latitude and longitude for each port of departure):

PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Embarked lon lat 0 1 0 3 Braund, Mr. Owen Harris 0 22.000000 1 0 A/5 21171 7.2500 0 -1.406013 50.896364 1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... 1 38.000000 1 0 PC 17599 71.2833 1 -8.294143 51.850910 2 3 1 3 Heikkinen, Miss. Laina 1 26.000000 0 0 STON/O2. 3101282 7.9250 0 -1.406013 50.896364 3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) 1 35.000000 1 0 113803 53.1000 0 -1.406013 50.896364 4 5 0 3 Allen, Mr. William Henry 0 35.000000 0 0 373450 8.0500 0 -1.406013 50.896364 5 6 0 3 Moran, Mr. James 0 29.699118 0 0 330877 8.4583 2 -1.612260 49.648194 6 7 0 1 McCarthy, Mr. Timothy J 0 54.000000 0 0 17463 51.8625 0 -1.406013 50.896364 7 8 0 3 Palsson, Master. Gosta Leonard 0 2.000000 3 1 349909 21.0750 0 -1.406013 50.896364 8 9 1 3 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) 1 27.000000 0 2 347742 11.1333 0 -1.406013 50.896364 9 10 1 2 Nasser, Mrs. Nicholas (Adele Achem) 1 14.000000 1 0 237736 30.0708 1 -8.294143 51.850910

Basically it's titanic dataset with coordenates assigned to each port of departure as it follows:

df.replace({'Embarked':{'S':0, 'C':1, 'Q':2}}, inplace=True)
df.loc[df['Embarked']==0, 'lon'] = '-1.406013'
df.loc[df['Embarked']==1, 'lon'] = '-8.294143'
df.loc[df['Embarked']==2, 'lon'] = '-1.612260'

df.loc[df['Embarked']==0, 'lat'] = '50.896364'
df.loc[df['Embarked']==1, 'lat'] = '51.850910'
df.loc[df['Embarked']==2, 'lat'] = '49.648194'
import plotly.graph_objects as go


sizearray = np.asarray(df['lon'].value_counts())

fig = go.Figure(data=go.Scattergeo(lon = df['lon'],lat = df['lat'],mode = 'markers'))

fig.update_layout(
        title = 'Shipping Ports',geo_scope='world',)
fig.update_geos(fitbounds="locations") 
fig.update_traces(marker=dict(size=sizearray,sizemode='area',sizeref=2.*max(sizearray)/(40.**2),sizemin=4))
#fig.update_traces(marker_size=sizearray/5)
#fig.update_traces(marker_size=df['lon'].value_counts()/5)
fig.update_traces(marker=dict(color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)','rgb(44, 160, 101)']))
fig.show()

Solution

  • If you want to visualize the number of people embarking from each port, you can either draw a graph with a data frame grouping the original data, or you can create a graph based on unique values of latitude and longitude, similar to the way the number of cases was extracted. I have obtained the latitude and longitude as well as the array of counts and specified it as the latitude and longitude. I also customized the marker size and coloring with your code. Please correct me if my customization is wrong.

    import plotly.graph_objects as go
    
    sizearray = np.asarray(df['lon'].value_counts())
    latarray = np.asarray(df['lat'].unique())
    lonarray = np.asarray(df['lon'].unique())
    
    fig = go.Figure(data=go.Scattergeo(lon=lonarray,
                                       lat=latarray,
                                       mode='markers',
                                       marker=dict(
                                           size=sizearray,
                                           sizemode='area',
                                           sizeref=2.*max(sizearray)/(40.**2),
                                           sizemin=4,
                                           color=[
                                               'rgb(93, 164, 214)',
                                               'rgb(255, 144, 14)',
                                               'rgb(44, 160, 101)'
                                           ]
                                       )))
    
    fig.update_layout(autosize=True, height=600, title='Shipping Ports', geo_scope='europe')
    fig.update_geos(fitbounds="locations") 
    fig.show()
    

    enter image description here