使用DataFrame.plot

时间:2017-06-09 12:33:45

标签: python pandas dataframe plot

我使用python绘制pandas DataFrame

我设置了这样的绘图颜色:

allDf = pd.DataFrame({
    'x':[0,1,2,4,7,6],
    'y':[0,3,2,4,5,7],
    'a':[1,1,1,0,0,0],
    'c':['red','green','blue','red','green','blue']
},index = ['p1','p2','p3','p4','p5','p6'])

allDf.plot(kind='scatter',x='x',y='y',c='c')
plt.show()

然而它不起作用(每个点都有蓝色)

如果我像这样更改了DataFrame的定义

'c':[1,2,1,2,1,2]

它看起来颜色但只有黑色和白色,我想使用蓝色,红色等......

2 个答案:

答案 0 :(得分:2)

Replace it by:

allDf.plot(kind='scatter',x='x',y='y',c=allDf.c)

Output:

enter image description here

答案 1 :(得分:0)

The c argument of pandas.DataFrame.plot is in this case passed through literally so everything will have the color 'c' (cyan).

You need to pass your column directly:

allDf.plot(kind='scatter', x='x', y='y', c=allDf['c'])

enter image description here

It's a bit weird and not well documented when the c parameter will use the column and when it will use the literal value. So in this case it's probably best to provide the "colors" explicitly. You might want to take a look at the source code in case you're interested to debug what is happening there.