在Python中,有没有一种方法可以在散点图上将静态标签放在数据点旁边?

huangapple go评论65阅读模式
英文:

Is there a way to put static labels next to points on a scatterplot in python?

问题

以下是您要的翻译部分:

import plotly.express as px

fig = px.scatter_mapbox(df, lat="Latitude", lon="Longitude", 
                        size="Count",
                        zoom=5,
                        mapbox_style="carto-positron",
                        text="Name",
                        opacity=0.4)

fig.show()

请注意,这是代码的翻译部分,其他内容不包括在内。

英文:

I currently have a dataframe that looks something like this:

Name Count Latitude Longtitude
place1 12 38 -121
place2 39 37 -119

I want to visualize these over a map - and this the plotly code I have as of now:

import plotly.express as px

fig = px.scatter_mapbox(df, lat="Latitude", lon="Longitude", 
                        size="Count",
                        zoom=5,
                        mapbox_style="carto-positron",
                        text="Name",
                        opacity=0.4)

fig.show()

Now, using parameters like tooltip or hover_data make the Name of each place show up when the mouse hovers over it. But I'd like these to be static labels. I saw a very similar question but that only seems to deal with the .Scatter method - which has a parameter mode that scatter_mapbox does not have.

答案1

得分: 2

在地图上进行标注可以使用图形对象。要创建一个,您需要提前获取一个免费的Mapbox API令牌;请查看这里以获取API。为了创建这个图形,go.Scattertermapbox() 设置了标记和文本的模式,以及分别设置了标记和文本装饰的大小。对于布局,我们设置了中心、mapbox样式和API密钥。如果您需要图例,您将需要在循环过程中设置数据帧的名称。文档中有一个示例与您的问题类似。

import plotly.graph_objects as go

mapbox_access_token = open("mapbox_api_key.txt").read()

fig = go.Figure()

fig.add_trace(go.Scattermapbox(mode='markers+text',
                               lat=df['Latitude'],
                               lon=df['Longitude'],
                               marker=go.scattermapbox.Marker(
                                   size=df['Count'],
                                   color='blue',
                                   opacity=0.4
                               ),
                               text=df['Name'],
                               textfont=dict(size=12, color='red'),
                               textposition='bottom center'
                              )
             )

fig.update_layout(
    autosize=True,
    hovermode='closest',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        style="light",
        bearing=0,
        center=dict(
            lat=df['Latitude'].mean(),
            lon=df['Longitude'].mean()
        ),
        pitch=0,
        zoom=5
    ),
)

fig.show()

在Python中,有没有一种方法可以在散点图上将静态标签放在数据点旁边?

英文:

Annotating on a map is possible with a graph object. To create one, you will need to obtain a free Mapbox API token in advance; see here to obtain the API. To create the graph, go.Scattertermapbox() sets the mode for markers and text, and sets the size of markers and text decoration, respectively. For layout, we set the center, mapbox style and API key. If you need a legend, you will need to set the name of the data frame in a looping process. The reference has an example similar to your question.

import plotly.graph_objects as go

mapbox_access_token = open("mapbox_api_key.txt").read()

fig = go.Figure()

fig.add_trace(go.Scattermapbox(mode='markers+text',
                               lat=df['Latitude'],
                               lon=df['Longitude'],
                               marker=go.scattermapbox.Marker(
                                   size=df['Count'],
                                   color='blue',
                                   opacity=0.4
                               ),
                               text=df['Name'],
                               textfont=dict(size=12,color='red'),
                               textposition='bottom center'
                              )
             )

fig.update_layout(
    autosize=True,
    hovermode='closest',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        style="light",
        bearing=0,
        center=dict(
            lat=df['Latitude'].mean(),
            lon=df['Longitude'].mean()
        ),
        pitch=0,
        zoom=5
    ),
)

fig.show()

在Python中,有没有一种方法可以在散点图上将静态标签放在数据点旁边?

huangapple
  • 本文由 发表于 2023年2月16日 11:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定