0

i have a code like that

import plotly.express as px

df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig = px.bar(df, y='pop', x='country', text='pop')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
fig.show()

and it builds me this diagram enter image description here

how to make max length on colums name?

1 Answers1

2

I think there is a very similar question & answer here. Adapted to your case, you could chose to keep only the first n letters of the column name. It would look like the following:

import plotly.express as px
# Data
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
# Figure
fig = px.bar(df, y='pop', x='country', text='pop')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')

# The modification: overwrite tick labels    
# Choose the number of letters to keep
n = 7
fig.update_layout(
    xaxis = {
     'tickmode': 'array',
     'tickvals': df.country.unique().tolist(),
     'ticktext': [i[:n] for i in df.country.unique().tolist()],
    }
)
fig.show()

For n=7 it produces: enter image description here

Mat.B
  • 336
  • 2
  • 8