I have two data frames with several columns. I plot df#1 as as stacked bar plot and overlay df #2 as a line plot. If a data series in df#1 is present in df#2 I want the color of the bar segment & line to be the same, for ease of comparison. How would I do that? Below, I would want the colors of ‘FO’ to be the same.
Data Frames:
df#1
FO MI
Date
08-Jan-23 0.4925 0.0000
15-Jan-23 0.9075 0.0000
22-Jan-23 0.9750 0.0250
29-Jan-23 0.9625 0.0375
05-Feb-23 0.9825 0.0000
df#2
FO AR
Jan-23 1.0 1.1
Feb-23 1.0 2.2
>Solution :
You can get the column’s union and reindex:
cols = df1.columns.union(df2.columns)
fig, axes = plt.subplots(ncols=2)
df1.reindex(columns=cols).plot.bar(stacked=True, ax=axes[0])
df2.reindex(columns=cols).plot.line(ax=axes[1])
Output:
