简体   繁体   English

如何旋转 plotly 中的多类别轴?

[英]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我想要的 output 是将地块的“第一”和“第二”类别旋转 45 度

rotate the bottom category axis旋转底部分类轴

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使用较小的字体大小:调整 update_xaxes 调用的 tickfont 参数

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

  2. use利用
    in the category text: if dataframe is used for instance,在类别文本中:例如,如果使用 dataframe,

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

truncate_len is the desired text space truncate_len 是所需的文本空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM