我试图在列中绘制值,并通过从外部手动传递颜色来根据不同列中的组对它们进行着色。我将蓝色分配给测试组,将红色分配给训练组。但以下代码仅以蓝色绘制值。import datetimeimport pandas as pddft2 = pd.DataFrame({'A': [1,3,3,4,1,3,3,4], 'B': [2015, 2016, 2017,2018, 2019, 2020, 2021, 2022], 'C': pd.Categorical(["test", "train", "test", "train","train","train","train","train"])})dft2 = dft2.set_index('B')colors = {'test': 'b', 'train': 'r'}dft2['A'].plot(figsize=(24,3), rot=90, color=[colors[i] for i in dft2['C']])
1 回答
www说
TA贡献1775条经验 获得超8个赞
您可能需要散点图版本
import datetime
import pandas as pd
dft2 = pd.DataFrame({'A': [1,3,3,4],
'B': [2015, 2016, 2017,2018],
'C': pd.Series(1, index=list(range(4)), dtype='float32'),
'D': np.array([3] * 4, dtype='int32'),
'E': pd.Categorical(["test", "train", "test", "train"]),
'F': 'foo'})
dft2.plot.scatter(x='B',y='A',c=['b' if i== 'test' else 'r' for i in dft2['E']])
添加回答
举报
0/150
提交
取消