I want to plot a scatter_mapbox plot over a choropleth_mapbox plot using plotly. I want to use Picnic for the scatter_mapbox plot's color scale. When I run this, the scatter_mapbox colorscale is set to the same properties as the choropleth_mapbox properties. Namely, it uses the color scale Viridis instead of Picnic and uses the choropleth's numerical scale. How can I make the the colorscale for the scatterplot Picnic.
import numpy as np
import pandas as pd
import geopandas as gpd
import plotly.express as px
import geopandas as gpd
import shapely
df = px.data.election()
df = gpd.GeoDataFrame.from_features(
px.data.election_geojson()["features"]
).merge(df, on="district").set_index("district")
df = df.loc[df['geometry'].map(lambda x: type(x) == shapely.geometry.polygon.Polygon)]
df2 = df.copy()
df2['geometry'] = df2['geometry'].map(lambda x: x.exterior.coords[0]).map(shapely.geometry.Point)
#make the charts
map_fig = px.choropleth_mapbox(
df,
geojson=df.geometry,
locations=df.index,
color='Bergeron',
center= { 'lon': df2.geometry.x.iloc[0], 'lat': df2.geometry.y.iloc[0]},
color_continuous_scale="Viridis",
mapbox_style="carto-positron",
opacity = 0.2,
)
map_fig2 = px.scatter_mapbox(
df2,
lat=df2.geometry.y,
lon=df2.geometry.x,
size='Bergeron',
zoom=12,
color='Bergeron', color_continuous_scale='Picnic',
opacity = 1,
size_max=10
)
map_fig.add_trace(map_fig2.data[0])
map_fig.update_geos(fitbounds="locations", visible=False)
map_fig.show()
A step in the right direction is to add this, which puts the scatter_mapbox on a separate coloraxis, but sets the color scale to the plotly default, Plasma, instead of Picnic, as specified. It also overlays the colorbar.
'color' : np.array(df2['Bergeron']),
'coloraxis' : 'coloraxis2',
'opacity' : 1,
'colorscale' : 'Picnic',
'sizemode' : 'area',
'sizeref' : .01,
'autocolorscale' : False
}
If this is what you're aiming to do:
Then follow these steps in addition to what you're already doing:
1. Steal the coloraxis from fig2
where color='Picnic'
to fig
with:
fig.layout.coloraxis2 = fig2.layout.coloraxis
2. Include the second trace with:
fig.add_trace(fig2.data[0])
3. Assign colors to the second trace with:
fig['data'][1]['marker'] = { 'color' : np.array(df2['Bergeron']),
'coloraxis' : 'coloraxis2',
}
4. Move the second colorbar to a more suitable place with:
fig.layout.coloraxis2.colorbar.x = -0.2
The third step makes the colors for the second trace available through 'coloraxis': 'coloraxis2'
I hope this is what you were looking for. Don't hesitate to let me know if not!
(Sorry, I got tired of typing map_fig
so I change the references to merely fig
)
import numpy as np
import pandas as pd
import geopandas as gpd
import plotly.express as px
import geopandas as gpd
import shapely
df = px.data.election()
df = gpd.GeoDataFrame.from_features(
px.data.election_geojson()["features"]
).merge(df, on="district").set_index("district")
df = df.loc[df['geometry'].map(lambda x: type(x) == shapely.geometry.polygon.Polygon)]
df2 = df.copy()
df2['geometry'] = df2['geometry'].map(lambda x: x.exterior.coords[0]).map(shapely.geometry.Point)
#make the charts
fig = px.choropleth_mapbox(
df,
geojson=df.geometry,
locations=df.index,
color='Bergeron',
center= { 'lon': df2.geometry.x.iloc[0], 'lat': df2.geometry.y.iloc[0]},
color_continuous_scale="Viridis",
mapbox_style="carto-positron",
opacity = 0.2,
)
fig2 = px.scatter_mapbox(
df2,
lat=df2.geometry.y,
lon=df2.geometry.x,
size='Bergeron',
zoom=12,
color='Bergeron',
color_continuous_scale='picnic',
opacity = 1,
size_max=10
)
fig.add_trace(fig2.data[0])
fig.layout.coloraxis2 = fig2.layout.coloraxis
fig['data'][1]['marker'] = { 'color' : np.array(df2['Bergeron']),
'coloraxis' : 'coloraxis2',
'opacity' : 1,
'sizemode' : 'area',
'sizeref' : .01,
'autocolorscale' : False
}
fig.update_geos(fitbounds="locations", visible=False)
fig.layout.coloraxis2.colorbar.x = -0.2
fig.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.