Reputation: 890
In the answer to this question R - plotly - combine bubble and chorpleth map, it was described nicely how to combine bubble and choropleth map within one map in plotly. I would like to replicate exactly the same example in Python, however I did not manage based on the information provided in the plotly documentation (https://plot.ly/python/reference/).
The equivalent R code reads as follows:
lon = c(-73.9865812, -118.2427266, -87.6244212, -95.3676974)
pop = c(8287238, 3826423, 2705627, 2129784)
df_cities = data.frame(cities, lat, lon, pop)
state_codes = c("NY", "CA", "IL", "TX")
pop = c(19746227.0, 38802500.0, 12880580.0, 26956958.0)
df_states = data.frame(state_codes, pop)
plot_ly(df_cities, lon=lon, lat=lat,
text=paste0(df_cities$cities,'<br>Population: ', df_cities$pop),
marker= list(size = sqrt(pop/10000) + 1), type="scattergeo",
filename="stackoverflow/choropleth+scattergeo") %>%
add_trace(z=df_states$pop,
locations=df_states$state_codes,
text=paste0(df_states$state_codes, '<br>Population: ', df_states$pop),
type="choropleth",
colors = 'Purples',
locationmode="USA-states") %>%
layout(geo = list(scope="usa"))
How can this be implemented in Python?
Upvotes: 2
Views: 1700
Reputation: 161
I found that I had to adjust r the code just a little to get it to work:
cities = c('New York', 'Los Angeles', 'Chicago', 'Houston')
lat = c(40.7305991, 34.053717, 41.8755546, 29.7589382)
lon = c(-73.9865812, -118.2427266, -87.6244212, -95.3676974)
pop = c(8287238, 3826423, 2705627, 2129784)
df_cities = data.frame(cities, lat, lon, pop)
state_codes = c("NY", "CA", "IL", "TX")
pop = c(19746227.0, 38802500.0, 12880580.0, 26956958.0)
df_states = data.frame(state_codes, pop)
plot_ly(df_cities, lon=lon, lat=lat,
text = paste0(df_cities$cities,'<br>Population: ', df_cities$pop),
marker = list(size = sqrt(pop/10000) + 1), type="scattergeo",
filename = "stackoverflow/choropleth+scattergeo") %>%
add_trace(z=df_states$pop,
locations = df_states$state_codes,
text = paste0(df_states$state_codes, '<br>Population: ', df_states$pop),
type = "choropleth",
colors = 'Purples',
locationmode = "USA-states") %>%
layout(geo = list(scope="usa"))
Upvotes: 0
Reputation: 5011
Simply append .py
to the graph's URL (e.g. https://plot.ly/~RPlotBot/1735.py) to see corresponding python code.
Upvotes: 1