我正在尝试在列中绘制值,并通过从外部手动传递颜色来基于不同列中的组为它们着色。我将蓝色分配给测试,将红色分配给训练组。但是下面的代码仅以蓝色绘制值。
import datetime
import pandas as pd
dft2 = 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']])
答案 0 :(得分:0)
您可能需要散点图版本
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']])