3-variable Lineplot
With the help of the seaborn
, we can view the dynamics of the population, for example, relative to each season during 10 years by adding the third variable to our lineplot.
To initialize a lineplot based on the pandas
DataFrame, you need to input at least 4 parameters: x
, y
(columns-markers for the plot), hue
(the third variable), and data
(the DataFrame containing the data).
Look at the code below!
99
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/example5.csv')
# Creating the 3-variable lineplot
sns.lineplot(x = 'x', y = 'y', hue = 'gender', data=df)
# Showing the plot
plt.show()
12345678910111213# 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/example5.csv') # Creating the 3-variable lineplot sns.lineplot(x = 'x', y = 'y', hue = 'gender', data=df) # Showing the plot plt.show()
Tarea
Swipe to start coding
- Import the
seaborn
withsns
alias. - Import the
matplotlib.pyplot
withplt
alias. - Import the
pandas
withpd
alias. - Read the file using
df
variable. - Create a 3-variable lineplot using
'year'
column for the x-value and'population'
column for the y-value and'season'
for the hue-value. - Show the plot.
Solución
9
1
2
3
4
5
6
7
8
9
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/lineplothue.csv')
sns.lineplot(x = 'year', y = 'population', hue = 'season', data = df)
plt.show()
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 2. Capítulo 6
99
1
2
3
4
5
6
7
8
9
10
11
12
13
# Import libraries needed
___
___
___
# Read the file
___ = ___.___('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/lineplothue.csv')
# Create a lineplot
___.___(___ = '___', ___ = '___', ___ = '___', ___)
# Showing the plot
plt.show()
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla