简体   繁体   English

如何获得为 Plotly Scattergeo 显示的图例?

[英]How to get a legend displayed for Plotly Scattergeo?

I hacked together this code to plot lat and lon coordinates on a map, and the code works pretty darn well, but I can't seem to get the legend displayed, so it's hard to tell what I'm actually looking at.我将这段代码一起破解为 map 上的 plot 纬度和经度坐标,代码工作得非常好,但我似乎无法显示图例,所以很难说出我实际在看什么。

import pandas as pd
import pandas_bokeh
import matplotlib.pyplot as plt
import pgeocode
import geopandas as gpd
from shapely.geometry import Point
from geopandas import GeoDataFrame
pandas_bokeh.output_notebook()
import plotly.graph_objects as go

nomi = pgeocode.Nominatim('us')

df_melted['Latitude'] = (nomi.query_postal_code(df_melted['my_zip'].tolist()).latitude)
df_melted['Longitude'] = (nomi.query_postal_code(df_melted['my_zip'].tolist()).longitude)


df_melted['colors'] = df_melted['value'].groupby(df_melted['value']).transform('count')
print(df_melted.shape)
print(df_melted.head())

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

fig = go.Figure(data=go.Scattergeo(
        lon = df_melted['Longitude'],
        lat = df_melted['Latitude'],
        text = df_melted['value'],
        marker_color = df_melted['colors']
        ))

fig.update_layout(
    autosize=False,
    width=1000,
    height=1000,
    title = 'Footprints Compared Based on Lat & Lon Coordinates)',
    geo_scope='usa',
    showlegend=True
    )

fig.update_layout(legend=dict(
    orientation="h",
    yanchor="bottom",
    y=1.02,
    xanchor="right",
    x=1
))
fig.show()

在此处输入图像描述

When I run the code, I see a nice map of the US, but there is not legend, even though I'm using this small script directly below, which came straight from the Plotly documentation.当我运行代码时,我看到了一个不错的美国 map,但没有传说,即使我在下面直接使用这个小脚本,它直接来自 Plotly 文档。

legend=True & showlegend=True legend=True & showlegend=True

Both gave me errors.两者都给了我错误。 Any idea how to get the legend to show up here?知道如何让传说出现在这里吗?

  • have used earthquake data to be able to simulate df_melted with compatible columns已经使用地震数据来模拟df_melted与兼容的列
  • there really is only one missing parameter: marker_coloraxis="coloraxis"确实只缺少一个参数: marker_coloraxis="coloraxis"
  • also changed showlegend=False也改变了showlegend=False

full working example using OP plotting code使用 OP 绘图代码的完整工作示例

import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
from shapely.geometry import Point
from geopandas import GeoDataFrame
import plotly.graph_objects as go
import requests

res = requests.get(
    "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojson"
)

us = (
    gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
    .loc[lambda d: d["iso_a3"].eq("USA"), "geometry"]
    .values[0]
)
gdf = gpd.GeoDataFrame.from_features(res.json(), crs="epsg:4386").loc[
    lambda d: d.intersects(us)
]
df_melted = pd.DataFrame(
    {
        "Latitude": gdf["geometry"].y,
        "Longitude": gdf["geometry"].x,
        "colors": gdf["mag"],
        "value": gdf["place"],
    }
)
fig = go.Figure(
    data=go.Scattergeo(
        lon=df_melted["Longitude"],
        lat=df_melted["Latitude"],
        text=df_melted["value"],
        marker_color=df_melted["colors"],
        marker_coloraxis="coloraxis",
    )
)

fig.update_layout(
    autosize=False,
    width=400,
    height=400,
    title="Footprints Compared Based on Lat & Lon Coordinates)",
    geo_scope="usa",
    showlegend=False,
)

fig.update_layout(
    legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
fig.show()

在此处输入图像描述

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

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