简体   繁体   中英

Reposition Colorbar plotly express / graph object Maps

I am trying to move / reposition the colorbar. I overlay multiple layers of spatial data on the map, and the colorbar overlaps with eachother. I'd like to be able to reposition the colorbars.

I am using the US counties GeoJSON file from: https://eric.clst.org/tech/usgeojson/

import pandas as pd 
import geopandas as gpd
import mapbox
from plotly import graph_objs as go
from plotly.graph_objs import *

# Update with host url
df_geo = gpd.read_file('https://raw.githubusercontent.com/host')

import plotly.graph_objects as go

fig = go.Figure(go.Choroplethmapbox(geojson=df_geo['geometry'].to_json(), 
                                    locations=df_geo['NAME'], 
                                    z=df_geo['CENSUSAREA'],
                                    autocolorscale=False,
                                    colorscale="Viridis", 
                                    zmin=df_geo['CENSUSAREA'].min(), 
                                    zmax=df_geo['CENSUSAREA'].max(), 
                                    marker_line_width=0))


fig.update_layout(mapbox_style="light", 
                  mapbox_accesstoken=token,
                  mapbox_zoom=3, 
                  mapbox_center = {"lat": 37.0902, "lon": -95.7129},
                  margin={"r":0,"t":0,"l":0,"b":0})

fig.update_layout()
fig.show()

在此处输入图像描述

  • I found a few issues with your sample code
    1. to_json() does not return geojson . Used __geo_interface__ instead
    2. related to 1. locations needs to link to id in geojson . Used set_index() as well to make it consistent and use real id in geojson
    3. colors were overloaded to Alaska, so used quantile()
  • finally, question. Positioning can be done with colorbar argument.
import pandas as pd
import geopandas as gpd

# import mapbox
import requests
import plotly.graph_objects as go

# Update with host url
df_geo = gpd.GeoDataFrame.from_features(
    requests.get(
        "https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_20m.json"
    ).json()
)

import plotly.graph_objects as go

fig = go.Figure(
    go.Choroplethmapbox(
        geojson=df_geo.set_index("GEO_ID")["geometry"].__geo_interface__,
        locations=df_geo["GEO_ID"],
        z=df_geo["CENSUSAREA"],
        autocolorscale=False,
        colorscale="Viridis",
        zmin=df_geo["CENSUSAREA"].min(),
        zmax=df_geo["CENSUSAREA"].quantile(0.95),
        marker_line_width=0,
        colorbar={"orientation": "h", "x": 0.5, "yanchor": "middle", "y": 0.1},
    )
)


fig.update_layout(
    mapbox_style="carto-positron",
    # mapbox_accesstoken=token,
    mapbox_zoom=3,
    mapbox_center={"lat": 37.0902, "lon": -95.7129},
    margin={"r": 0, "t": 0, "l": 0, "b": 0},
)

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.

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