I am trying to create two line plots with errorbar using the primary and secondary y-axis on the same figure. I want to plot the errorbar line in such a way that it does not overlap the markers on the line plots. zorder is not working here properly. The errorbar corresponding to the secondary y-axis always overlaps the markers of the primary y-axis line plot but the reverse is not showing.
data = {
'A': [439, 1263, 1631, 2059],
'A-std': [255, 829, 1058, 1365],
'B': [0.16, 0.39, 0.47, 0.58],
'B-std': [0.11, 0.18, 0.19, 0.18]}
df = pd.DataFrame(data)
fig, ax = plt.subplots(figsize=(5.0,4.0))
ccn_plot = ax.errorbar(df.index, df[df.columns[0]],
yerr=df[df.columns[1]], label='Blue', color= 'blue',
linewidth=4.0, elinewidth=2.0, ls='solid', capsize =5.0, zorder = 0)
ax1 = ax.twinx()
## Plotting AF data on the secondary y-axis
af_plot = ax1.errorbar(df.index, df[df.columns[2]],
yerr=df[df.columns[3]], label = 'Red', color = 'red',
linewidth = 4.0, elinewidth=2.0, ls='solid', capsize =5.0, zorder = 0)
ax.scatter(df.index, df[df.columns[0]],
marker='s', s=50, c = 'blue',
edgecolors='black',
zorder=10)
ax1.scatter(df.index, df[df.columns[2]],
marker='o', s=50, c = 'red',
edgecolors='black',
zorder=10)
## Collect legend handles and labels from both axes
primary_handles, primary_labels = ax.get_legend_handles_labels() # from primary y-axes
sec_handles, sec_labels = ax1.get_legend_handles_labels() # from secondary y-axe
## Combine handles and labels
all_handles = primary_handles + sec_handles
all_labels = primary_labels + sec_labels
ax.legend(all_handles, all_labels)
ax.set_ylabel('Blue')
ax1.set_ylabel('Red')
[The code gives the following output:][1]
I do not understand why zorder is showing such strenge behaviour. I want to plot the errorbar line in such a way that it does not overlap the markers on any of the line plots. Any help is highly appriciated. Thanks in advance.