'how to rotate the multicategory axis in plotly?

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Bar(
    x = [['First', 'First', 'Second', 'Second'],
    ["A", "B", "A", "B"]],
    y = [2, 3, 1, 5],
    name = "Adults",
))

fig.add_trace(go.Bar(
    x = [['First', 'First', 'Second', 'Second'],
    ["A", "B", "A", "B"]],
    y = [8, 3, 6, 5],
    name = "Children",
))

fig.update_layout(title_text="Multi-category axis")

fig.show()

my desired output is to rotate the plot's 'first' and 'second' category to 45 degree

rotate the bottom category axis



Solution 1:[1]

I used two methods to prevent category text overflow desired space when the category text is too long:

  1. use smaller font size: adjust the tickfont param of the update_xaxes call

    fig.update_xaxes(tickangle=45, tickfont=dict(size=8), dividerwidth=2)

  2. use
    in the category text: if dataframe is used for instance,

    df = df.rename(columns=lambda x: (x[:truncate_len] + '
    ' + x[truncate_len:]) if (len(x) > truncate_len) else x)

truncate_len is the desired text space

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yangbin Zhou