簡體   English   中英

如何在彼此下方的兩個不同軸上對齊條形圖和折線圖的 matplotlib x-ticks?

[英]How do I align matplotlib x-ticks of a bar chart and line chart in two different axes underneath of each other?

我正在嘗試從 matplotlib 中的兩個圖表中對齊我的 x-ticks。 問題似乎與一個圖表有關,它是一條線 plot,另一個是條形圖。 通過搜索 function 找不到解決方案。 如果我錯過了早些時候在某處發布的解決方案,請指導我。

我正在談論的情節圖片

這是我的代碼(對於德語變量名來說很抱歉^^):

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

因此,在 JohanC 的提示下,我將代碼更改為以下,現在 xticks 已對齊

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM