调整Plotly中文本标签的大小

问题描述 投票:0回答:2

我正在尝试根据国家/地区大小调整文本大小,因此文本将位于该国家/地区的边界内。这是我的代码:

# imports
import pandas as pd
import plotly.express as px

# uploading file
df=pd.read_csv('regional-be-daily-latest.csv', header = 1)

# creating figure
fig = px.choropleth(df, locations='Code', color='Track Name')
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

fig.add_scattergeo(

  locations = df['Code'],
  text = df['Track Name'],
  mode = 'text',
)

fig.show()

我的代码给我的输出可视化是:

enter image description here

橙色国家/地区的文本标签位于该国家/地区的边界内,但标记蓝色国家/地区的文本比该国家/地区本身更大。

我正在寻找的是调整大小,使其不会超过该国的边界。我怎样才能做到这一点?

python plotly choropleth
2个回答
21
投票

您可以使用 update_layout 函数设置字体大小,并通过在 font 参数中传递字典来指定字体大小。

import pandas as pd
import plotly.express as px

df=pd.read_csv('regional-be-daily-latest.csv', header = 1)

fig = px.choropleth(df, locations='Code', color='Track Name')
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

fig.add_scattergeo(

  locations = df['Code'],
  text = df['Track Name'],
  mode = 'text',
)


fig.update_layout(
    font=dict(
        family="Courier New, monospace",
        size=18,  # Set the font size here
        color="RebeccaPurple"
    )
)

fig.show()

2
投票

Python 中的文本和注释

您可以像这样使用uniformtext控制文本大小

fig.update_traces(textposition='inside') fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')

控制最大文本大小

fig.update_traces(textposition='inside', textfont_size=14)
通过这些方式,您无需更改轴和标签字体大小。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.