DejaVuMan
DejaVuMan

Reputation: 35

How do I have certain points on a scatterplot with Myplotlib be different colors?

I'm working with a dataset regarding the survivors on the Titanic, where I'm trying to show the relationship between Age of passengers and the fare they paid.

This is what the data is currently formatted as: Titanic Passenger data format: head

from here, it was fairly easy to make a simple scatterplot, like so: Scatterplot showing the relationship between Age and Fare

However, I am curious as to if there is a way to set the color of some of the points to be different based on the sex from the dataset. Most examples I have seen across the internet focus on how to change the color for two separate data sets. I initially tried to use an if statement to change the color depending on sex, but that didn't work for me the way I hoped it would.

Upvotes: 0

Views: 80

Answers (2)

DejaVuMan
DejaVuMan

Reputation: 35

One potential solution I came up to after pondering a bit could potentially look like this as well:

enter image description here

The problem with this solution is you have to add more variables, which isn't ideal, and the results stack over each other a bit making it harder to see the data trends.

Upvotes: 0

BigBen
BigBen

Reputation: 49998

Perhaps much easier with :

import seaborn as sns

data = sns.load_dataset('titanic')
sns.scatterplot('age', 'fare', data=data, hue='sex')

enter image description here

Upvotes: 1

Related Questions