I have read a lot of articles here regarding this problem. However, none of the solutions helped me with my problem. So please allow me to reopen this problem.
I am trying to display a line chart and a bar chart in one graph. See the image here. However, I can't get the horizontal grid "under" both graphs even though I have the zorder parameter set correctly. I read somewhere that you just need to try different values for zorder. I have tried hundreds of them, but failed.
I won't write all the code here, just the parts that are relevant:
#LINE CHART
fig, ax1 = plt.subplots(figsize=(12,6))
ax1.plot(mdates.date2num(df_exchangerate_FINAL['Date']), df_exchangerate_FINAL["LINE"])
ax1.grid(axis='y', color='gray', visible = True, zorder = 0, linewidth=0.4)
ax1.set_zorder(3)
ax1.patch.set_visible(False)
#BAR CHART
ax2 = ax1.twinx()
rects1 = ax2.bar(mdates.date2num(df_operations_FINAL["Date"]) - X, df_operations_FINAL["BAR1"])
rects2 = ax2.bar(mdates.date2num(df_operations_FINAL["Date"]) + X, df_operations_FINAL["BAR2"])
all_dates = sorted(df_operations_FINAL['Date'][::F].tolist())
all_dates = mdates.date2num(all_dates)
ax2.set_xticks(all_dates)
ax2.set_xticklabels([mdates.num2date(d).strftime('%#m/%y') for d in all_dates])
ax2.set_zorder(2)
The desired chart for me is one where there will be a grid in the back, then a bar chart and a line chart in the front.
This is quite a critical problem for me, so I would be very grateful for any help --> what should I use and where in the code should I place it? Many thanks!