Contenido del Curso
Ultimate Visualization with Python
Ultimate Visualization with Python
Countplot
A countplot is a plot which creates columns (bars) that represent the number of entries for each category of a categorical list. It can be also thought of as a histogram of a categorical variable. Let’s have a look at an example of a count plot:
Here each column represents the number of Titanic passengers of each class. You may have already noticed that this plot is very similar to the bar chart. Indeed, it is a rather specific kind of bar chart representing the frequency of each category.
Note
You still have to import the
pyplot
module frommatplotlib
and use theplt.show()
function to display the plots created withseaborn
.
In order to create a countplot with seaborn
, you should use the countplot()
function. There are several possible options to pass our data to this function.
Passing a 1D Array
The first option is to simply pass the value for the x
parameter which can be some kind of an array:
import seaborn as sns import matplotlib.pyplot as plt fruits = ['apple', 'banana', 'orange', 'apple', 'apple', 'apple', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] sns.countplot(x=fruits) plt.show()
As you can see, the function simply counts the occurrences of each unique element in the list and creates a column with the respective height for each of them.
Note
We may also use y parameter instead of x to change the orientation of the plot from vertical to horizontal.
Passing a 2D Object
Another option is to use the data
parameter combined with the x
or y
parameter. This approach is suitable for working with pandas
DataFrames. You can pass a list of arrays or a DataFrame
as the value for data
. For x
or y
you can pass a name of the column in the DataFrame
, for example:
import seaborn as sns import matplotlib.pyplot as plt # Loading a built-in dataset of the Titanic passengers titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
Our function in this example creates a countplot based on the 'class'
column of a titanic DataFrame
and counts the number of entries for each unique value in this column.
Swipe to show code editor
- Import the
seaborn
library with thesns
alias. - Import the
matplotlib.pyplot
module with theplt
alias. - Use the correct function to create a countplot.
- Use the
diamonds
as the first argument to specify theDataFrame
. - Use the
'cut'
column of thediamonds
DataFrame as the categories for the countplot and display the cateories on the y-axis via the second argument. - Display the plot using the correct function.
¡Gracias por tus comentarios!
Countplot
A countplot is a plot which creates columns (bars) that represent the number of entries for each category of a categorical list. It can be also thought of as a histogram of a categorical variable. Let’s have a look at an example of a count plot:
Here each column represents the number of Titanic passengers of each class. You may have already noticed that this plot is very similar to the bar chart. Indeed, it is a rather specific kind of bar chart representing the frequency of each category.
Note
You still have to import the
pyplot
module frommatplotlib
and use theplt.show()
function to display the plots created withseaborn
.
In order to create a countplot with seaborn
, you should use the countplot()
function. There are several possible options to pass our data to this function.
Passing a 1D Array
The first option is to simply pass the value for the x
parameter which can be some kind of an array:
import seaborn as sns import matplotlib.pyplot as plt fruits = ['apple', 'banana', 'orange', 'apple', 'apple', 'apple', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] sns.countplot(x=fruits) plt.show()
As you can see, the function simply counts the occurrences of each unique element in the list and creates a column with the respective height for each of them.
Note
We may also use y parameter instead of x to change the orientation of the plot from vertical to horizontal.
Passing a 2D Object
Another option is to use the data
parameter combined with the x
or y
parameter. This approach is suitable for working with pandas
DataFrames. You can pass a list of arrays or a DataFrame
as the value for data
. For x
or y
you can pass a name of the column in the DataFrame
, for example:
import seaborn as sns import matplotlib.pyplot as plt # Loading a built-in dataset of the Titanic passengers titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
Our function in this example creates a countplot based on the 'class'
column of a titanic DataFrame
and counts the number of entries for each unique value in this column.
Swipe to show code editor
- Import the
seaborn
library with thesns
alias. - Import the
matplotlib.pyplot
module with theplt
alias. - Use the correct function to create a countplot.
- Use the
diamonds
as the first argument to specify theDataFrame
. - Use the
'cut'
column of thediamonds
DataFrame as the categories for the countplot and display the cateories on the y-axis via the second argument. - Display the plot using the correct function.
¡Gracias por tus comentarios!
Countplot
A countplot is a plot which creates columns (bars) that represent the number of entries for each category of a categorical list. It can be also thought of as a histogram of a categorical variable. Let’s have a look at an example of a count plot:
Here each column represents the number of Titanic passengers of each class. You may have already noticed that this plot is very similar to the bar chart. Indeed, it is a rather specific kind of bar chart representing the frequency of each category.
Note
You still have to import the
pyplot
module frommatplotlib
and use theplt.show()
function to display the plots created withseaborn
.
In order to create a countplot with seaborn
, you should use the countplot()
function. There are several possible options to pass our data to this function.
Passing a 1D Array
The first option is to simply pass the value for the x
parameter which can be some kind of an array:
import seaborn as sns import matplotlib.pyplot as plt fruits = ['apple', 'banana', 'orange', 'apple', 'apple', 'apple', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] sns.countplot(x=fruits) plt.show()
As you can see, the function simply counts the occurrences of each unique element in the list and creates a column with the respective height for each of them.
Note
We may also use y parameter instead of x to change the orientation of the plot from vertical to horizontal.
Passing a 2D Object
Another option is to use the data
parameter combined with the x
or y
parameter. This approach is suitable for working with pandas
DataFrames. You can pass a list of arrays or a DataFrame
as the value for data
. For x
or y
you can pass a name of the column in the DataFrame
, for example:
import seaborn as sns import matplotlib.pyplot as plt # Loading a built-in dataset of the Titanic passengers titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
Our function in this example creates a countplot based on the 'class'
column of a titanic DataFrame
and counts the number of entries for each unique value in this column.
Swipe to show code editor
- Import the
seaborn
library with thesns
alias. - Import the
matplotlib.pyplot
module with theplt
alias. - Use the correct function to create a countplot.
- Use the
diamonds
as the first argument to specify theDataFrame
. - Use the
'cut'
column of thediamonds
DataFrame as the categories for the countplot and display the cateories on the y-axis via the second argument. - Display the plot using the correct function.
¡Gracias por tus comentarios!
A countplot is a plot which creates columns (bars) that represent the number of entries for each category of a categorical list. It can be also thought of as a histogram of a categorical variable. Let’s have a look at an example of a count plot:
Here each column represents the number of Titanic passengers of each class. You may have already noticed that this plot is very similar to the bar chart. Indeed, it is a rather specific kind of bar chart representing the frequency of each category.
Note
You still have to import the
pyplot
module frommatplotlib
and use theplt.show()
function to display the plots created withseaborn
.
In order to create a countplot with seaborn
, you should use the countplot()
function. There are several possible options to pass our data to this function.
Passing a 1D Array
The first option is to simply pass the value for the x
parameter which can be some kind of an array:
import seaborn as sns import matplotlib.pyplot as plt fruits = ['apple', 'banana', 'orange', 'apple', 'apple', 'apple', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] sns.countplot(x=fruits) plt.show()
As you can see, the function simply counts the occurrences of each unique element in the list and creates a column with the respective height for each of them.
Note
We may also use y parameter instead of x to change the orientation of the plot from vertical to horizontal.
Passing a 2D Object
Another option is to use the data
parameter combined with the x
or y
parameter. This approach is suitable for working with pandas
DataFrames. You can pass a list of arrays or a DataFrame
as the value for data
. For x
or y
you can pass a name of the column in the DataFrame
, for example:
import seaborn as sns import matplotlib.pyplot as plt # Loading a built-in dataset of the Titanic passengers titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
Our function in this example creates a countplot based on the 'class'
column of a titanic DataFrame
and counts the number of entries for each unique value in this column.
Swipe to show code editor
- Import the
seaborn
library with thesns
alias. - Import the
matplotlib.pyplot
module with theplt
alias. - Use the correct function to create a countplot.
- Use the
diamonds
as the first argument to specify theDataFrame
. - Use the
'cut'
column of thediamonds
DataFrame as the categories for the countplot and display the cateories on the y-axis via the second argument. - Display the plot using the correct function.