Python 使用默认颜色映射打印数据帧的列,但使用不同颜色的列除外

Python 使用默认颜色映射打印数据帧的列,但使用不同颜色的列除外,python,pandas,matplotlib,plot,colors,Python,Pandas,Matplotlib,Plot,Colors,我在一个数据框中有一大堆列,我正在这样绘制: df.xs('Mean',level=1).cumsum().plot(cmap='winter')。 这将绘制我所有列的平均值随时间的累积和。(0级多重索引是时间)但是,我想用不同的颜色突出显示绘图中的特定列,但不确定如何进行。大概是这样的: df.xs('Mean',level=1).cumsum().plot(cmap='winter',color={'my'u specific_column':'red'})。 它将沿着'winter'cma

我在一个数据框中有一大堆列,我正在这样绘制:

df.xs('Mean',level=1).cumsum().plot(cmap='winter')。

这将绘制我所有列的平均值随时间的累积和。(0级多重索引是时间)但是,我想用不同的颜色突出显示绘图中的特定列,但不确定如何进行。大概是这样的:

df.xs('Mean',level=1).cumsum().plot(cmap='winter',color={'my'u specific_column':'red'})。

它将沿着
'winter'
cmap光谱绘制所有其他列(蓝绿色到蓝色),除了
'my_specific_column'
,该列将为红色


有什么想法吗?

我认为简单的选择是绘制两次

to_draw = df.xs('Mean', level=1).cumsum()

fig, ax = plt.subplots()

to_draw.drop('my_specific_column', axis=1).plot(cmap='winter', ax=ax)
to_draw['my_specific_column'].plot(color='red', ax=ax, label='my_specific_column')

ax.legend()

这是另一个解决办法。首先,我们从colormap中获取颜色,并将它们设置为要绘制的每一列。我们用我们想要的颜色来更新字典。最后,我们设置了一个应用于绘图的地图

import matplotlib
cmap = matplotlib.cm.get_cmap('winter')

# get colors for each of the columns
columns = df.columns.values
colors = [cmap(i) for i in range(1, len(columns)+1)]

# build a dictionary with them
COLORS_DICT = dict(zip(columns, colors))

# update with the column we want to change
COLORS_DICT.update({"my_specific_column": 'red'})
COLORS_MAP = list(map(lambda x : COLORS_DICT[x], COLORS_DICT))
df[columns].plot(color=COLORS_MAP)