1

What I'd like to do is create a matlab scatter plot as below, but color the dots according to the the string in column C, for example, yes = blue and no = red.

 import matplotlib.pyplot as plt
 A = pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6]), ('C', ['yes', 'yes', 'no'])])


plt.scatter(A['A'], A['B'])

2 Answers 2

1

You can create a dictionary of (value: color) pairs and use those values as color argument on plt.scatter using a list comprehension.

colors = {'yes': 'b', 'no': 'r'}

plt.scatter(A['A'], A['B'], color=[colors[r] for r in A['C']])

EDIT: Preferred way to add legend is to iterate over unique classes in your dataset and use legend argument. So we can iterate over the dataframe grouped by C and plot them individually with label arguments. And finally call plt.legend() to show it.

colors = {'yes': 'b', 'no': 'r'}

for idx, row in A.groupby('C'):
    plt.scatter(row['A'], row['B'], 
                color=[colors[r] for r in row['C']], 
                label=idx)

plt.legend()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Is there any way to create a legend with this info as well?
@Sparky_47 I'm sure there are other ways but preferred method from matplotlib is to iterate over unique classes and use label argument. I'll edit the answer with legend for each unique class.
1

Funny one-liner:

plt.scatter(x="A",y="B",c="C",
            data=df.apply(lambda x: [x.A,x.B,chr(int(1.6*ord(x.C[1])-63.6))], axis=1))

enter image description here

1 Comment

Heh, +1 on the creativity. I would still prefer plt.scatter(A['A'], A['B'], color=['b' if r=='yes' else 'r' for r in A['C']]) for a one liner (and be boring) but that is pretty cool.

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.