Black Lives Matter. Please consider donating to Black Girls Code today.

Center Map plot python

Plotly on python here. Cant seem to get the center and lat lon set up correctly. Any help would be greatly appreciated. I have the scale attribute working okay. But the problem is that my map does not center over the northeast region of the USA. it always goes to the center of the scope.

###########I have tried this below

layout = dict(
title = ‘2014 US city populations
(Click legend to toggle traces)’,
mapbox= dict(bearing=0, pitch=0, zoom=10, center=dict(lat=45,lon=77)),

    geo = dict(
        scope='north america',
        autosize=True,
        
        projection=dict( type='albers usa' , scale=4),
        #center =dict(lon=[70],lat=[40]),
        
        
        showland = True,
        landcolor = 'rgb(217, 217, 217)',
        subunitwidth=1,
        countrywidth=1,
        subunitcolor="rgb(255, 255, 255)",
        countrycolor="rgb(255, 255, 255)"
    ),
)

and This #################

layout = dict(
title = ‘2014 US city populations
(Click legend to toggle traces)’,
#mapbox= dict(bearing=0, pitch=0, zoom=10, center=dict(lat=45,lon=77)),

    geo = dict(
        scope='north america',
        autosize=True,
        
        projection=dict( type='albers usa' , scale=4),
        center =dict(lon=[70],lat=[40]),
        
        
        showland = True,
        landcolor = 'rgb(217, 217, 217)',
        subunitwidth=1,
        countrywidth=1,
        subunitcolor="rgb(255, 255, 255)",
        countrycolor="rgb(255, 255, 255)"
    ),
)

fig = dict( data=cities, layout=layout )
iplot( fig, validate=False, filename=‘d3-bubble-map-populations’ , image=‘svg’)

note: commenting out autosize=True did not help

Are you using a geo subplot or a mapbox subplot?

should be geosubplot

All code is below except for the pandas table i am hitting

#df = pd.read_csv(‘https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv’)

df[‘text’] = ‘test’
limits = [(0,15),(15,45)]
colors = [“rgb(0,116,217)”,“rgb(255,65,54)”,“rgb(133,20,75)”,“rgb(255,133,27)”,“lightgrey”]
cities = []
scale = .7

for i in range(len(limits)):
lim = limits[i]
df_sub = df[lim[0]:lim[1]]
city = dict(
type = ‘scattergeo’,
locationmode = ‘USA-states’,
lon = df_sub[‘lon’],
lat = df_sub[‘lat’],
text = df_sub[‘text’],
marker = dict(
size = df_sub[‘Magnitude’]/scale,
color = colors[i],
line = dict(width=0.5, color=‘rgb(40,40,40)’),
sizemode = ‘area’
),
name = ‘{0} - {1}’.format(lim[0],lim[1]) )
cities.append(city)

layout = dict(
title = ‘2014 US city populations
(Click legend to toggle traces)’,
#mapbox= dict(bearing=0, pitch=0, zoom=10, center=dict(lat=45,lon=77)),

    geo = dict(
        scope='usa',
       # autosize=True,
        
        projection=dict( type='usa-states' , scale=1),
        lonaxis =dict(range=[-20,90]),
        lataxis =dict(range=[-100,90]),
        
        
        showland = True,
        landcolor = 'rgb(217, 217, 217)',
        subunitwidth=1,
        countrywidth=1,
        subunitcolor="rgb(255, 255, 255)",
        countrycolor="rgb(255, 255, 255)"
    ),
)

fig = dict( data=cities, layout=layout )
iplot( fig, validate=False, filename=‘d3-bubble-map-populations’ , image=‘svg’)

Your attributes are a little off. Trimming out validate: False should help you figure out where you made mistakes.

Here’s a working example: https://codepen.io/etpinard/pen/EREzjG?editors=0010

Thanks for the review. Unfortunately i cant get it to work. every time I add center =dict(lon=31,lat=99) under geo the map goes white and i cant zoom or pan.

If you could provide a simple example that shows me how to move the map center it would be greatly appreciated.

df[‘text’] = ‘test’
limits = [(0,15),(15,45)]
colors = [“rgb(0,116,217)”,“rgb(255,65,54)”,“rgb(133,20,75)”,“rgb(255,133,27)”,“lightgrey”]
cities = []
scale = .7

for i in range(len(limits)):
lim = limits[i]
df_sub = df[lim[0]:lim[1]]
city = dict(
type = ‘scattergeo’,
locationmode = ‘USA-states’,
lon = df_sub[‘lon’],
lat = df_sub[‘lat’],
text = df_sub[‘text’],
marker = dict(
size = df_sub[‘Magnitude’]/scale,
color = colors[i],
line = dict(width=0.5, color=‘rgb(40,40,40)’),
sizemode = ‘area’
),
name = ‘{0} - {1}’.format(lim[0],lim[1]) )
cities.append(city)

layout = dict(
title = ‘2014 US city populations
(Click legend to toggle traces)’,

    geo = dict(
        scope='north america',
        autosize=True,
        center =dict(lon=31,lat=99),
        projection=dict( type='albers usa' , scale=1),
        
        
        
        showland = True,
        landcolor = 'rgb(217, 217, 217)',
        subunitwidth=1,
        countrywidth=1,
        subunitcolor="rgb(255, 255, 255)",
        countrycolor="rgb(255, 255, 255)"
    ),
)

fig = dict( data=cities, layout=layout )
iplot( fig, validate=False, filename=‘d3-bubble-map-populations’ , image=‘svg’)