我有两个数据框,其中包含多个列。我将df#1绘制为堆叠条形图,并将df#2覆盖为折线图。如果df#1中的数据序列存在于df#2中,我希望条形图和折线的颜色相同,以便于比较。我将如何做到这一点?下面,我希望'FO'的颜色相同。
Dataframe :
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
下面是我的代码:
# Build a union of the dataframe columns so the line and bar series can have the same color
# for ease of comparison
df_Plot1 = vg.dc_dF_Groups[employee]
cols = df_Plot.columns.union(df_Plot1.columns)
cmap = matplotlib.colormaps['Set1']
colors = dict(zip(cols, cmap.colors))
# Plot the employee's charges
df_Plot.iloc[0:plotFinish].reindex(columns=cols).plot.bar(ax=ax1, color=df_Plot.columns.map(colors), rot=45, legend=True, xlabel='Date', ylabel='Actuals', alpha=0.5, stacked=True)
# Plot the employee's group allocations
df_Plot1.iloc[0:plotFinish].reindex(columns=cols).plot.line(ax=ax2, color=df_Plot1.columns.map(colors), ylabel='Allocation', legend=True, linewidth=3)
ax1.legend(title='Actuals', bbox_to_anchor=(1.05,1.0), loc='upper left', ncol=1)
ax2.legend(title='Allocations', bbox_to_anchor=(1.05,0.5), loc='upper left', ncol=1)
fig.suptitle(employee)
1条答案
按热度按时间56lgkhnf1#
你可以得到列的
union
和reindex
:输出:
或者,创建自定义颜色列表并从列名Map它们:
输出: