Python 绘制颜色图检索colormap堆叠条形图使用的颜色

Python 绘制颜色图检索colormap堆叠条形图使用的颜色,python,pandas,matplotlib,Python,Pandas,Matplotlib,我使用下面的一行,使用特定的颜色映射集1从数据帧绘制堆叠条形图。我想在另一个条形图中使用此图中使用的同一组颜色 pl=df.plot(ax=ax1, kind='bar', stacked=True, colormap="Set1", width=0.5) 这里是另一个绘图,我希望能够使用与第一个绘图完全相同的颜色 for index, row in df2.iterrows(): ax2=row.plot(ax=ax2, kind='bar', color=#same colors

我使用下面的一行,使用特定的颜色映射集1从数据帧绘制堆叠条形图。我想在另一个条形图中使用此图中使用的同一组颜色

pl=df.plot(ax=ax1, kind='bar', stacked=True, colormap="Set1", width=0.5)
这里是另一个绘图,我希望能够使用与第一个绘图完全相同的颜色

for index, row in df2.iterrows():
    ax2=row.plot(ax=ax2, kind='bar', color=#same colors used up, width=0.5

解决方案是按如下所示从颜色贴图构建相同的颜色列表,并在两个图中使用该颜色列表

    rng = np.arange(len(df.columns))/(len(df.columns))
    colors = plt.cm.Set1(rng)
    pl=df.plot(ax=ax1, kind='bar', stacked=True, color=colors, width=0.5)
    ax2=row.plot(ax=ax2, kind='bar', color=colors, width=0.5)

对于我(Pandas版本0.23.1),
row.plot
有一个
colormap=
参数。它只使用贴图中的第一种颜色。