1

Let's say I have the following dataset:

d = {'Team': ['Duke', 'LSU'], 'Wins': [20, 18], 'Losses' : [5, 7], 'Conference' : ['ACC', 'SEC']}
df = pd.DataFrame(data=d)
df

    Team    Wins   Losses   Conference
0   Duke     20      5          ACC
1   LSU      18      7          SEC

Then I make a scatterplot of it

plt.plot(d['Losses'], d['Wins'], 'o')

I would like to color code my scatter plot by Conference. More specifically, I would like to only color SEC teams red, while all other data points are the default blue. Additionally, how would I go about coloring just Duke red, while every other datapoint is blue? I have about 200 teams in my dataset. How would I ago about doing this? Thanks!

1 Answer 1

5

IIUC you can try

import pandas as pd
import numpy as np

d = {'Team': ['Duke', 'LSU'], 
     'Wins': [20, 18], 
     'Losses' : [5, 7], 
     'Conference' : ['ACC', 'SEC']}
df = pd.DataFrame(data=d)

df["color"] = np.where(df["Conference"]=="SEC", "red", "blue")

df.plot(x='Losses', y='Wins', kind="scatter", color=df["color"]);

If then you want to use the same logic for Duke you just need to change the line with np.where accordingly.

Update For this particular case I think you should have a look at plotly

import plotly.express as px
px.scatter(df,x="Losses", y="Wins", color="Conference", hover_name="Team")

enter image description here

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

7 Comments

When I use the np.where, it just assigns blue for every row. Also, I wouldn't want to color code it by conference, I would just like to have one conference be a different color than all other data points (so essentially around 10 specific points would be red while 100 others are a different color).
If you have a conference called "SEC" np.where should assign red to it. Please check if you have whitespaces. I'm not sure to understand what do you mean. Do you want "SEC" to be red and a different color for each conference?
When you reproduce all my code including the definition of df do you have SEC in red and the other in blue?
Which version of pandas are you using?
I got it, the correct syntax is scatter_kws={'facecolors':df['color']}. Your np.where helped tremendously. Thank you so much.
|

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.