Python plotly.express - 在 scatter_geo 中添加标签
Python plotly.express - add labels in scatter_geo
我需要在 scatter_geo 可视化中的标记上添加静态标签。我添加了 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()
我需要在 scatter_geo 可视化中的标记上添加静态标签。我添加了 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()