使用 plotly 格式化多级轴标签

Formatting Multilevel Axes Labels with plotly

如何更改次要 x 轴标签的大小或旋转,以便在将其导出为静态图像时它不与线条重叠?

# packages
import pandas as pd
import plotly.graph_objects as go

df_RampUp_2030 = pd.read_excel('RampUp.xlsx', index_col=[0,1])

# define xlabels
xlabels = [list(df_RampUp_2030.index.get_level_values(0)),
           list(df_RampUp_2030.index.get_level_values(1)),
           df_RampUp_2030.index]

fig = go.Figure()

# create traces
for idx, tech in enumerate(df_RampUp_2030.columns):
    fig.add_trace(
        go.Bar(
            y=df_RampUp_2030[tech],
            x=xlabels,
            name=tech,
            showlegend=True,
            width=0.5,
        )
    )

# specify layout
fig.update_layout(
    yaxis=dict(
        titlefont_size=16,
        tickfont_size=12,
        title="Mio. Stk.",
        showspikes=True),
    xaxis=dict(
        titlefont_size=16,
        tickfont_size=12),
    title_text='Fahrzeughochlauf<br>alternativer Antriebstechnologien bis 2030',
    template='plotly_white',
    barmode='stack',
)

fig.show()

在生成的图像中,标签 NPE 2018eMobil 2050 不适合给定的 space。是否可以只旋转这两个标签?如果不是,如何更改二级标签的字体大小?

数据:

+-----------------+-------------------+-------+-------+-------+------+------------------+
|     Studie      |     Szenario      |  BEV  | PHEV  |  FCV  | REEV | Elektrofahrzeuge |
+-----------------+-------------------+-------+-------+-------+------+------------------+
| dena            | RF                |   1.9 |   3.2 |   0.9 |      |                  |
| dena            | TM                |   5.6 |  16.4 |   2.2 |      |                  |
| dena            | EL                |  13.3 |  11.0 |   1.2 |      |                  |
| BCG             | RF                |   2.0 |   7.0 |   0.0 |      |                  |
| BCG             | 80                |   4.0 |   6.0 |   0.0 |      |                  |
| BCG             | 95                |   4.0 |   6.0 |   0.0 |      |                  |
| NPE 2018        | konservativ       |       |       |       |      |              4.2 |
| NPE 2018        | optimistisch      |       |       |       |      |              7.0 |
| RENEWBILITY III | Basis             | 0.449 | 2.513 | 0.004 |      |                  |
| RENEWBILITY III | Fokus Kraftstoffe | 0.456 |  2.55 |   0.0 |      |                  |
| RENEWBILITY III | Pkw-Maut          | 1.004 | 6.277 | 0.007 |      |                  |
| RENEWBILITY III | ohne O-LKW        | 1.037 | 6.485 |   0.0 |      |                  |
| RENEWBILITY III | Effizienz Plus    | 1.037 | 6.485 | 0.008 |      |                  |
| RENEWBILITY III | Effizienz         | 1.001 | 5.387 | 0.008 |      |                  |
| eMobil 2050     | Regional eMobil   |   3.0 |   1.0 |       |  2.0 |                  |
| eMobil 2050     | Grenzenlos eMobil |   2.0 |   2.0 |       |  2.0 |                  |
+-----------------+-------------------+-------+-------+-------+------+------------------+

我修正了两点。首先,我将图例移到了标题的顶部,以便在右侧为它腾出空间。接下来,我通过手动设置图像大小而不是自动设置来增加宽度。

# packages
import pandas as pd
import plotly.graph_objects as go

# df_RampUp_2030 = pd.read_excel('RampUp.xlsx', index_col=[0,1])

# define xlabels
xlabels = [list(df_RampUp_2030.index.get_level_values(0)),
           list(df_RampUp_2030.index.get_level_values(1)),
           df_RampUp_2030.index]

fig = go.Figure()

# create traces
for idx, tech in enumerate(df_RampUp_2030.columns):
    fig.add_trace(
        go.Bar(
            y=df_RampUp_2030[tech],
            x=xlabels,
            name=tech,
            showlegend=True,
            width=0.5,
        )
    )

# specify layout
fig.update_layout(
    autosize=False,
    width=1000,
    height=500,
    yaxis=dict(
        titlefont_size=16,
        tickfont_size=12,
        title="Mio. Stk.",
        showspikes=True),
    xaxis=dict(
        titlefont_size=16,
        tickfont_size=12),
    title_text='Fahrzeughochlauf<br>alternativer Antriebstechnologien bis 2030',
    template='plotly_white',
    barmode='stack',
)

fig.update_layout(legend=dict(
    orientation="h",
    yanchor="bottom",
    y=1.02,
    xanchor="right",
    x=1
))

fig.show()