【问题标题】:Convert plotly marker from continuous to discrete将绘图标记从连续转换为离散
【发布时间】:2021-09-19 15:39:21
【问题描述】:

以下是我尝试使用 plotly 在地图上显示的输入文件。

data.csv
lat,long,type
-7.80715,110.371203,1
-7.791087,110.368346,3
-7.778744,110.365107,7
-7.77877,110.365379,4

脚本有效,但比例以连续格式显示。我试图将type 列转换为here 提到的文本,但我无法让它工作。有没有更简单的方法来解决这个问题?

df = pd.read_csv("data.csv").dropna()
fig = go.Figure(go.Scattermapbox(
        lat=df["lat"].tolist(),
        lon=df["long"].tolist(),
        mode='markers',
        text=df['type'].tolist(),
        marker=go.scattermapbox.Marker(
            size=10,
            color=df['type'],
            showscale=True
        ),
    ))

fig.show()

【问题讨论】:

  • 不知道是不是这个原因,但是你用df[type]而不是df['type']
  • 抱歉,打错了。

标签: python plotly plotly-python


【解决方案1】:

如果要指定离散的颜色,可以直接作为颜色规范列表处理,也可以在plotly_express中指定默认的color name

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

mapbox_access_token = open("mapbox_api_key.txt").read()
colors = px.colors.qualitative.D3

fig = go.Figure(go.Scattermapbox(
        lat=df["lat"].tolist(),
        lon=df["long"].tolist(),
        mode='markers',
        text=df['type'].tolist(),
        marker=go.scattermapbox.Marker(
            size=10,
            color=colors, 
            showscale=False
        ),
    ))

fig.update_layout(
    autosize=False,
    height=450,
    width=1000,
    mapbox=dict(
        accesstoken=mapbox_access_token,
        style="outdoors",
        center=dict(
            lat=-7.78,
            lon=110.365
        ),
        zoom=10),
    showlegend = False
)

fig.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2013-04-15
    • 2021-08-04
    相关资源
    最近更新 更多