Plotly:如何在散点图中自定义标签?
Plotly: How to customize labels in a scattergeo plot?
在下面的代码中,我有标记标签,现在靠近这些标记。在标记和它的标签之间制作自定义目的地的方法是什么?我现在想把标签放得离标记有点远。
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.update_traces(hovertemplate ='bins')
fig.add_trace(go.Scattergeo(lon=df["longitude"],
lat=df["latitude"],
text=df["data"],
textposition="middle right",
mode='text',
showlegend=False))
fig.show()
我看到您正在使用第二个 Scattergeo
跟踪来显示您的标签。这似乎不是一个坏主意,因为在我看来 fig.add_annotation
在 px.scatter_geo
图中可能有点棘手。所以我会简单地稍微调整纬度和经度数字以获得您喜欢的位置的标签,例如 lon=[float(d) + 10 for d in df['longitude']]
:
完整代码:
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.update_traces(hovertemplate ='bins')
fig.add_trace(go.Scattergeo(lon=[float(d) + 10 for d in df['longitude']],
lat=[float(d) - 10 for d in df['latitude']],
text=df["data"],
textposition="middle right",
mode='text',
showlegend=False))
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.update_traces(hovertemplate ='bins')
fig.add_trace(go.Scattergeo(lon=df["longitude"],
lat=df["latitude"],
text=df["data"],
textposition="middle right",
mode='text',
showlegend=False))
fig.show()
我看到您正在使用第二个 Scattergeo
跟踪来显示您的标签。这似乎不是一个坏主意,因为在我看来 fig.add_annotation
在 px.scatter_geo
图中可能有点棘手。所以我会简单地稍微调整纬度和经度数字以获得您喜欢的位置的标签,例如 lon=[float(d) + 10 for d in df['longitude']]
:
完整代码:
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.update_traces(hovertemplate ='bins')
fig.add_trace(go.Scattergeo(lon=[float(d) + 10 for d in df['longitude']],
lat=[float(d) - 10 for d in df['latitude']],
text=df["data"],
textposition="middle right",
mode='text',
showlegend=False))
fig.show()