How Do I Align Matplotlib X-ticks Of A Bar Chart And Line Chart In Two Different Axes Underneath Of Each Other?
I'm trying to align my x-ticks from two graphs in matplotlib. The problem seems to be connected to the one graph being a line plot and the other one being a bar chart. I can't find
Solution 1:
So with the tipp of JohanC I changend my code to following and now the xticks are aligned
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()
Post a Comment for "How Do I Align Matplotlib X-ticks Of A Bar Chart And Line Chart In Two Different Axes Underneath Of Each Other?"