如何将条形图和折线图的matplotlib XTICK在彼此下方的两个不同轴上对齐?

2024-09-30 00:26:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试对齐matplotlib中两个图形的x记号。问题似乎与一个图形是直线图,另一个是条形图有关。我无法通过搜索功能找到解决方案。如果我错过了早些时候发布的解决方案,请告诉我

Picture of the Plot I'm talking about

这是我的代码(sry表示德语变量名^^):

plot_farben = {"cyan": "#55CBCD",
               "rot": "#FF968A",
               "grün": "#97C1A9",
               "orange": "#FFC8A2",
               "gelb": "#FFFFB5"}

gridsize = (3, 2)
fig = plt.figure(figsize=(12, 8))
ax1 = plt.subplot2grid(gridsize, (0, 0), colspan=2, rowspan=2)
ax2 = plt.subplot2grid(gridsize, (2, 0), colspan=2)


ax1.fill_between(zeitwerte.index, leistungsband["P1_low"], leistungsband["P1_high"], alpha=0.2, color=plot_farben["cyan"])
ax1.plot(zeitwerte.index, zeitwerte["Solarleistung[kW]"], color=plot_farben["orange"])
ax1.plot(zeitwerte.index, zeitwerte["P1[kW]"], color=plot_farben["cyan"], linestyle='--')
ax1.plot(zeitwerte.index, p1["P1_opt"], color=plot_farben["rot"])
ax1.legend(['Solarleistung', 'P1_Ausgangslastprofil', 'P1_optimiert', 'P1_Flexiband'], loc=0, shadow=True)
ax1.set_title('Laststeuerung mit ToU Tarifen', fontsize=18)
ax1.set_ylabel('Leistung / kW')
ax1.grid(linestyle='--')
ax1.set_xticks(zeitwerte.index)

ax2.bar(zeitwerte.index, zeitwerte["Strompreis[€/kW]"], color=plot_farben["grün"])
ax2.legend(['ToU Strompreis'], loc=0, shadow=True)
ax2.set_xlabel('Zeit / h')
ax2.set_ylabel('Strompreis [€/kWh]')
ax2.grid(linestyle='--')
ax2.set_xticks(zeitwerte.index)

for index, value in enumerate(zeitwerte["Strompreis[€/kW]"]):
    plt.text(index + 0.9, value - 0.08, str(value), color="white")

plt.show()

Tags: indexplotpltcolorsetkwp1cyan
1条回答
网友
1楼 · 发布于 2024-09-30 00:26:21

因此,随着约翰的tipp,我将代码更改为followind,现在XTICK已对齐

plot_farben = {"cyan": "#55CBCD",
               "rot": "#FF968A",
               "grün": "#97C1A9",
               "orange": "#FFC8A2",
               "gelb": "#FFFFB5"}


fig = plt.figure(figsize=(12, 8))
gs = gridspec.GridSpec(3, 1, figure=fig)


ax1 = fig.add_subplot(gs[0:2, 0])
ax1.fill_between(zeitwerte.index, leistungsband["P1_low"], leistungsband["P1_high"], alpha=0.2, color=plot_farben["cyan"])
ax1.plot(zeitwerte.index, zeitwerte["Solarleistung[kW]"], color=plot_farben["orange"])
ax1.plot(zeitwerte.index, zeitwerte["P1[kW]"], color=plot_farben["cyan"], linestyle=' ')
ax1.plot(zeitwerte.index, p1["P1_opt"], color=plot_farben["rot"])
ax1.legend(['Solarleistung', 'P1_Ausgangslastprofil', 'P1_optimiert', 'P1_Flexiband'], loc=0, shadow=True)
ax1.set_title('Laststeuerung mit ToU Tarifen', fontsize=18)
ax1.set_ylabel('Leistung / kW')
ax1.grid(linestyle=' ')
ax1.set_xticks(zeitwerte.index)

ax2 = fig.add_subplot(gs[2, 0], sharex=ax1)
ax2.bar(zeitwerte.index, zeitwerte["Strompreis[€/kW]"], color=plot_farben["grün"])
ax2.legend(['ToU Strompreis'], loc=0, shadow=True)
ax2.set_xlabel('Zeit / h')
ax2.set_ylabel('Strompreis [€/kWh]')
ax2.grid(linestyle=' ')
ax2.set_xticks(zeitwerte.index)

for index, value in enumerate(zeitwerte["Strompreis[€/kW]"]):
    plt.text(index + 0.9, value - 0.08, str(value), color="white")

plt.show()

相关问题 更多 >

    热门问题