2

I am using python to plot a pandas DataFrame

I set the color for plotting like this:

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()

However it doesn't work (every point has a blue color)

If I changed the definition of DataFrame like this

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

It appears color but only black and white, I want to use blue, red and more...

0

2 Answers 2

2

Replace it by:

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

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you it works well, but I thought I should pass the name of lines as argumens to plot(). like 'x' and 'y' . Why not like allDf.plot(kind='scatter',x=allDf.x, y=allDf.y, c=allDf.c)
The DataFrame.plot documentation explains what kind of arguments are allowed. For x and y it's "label or position" for most other parameters it simply passes them to matplotlib (so you can't specify them by "label or position").
It's understandable mistake, it all come with practice. :D Like MSeifert said below, you are litterally asking him to use the color 'c':cyan : )
I see it's the specification of plot(). I need to get used to it more.
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.

1 Comment

I think the c stand for cyan. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.