adjust the size of the text label in plotly

Question:

Im trying to adjust the text size accoridng to country size, so the text will be inside the boardes of the copuntry.

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.show()

For the visualiztion:

enter image description here

The text for orange country is inside the boardes of the country but the text to label the blue countrry is bigger.

Best would be to adjust the size so it will not exceed the boardes of the country

Asked By: Greencolor

||

Answers:

You can set the font size using the update_layout function and specifying the font’s size by passing the dictionary in the font parameter.

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()
Answered By: Trishant Pahwa

Text and Annotations in Python

You can Controll Text Size with uniformtext like this

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

or
Controll Maximum Text Size

fig.update_traces(textposition='inside', textfont_size=14)

These ways you don’t change axis and label font sizes.

Answered By: HR85
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.