Histplot
A histogram (aka histplot) is a classic visualization tool that represents the distribution of one or more variables by counting the number of observations that fall within discrete bins.
An example of a histplot:
To initialize a histplot based on the pandas
DataFrame, we need to input at least 2 parameters: x/y
(the column in which values will be used to create a histplot) 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 a file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/example6.csv')
# Creating a histplot
sns.histplot(x = 'value', 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 a file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/example6.csv') # Creating a histplot sns.histplot(x = 'value', data = df) # Showing the plot plt.show()
We can change the histogram binning intervals by using binwidth=n
as an argument in the plot function, n
- is the width of one column:
99
1
2
3
4
5
6
7
8
9
10
11
12
13
# Importing the libraries needed
import seaborn as sns
import matplotlib.pyplot
import pandas as pd
# Reading a file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/example6.csv')
# Creating a histplot
sns.histplot(x = 'value', data = df, binwidth = 2)
# Showing the plot
plt.show()
12345678910111213# Importing the libraries needed import seaborn as sns import matplotlib.pyplot import pandas as pd # Reading a file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/example6.csv') # Creating a histplot sns.histplot(x = 'value', data = df, binwidth = 2) # Showing the plot plt.show()
Let's solve this problem!
Taak
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 x-oriented lineplot using
'answer'
column in the plot function. - Set
binwidth = 0.2
. - Show the plot.
Oplossing
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/fish.csv')
sns.histplot(x = 'answer', data = df, binwidth = 0.2)
plt.show()
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 2. Hoofdstuk 7
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/fish.csv')
# Create the histplot
___.___(___ = '___', ___ = ___, binwidth = ___)
# Show the plot
___
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.