adjust the size of the text label in plotly

Viewed 2036

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

1 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()
Related