本文将向您介绍使用Seaborn在Python中绘制图形,Seaborn是Python中最流行的统计可视化库。
安装: 安装seaborn最简单的方法是使用pip。在终端中键入以下命令:
pip install seaborn
或者,你可以从 在这里 然后手动安装。
用Seaborn绘制分类散点图
条纹图
Python3
# Python program to illustrate # Plotting categorical scatter # plots with Seaborn # importing the required module import matplotlib.pyplot as plt import seaborn as sns # x axis values x = [ 'sun' , 'mon' , 'fri' , 'sat' , 'tue' , 'wed' , 'thu' ] # y axis values y = [ 5 , 6.7 , 4 , 6 , 2 , 4.9 , 1.8 ] # plotting strip plot with seaborn ax = sns.stripplot(x, y); # giving labels to x-axis and y-axis ax. set (xlabel = 'Days' , ylabel = 'Amount_spend' ) # giving title to the plot plt.title( 'My first graph' ); # function to show plot plt.show() |
输出:
说明: 这是seaborn帮助下分类数据的一种散点图。
- 分类数据在x轴上表示,值通过y轴表示。
- .striplot() 函数用于定义打印类型,并使用在画布上打印。
- .set() 函数用于设置x轴和y轴的标签。
- .标题() 函数用于为图表提供标题。
- 查看我们使用的绘图 .show() 作用
使用seaborn中给出的内置数据集绘制条带图:
Python3
# Python program to illustrate # Stripplot using inbuilt data-set # given in seaborn # importing the required module import matplotlib.pyplot as plt import seaborn as sns # use to set style of background of plot sns. set (style = "whitegrid" ) # loading data-set iris = sns.load_dataset( 'iris' ) # plotting strip plot with seaborn # deciding the attributes of dataset on # which plot should be made ax = sns.stripplot(x = 'species' , y = 'sepal_length' , data = iris) # giving title to the plot plt.title( 'Graph' ) # function to show plot plt.show() |
输出:
说明:
- 艾里斯 数据集是否已存在于seaborn模块中以供使用。
- 我们使用 .load_数据集() 函数以加载数据。我们还可以通过在参数中提供文件的路径和名称来加载任何其他文件。
- .set(style=“whitegrid”) 这里的函数也用于定义绘图的背景。我们可以使用 “黑格力” 如果我们想要深色的背景,就不要白格子。
- 在里面 .stripplot() 函数我们必须定义数据集的哪个属性在x轴上,哪个属性在y轴上。 数据=虹膜 意味着我们之前定义的属性应该取自给定的数据。
- 我们也可以使用matplotlib绘制此图,但matplotlib的问题在于其默认参数。Seaborn对数据帧如此出色的原因是,例如,数据帧中的标签会自动传播到绘图或其他数据结构中,如上图中的列名所示 种 出现在x轴和列名上 阶梯长度 位于y轴上,这在matplotlib中是不可能的。我们必须明确定义x轴和y轴的标签。
斯旺普洛特
Python3
# Python program to illustrate # plotting using Swarmplot # importing the required module import matplotlib.pyplot as plt import seaborn as sns # use to set style of background of plot sns. set (style = "whitegrid" ) # loading data-set iris = sns.load_dataset( 'iris' ) # plotting strip plot with seaborn # deciding the attributes of dataset on # which plot should be made ax = sns.swarmplot(x = 'species' , y = 'sepal_length' , data = iris) # giving title to the plot plt.title( 'Graph' ) # function to show plot plt.show() |
输出:
说明: 这与stripplot非常相似,但唯一的区别是它不允许标记重叠。它会导致绘图标记出现抖动,这样就可以轻松读取图形,而不会像上面的绘图那样丢失信息。
- 我们使用 斯瓦姆洛特先生() 用于绘制Swarm plot的函数。
- 我们在Seaborn和Matplotlib中可以注意到的另一个区别是,使用Matplotlib处理数据帧的过程并不像使用Matplotlib那样顺利,如果我们使用Pandas进行探索性分析,这可能会很烦人。这正是Seaborn容易做到的,绘图功能在包含整个数据集的数据帧和数组上运行。
注: 如果需要,我们还可以更改特定轴上数据的表示形式。
例子:
Python3
# importing the required module import matplotlib.pyplot as plt import seaborn as sns # use to set style of background of plot sns. set (style = "whitegrid" ) # loading data-set iris = sns.load_dataset( 'iris' ) # plotting strip plot with seaborn # deciding the attributes of dataset on # which plot should be made ax = sns.swarmplot(x = 'sepal_length' , y = 'species' , data = iris) # giving title to the plot plt.title( 'Graph' ) # function to show plot plt.show() |
输出:
在striplot中也可以这样做。最后,我们可以说Seaborn是matplotlib的一个扩展版本,它试图使一组定义明确的困难变得容易。
条形图
A. 条形图 基本上用于根据某些方法聚合分类数据,默认情况下为平均值。它也可以被理解为一个行动的群体形象。为了使用这个图,我们选择一个分类列作为x轴,一个数字列作为y轴,我们看到它创建了一个以每个分类列的平均值为基础的图。
语法:
barplot([x, y, hue, data, order, hue_order, …])
Python3
# import the seaborn library import seaborn as sns # reading the dataset df = sns.load_dataset( 'tips' ) # change the estimator from mean to # standard deviation sns.barplot(x = 'sex' , y = 'total_bill' , data = df, palette = 'plasma' ) |
输出:
说明: 从情节来看,我们可以说男性的平均总账单比女性多。
- 调色板用于设置绘图的颜色
- 估计器被用作每个分类箱内估计的统计函数。
计数图
countplot基本上统计类别并返回它们出现的次数。这是seaborn图书馆提供的最简单的情节之一。
语法:
countplot([x, y, hue, data, order, …])
Python3
# import the seaborn library import seaborn as sns # reading the dataset df = sns.load_dataset( 'tips' ) sns.countplot(x = 'sex' , data = df) |
输出:
说明:
从图中可以看出,数据集中男性的数量大于女性的数量。因为它只返回基于分类列的计数,所以我们只需要指定x参数。
箱形图
盒状图 是通过四分位数描述数值数据组的视觉表示。箱线图还用于检测数据集中的异常值。
语法:
boxplot([x, y, hue, data, order, hue_order, …])
Python3
# import the seaborn library import seaborn as sns # reading the dataset df = sns.load_dataset( 'tips' ) sns.boxplot(x = 'day' , y = 'total_bill' , data = df, hue = 'smoker' ) |
输出:
说明:
x为分类列,y为数字列。因此,我们可以看到每天花费的总账单。”“色调”参数用于进一步添加分类分隔。通过观察情节,我们可以说,与吸烟的人相比,不吸烟的人在周五的账单更高。
小提琴谱
它与箱线图类似,只是它提供了更高、更高级的可视化,并使用核密度估计来更好地描述数据分布。
语法:
violinplot([x, y, hue, data, order, …])
Python3
# import the seaborn library import seaborn as sns # reading the dataset df = sns.load_dataset( 'tips' ) sns.violinplot(x = 'day' , y = 'total_bill' , data = df, hue = 'sex' , split = True ) |
输出:
说明:
- 色调用于使用性别类别进一步分离数据
- 设置split=True将为每个级别绘制一半小提琴。这可以使直接比较分布更容易。
条纹图
它基本上根据类别创建散点图。
语法:
stripplot([x, y, hue, data, order, …])
Python3
# import the seaborn library import seaborn as sns # reading the dataset df = sns.load_dataset( 'tips' ) sns.stripplot(x = 'day' , y = 'total_bill' , data = df, jitter = True , hue = 'smoker' , dodge = True ) |
输出:
说明:
- 条形图的一个问题是,你无法真正分辨哪些点是堆叠在彼此之上的,因此我们使用抖动参数来添加一些随机噪声。
- jitter参数用于添加一定量的抖动(仅沿分类轴),当有多个点重叠时,这非常有用,以便更容易看到分布。
- 色调用于提供额外的分类分隔
- 设置split=True用于根据色调参数指定的类别绘制单独的条形图。