Colors in a 3-variable Plot
We also can change colors for the 3rd variable manually. To do that, we need to create a kind of a palette.
To set colors for a 3-variable plot:
It is important to create a new color palette for the hue variable and add this palette as an argument in the plot function.
The short list of the matplotlib
colors: blue, green, red, green/blue, yellow, purple, white, black, etc.
Let's change gender
colors in the Tips problem!
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Importing libraries needed
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# Reading the file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/bilibili.csv')
# Setting colors for the hue variable
hue_colors = {'woman':'red', 'man':'black'}
# Creating the 3-variable scatterplot
sns.scatterplot(x = 'bill', y = 'tips', hue = 'gender', data=df, palette = hue_colors)
# Showing the plot
plt.show()
12345678910111213141516# Importing libraries needed import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # Reading the file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/bilibili.csv') # Setting colors for the hue variable hue_colors = {'woman':'red', 'man':'black'} # Creating the 3-variable scatterplot sns.scatterplot(x = 'bill', y = 'tips', hue = 'gender', data=df, palette = hue_colors) # Showing the plot plt.show()
Tehtävä
Swipe to start coding
- Set the
'blue'
color forbreakfast
,'purple'
forlunch
,'green'
fordinner
for the hue value. - Show the plot.
Ratkaisu
99
1
2
3
4
5
6
7
8
9
10
11
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/bilibili.csv')
hue_colors = {'breakfast' : 'blue', 'lunch' : 'purple', 'dinner' : 'green'}
sns.scatterplot(x = 'bill', y = 'tips', hue = 'daytime', data = df, palette = hue_colors)
plt.show()
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 2. Luku 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Import libraries needed
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# Reading the file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/bilibili.csv')
# Set the palette for daytimes
hue_colors = {'breakfast' : '___', '___' : '___', '___ ' : '___'}
# Create the 3-variable scatterplot
sns.scatterplot(x = 'bill', y = 'tips', hue = 'daytime', data = df, palette = hue_colors)
# Show the plot
___
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme