我需要在scatter_geo vizualization中为标记添加静态标签。我添加了 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
您可以使用参数 mode
和 textposition
的方法内,而对于 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()