1

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!

Petr_k
  • 13
  • 3
  • 1
    Please provide a self-contained example we can try. Otherwise, its pretty hard to understand what the problem is, particularly as you have shown no images. Note however, that z-order doesn't apply to axes, and that the axes will be drawn in the order that they are created. So `ax2.set_order` has no effect so far as I know. – Jody Klymak Jan 03 '22 at 15:24
  • Sorry, the image wasn't uploaded. I did it again. – Petr_k Jan 04 '22 at 10:27
  • SOrry, as the above links point out, I am wrong - axes do have zorder. However, the thing to keep in mind is that their z-order is different from the artists in the axes. So you problem above is that you have set ax1 to have a higher zorder than ax2, so ax1 gets drawn after ax2 – Jody Klymak Jan 04 '22 at 14:21

0 Answers0