使用绘图格式化多级轴标签

问题描述

当我将其作为静态图像导出时,如何更改辅助xaxis标签的大小或旋转,以使其与行不重叠?

# 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 2018 eMobil 2050 不适合给定空间。是否可以仅旋转这两个标签?如果没有,如何更改辅助标签的字体大小?

enter image description here

数据:

+-----------------+-------------------+-------+-------+-------+------+------------------+
|     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()

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...