user13747020
user13747020

Reputation:

Multiple line text labels plotly

I have some bar charts with some longer labels and I'd like to stack the labels with multiple lines of text.

So "A very long sentence that needs broken up" appears as

A very long sentence that needs broken up.

I'd like to make the change ideally in plotly and not in my pandas dataframe. But if that is the easier way to do that please let me know. Here is an MRE.

import plotly.graph_objects as go

x1 = ['A very long sentence that needs broken up', 'Regular Length', "Cory", "Dave"]

y1 = [20, 30, 25, 40]

fig = go.Figure()

fig.add_trace(go.Bar(x=x1, y=y1))

Upvotes: 1

Views: 5264

Answers (2)

Anurag Reddy
Anurag Reddy

Reputation: 1215

so you can do this by adding a <br> element in the input text. One more way is to hardset the tickwidth using

fig.update_layout(
    xaxis = {
    'tickwidth' : %number in px
    }
)

The sample output in 2 lines

You can find the documentation here

Alternate way is to check the ticklen or tickformat parameters also

Upvotes: 2

r-beginners
r-beginners

Reputation: 35115

In the case of plotly, you can control the labels with HTML, so you can do this with the following code.

fig.update_xaxes(tickmode='array',
                tickvals=[0,1,2,3],
                ticktext=['A very long sentence</br></br> that needs broken up', 'Regular Length', "Cory", "Dave"])

Upvotes: 1

Related Questions