0

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 a solution through the search function. Should I have missed a solution posted earlier somewhere, please direct me to it.

Picture of the Plot I'm talking about

This is my code(sry for the German variable names^^):

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()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • The usual way would be to create the axes via `fig, (ax1, ax2) = plt.subplots(nrows=2, ..., gridspec={....}, sharex=True)`. Alternatively, you could set the xlims equal for both, e.g. `ax1.set_xlim(0.5,10.5)` and `ax2.set_xlim(0.5,10.5)` – JohanC Jan 02 '21 at 00:10

1 Answers1

0

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