I have the following code for plotting two overlay bar plots in Pandas, one of which is semitransparent and the other has white edgecolor:
from io import StringIO
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["patch.force_edgecolor"] = True
txt = u'''Category COLUMN1 COLUMN2 COLUMN3
A 0.5 3 Cat1
B 0.3 5 Cat1
C 0.7 4 Cat1
A 0.4 3 Cat2
B 0.8 5 Cat2
C 0.3 4 Cat2
A 0.5 3 Cat3
B 0.3 5 Cat3
C 0.7 4 Cat3
A 0.4 3 Cat4
B 0.8 5 Cat4
C 0.3 4 Cat4
'''
df = pd.read_table(StringIO(txt), sep="\s+")
order = ['Cat1', 'Cat2', 'Cat3', 'Cat4']
fig, ax = plt.subplots()
ax2 = ax.twinx()
col1 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN2').reindex(order)
col1.plot(kind='bar', ax=ax2, legend=False, alpha=0.5, edgecolor='Black', lw=3)
col2 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN1').reindex(order)
col2.plot(kind='bar', ax=ax, edgecolor='antiquewhite', lw=3, color='black', width=0.2)
ax2.xaxis.set_visible(False)
ax.set_ylim(ax2.get_ylim())
for bbar, cbar in zip(ax.patches, ax2.patches):
cpos = cbar.get_x()+cbar.get_width()/2.
bpos = cpos-bbar.get_width()/2.
bbar.set_x(bpos)
plt.show()
The result looks as follows where as one can clearly see the white edgecolor can barely be seen because the black bars are plotted in the background/under the semitransparent bars:
Adjusting the code as follows returns the same result as shown in the image above:
col1 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN1').reindex(order)
col1.plot(kind='bar', ax=ax, edgecolor='antiquewhite', lw=3, color='black', width=0.2)
col2 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN2').reindex(order)
col2.plot(kind='bar', ax=ax2, legend=False, alpha=0.5, edgecolor='Black', lw=3)
If the black bars are plotted on top/last, the white edgecolor should show more clearly.
I tried adjusting the code to this:
col1 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN2').reindex(order)
col1.plot(kind='bar', ax=ax2, legend=False, edgecolor='Black', lw=3)
col2 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN1').reindex(order)
col2.plot(kind='bar', ax=ax, edgecolor='antiquewhite', alpha=0.5, lw=3, color='black', width=0.2)
But that clearly shows that the black bars are plotted in the background (again, it seems like they insist on wanting to be in the background, why?):
So how can one plot the black bars so that they appear on top with clearly distinguishable white edgecolor?