简体   繁体   English

Plotly:如何在单个 plotly map 图中使用两个色标?

[英]Plotly: How to use two color scales in a single plotly map figure?

I want to plot a scatter_mapbox plot over a choropleth_mapbox plot using plotly.我想在 choropleth_mapbox plot3266C5DBACC290AADC. I want to use Picnic for the scatter_mapbox plot's color scale.我想将 Picnic 用于 scatter_mapbox 图的色阶。 When I run this, the scatter_mapbox colorscale is set to the same properties as the choropleth_mapbox properties.当我运行它时, scatter_mapbox 色阶设置为与 choropleth_mapbox 属性相同的属性。 Namely, it uses the color scale Viridis instead of Picnic and uses the choropleth's numerical scale.即,它使用色阶 Viridis 而不是 Picnic,并使用 choropleth 的数值尺度。 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.朝着正确方向迈出的一步是添加它,它将 scatter_mapbox 放在单独的颜色轴上,但将颜色比例设置为 plotly 默认值 Plasma,而不是指定的 Picnic。 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: 1.fig2 where color='Picnic'中窃取 coloraxis 到fig

fig.layout.coloraxis2 = fig2.layout.coloraxis

2. Include the second trace with: 2.包括第二个跟踪:

fig.add_trace(fig2.data[0])

3. Assign colors to the second trace with: 3.将 colors 分配给第二条轨迹:

fig['data'][1]['marker'] = {    'color' : np.array(df2['Bergeron']),
                                    'coloraxis' : 'coloraxis2',
                                }

4. Move the second colorbar to a more suitable place with: 4.将第二个颜色条移动到更合适的位置:

fig.layout.coloraxis2.colorbar.x = -0.2

The third step makes the colors for the second trace available through 'coloraxis': 'coloraxis2'第三步使 colors 可通过'coloraxis': 'coloraxis2'获得第二条迹线

I hope this is what you were looking for.我希望这就是你要找的。 Don't hesitate to let me know if not!如果没有,请不要犹豫,让我知道!

Complete code:完整代码:

(Sorry, I got tired of typing map_fig so I change the references to merely fig ) (抱歉,我厌倦了输入map_fig所以我将引用更改为仅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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM