繁体   English   中英

Python plotly.express - 在 scatter_geo 中添加标签

[英]Python plotly.express - add labels in scatter_geo

我需要在 scatter_geo vizualization 中的标记上添加 static 标签。 我添加了text='data' ,但什么也没发生:

import plotly.express as px
import pandas as pd

rows=[['501-600','15','122.58333','45.36667'],
      ['till 500','4','12.5','27.5'],
      ['more 1001','41','-115.53333','38.08'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)
df = df.astype({"data": int})

fig=px.scatter_geo(df,lon='longitude', lat='latitude',
                      color='bins',
                      text='data',
                      opacity=0.5,size='data',
                      projection="natural earth")

fig.show()
fig.write_html('first_figure.html', auto_open=True)

你是一个远离解决方案的update_trace 使用go.Scattergeo您可以在方法中使用参数modetextposition ,而使用plotly.express您应该在之后进行更改。

import plotly.express as px
import pandas as pd

rows=[['501-600','15','122.58333','45.36667'],
      ['till 500','4','12.5','27.5'],
      ['more 1001','41','-115.53333','38.08'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)
df = df.astype({"data": int})

fig=px.scatter_geo(df,lon='longitude', lat='latitude',
                      color='bins',
                      text='data',
                      opacity=0.5,
                      size='data',
                      projection="natural earth")
# This
fig.update_traces(textposition="top center",
                  mode='markers+text')

fig.show()

** 更新**

为了避免传说中的问题,一个可能的解决方案是

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

rows=[['501-600','15','122.58333','45.36667'],
      ['till 500','4','12.5','27.5'],
      ['more 1001','41','-115.53333','38.08'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)
df = df.astype({"data": int})

fig=px.scatter_geo(df,lon='longitude', lat='latitude',
                      color='bins',
                      opacity=0.5,
                      size='data',
                      projection="natural earth")

fig.add_trace(go.Scattergeo(lon=df["longitude"],
              lat=df["latitude"],
              text=df["data"],
              textposition="middle center",
              mode='text',
              showlegend=False))
fig.show()

晚了但截至目前(2023 年 1 月)以下工作正常(假设“数据”是一个 df 列):

px.scatter_geo(text='data' .....

暂无
暂无

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

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