📜  在Python中使用 seaborn 的计数图

📅  最后修改于: 2022-05-13 01:54:27.144000             🧑  作者: Mango

在Python中使用 seaborn 的计数图

Seaborn是一个惊人的Python统计图形绘图可视化库。它提供了漂亮的默认样式和调色板,使统计图更具吸引力。它建立在 matplotlib 库之上,并且与 pandas 的数据结构紧密集成。

Seaborn.countplot()

seaborn.countplot()方法用于使用条形图显示每个分类箱中的观察计数。

下面的例子说明了 seaborn 库的 countplot() 方法。

在 Seaborn 计数图中对具有不同属性的变量进行分组

示例 1:显示单个分类变量的值计数。

如果我们只使用一个数据变量而不是两个数据变量,那么这意味着轴将这些数据变量中的每一个都表示为一个轴。

X表示x轴,y表示y轴。

句法:

seaborn.countplot(x)
Python3
# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# count plot on single categorical variable
sns.countplot(x ='sex', data = df)
 
# Show the plot
plt.show()


Python3
# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# count plot on two categorical variable
sns.countplot(x ='sex', hue = "smoker", data = df)
 
# Show the plot
plt.show()


Python3
# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# count plot along y axis
sns.countplot(y ='sex', hue = "smoker", data = df)
 
# Show the plot
plt.show()


Python3
# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# use a different colour palette in count plot
sns.countplot(x ='sex', data = df, palette = "Set2")
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.countplot(x = 'class', y = 'fare',
            hue = 'sex',
            data = df,color="salmon")
 
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.countplot(x ='sex', data = df,
              color="salmon",
              saturation = 0.1)
# Show the plot
plt.show()


Python3
# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.countplot(x ='sex', data = df,color="salmon", facecolor=(0, 0, 0, 0),
                   linewidth=5,
                   edgecolor=sns.color_palette("BrBG", 2))
# Show the plot
plt.show()


输出 :

计数图-1

示例 2:显示两个分类变量的值计数并使用色调参数:

虽然点是在二维中绘制的,但可以通过根据第三个变量对点着色来将另一个维度添加到图中。

句法:

Python3

# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# count plot on two categorical variable
sns.countplot(x ='sex', hue = "smoker", data = df)
 
# Show the plot
plt.show()

输出 :

countplot-2

示例 3:水平绘制条形图。

在上面的示例中,我们看到了如何绘制单个水平计数图,并且在这里可以通过与另一个轴交换数据变量来执行多个水平计数图。

Python3

# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# count plot along y axis
sns.countplot(y ='sex', hue = "smoker", data = df)
 
# Show the plot
plt.show()

输出 :

countplot-3

示例 4:使用不同的调色板属性。

使用调色板,我们可以生成具有不同颜色的点。在下面的示例中,我们可以看到调色板可以负责生成具有不同颜色图值的计数图。

句法:

Python3

# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# use a different colour palette in count plot
sns.countplot(x ='sex', data = df, palette = "Set2")
 
# Show the plot
plt.show()

输出 :

countplot-4

示例 5:在图中使用颜色参数。

使用颜色属性,我们是所有元素的颜色。

句法:

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.countplot(x = 'class', y = 'fare',
            hue = 'sex',
            data = df,color="salmon")
 
# Show the plot
plt.show()

输出:

示例 6:在图中使用饱和度参数。

绘制颜色的原始饱和度的比例。大色块通常看起来更好,颜色稍微不饱和,但如果您希望绘图颜色与输入颜色规范完美匹配,请将其设置为 1。

句法:

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.countplot(x ='sex', data = df,
              color="salmon",
              saturation = 0.1)
# Show the plot
plt.show()

输出:

示例 7:使用 matplotlib.axes.Axes.bar() 参数来控制样式。

我们可以使用线宽设置构成绘图元素的灰线的宽度。每当我们增加线宽时,点也会自动增加。

句法:

Python3

# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.countplot(x ='sex', data = df,color="salmon", facecolor=(0, 0, 0, 0),
                   linewidth=5,
                   edgecolor=sns.color_palette("BrBG", 2))
# Show the plot
plt.show()

输出: