Python Seaborn – 使用 Catplot 的带状图插图
Seaborn是一个 基于matplotlib的数据可视化库。 seaborn提供了一个高级界面,用于绘制信息丰富且有吸引力的统计图形。 Seaborn Catplot是seaborn的新增功能,它使绘图更容易并涉及分类变量。它用于显示分类变量(如带状图、箱形图等)与数值变量之间的关系。
由strip表示的Stripplot是 c atplot()中的默认插图。
循序渐进的方法:
- 导入所需的模块。
Python3
# importing modules
import pandas as pnd
import matplotlib.pyplot as plt
import seaborn as sbn
Python3
# fetching data from the url
url_data='http://bit.ly/2cLzoxH'
# using pandas for reading the data file
# storing the data in input_data variable
input_data=pnd.read_csv(url_data)
Python3
# head() function helps to see the first n rows of data
# by default n is 5 in head function
input_data.head(5)
Python3
# using seaborn module to show the relation betwwen
# categorical variables and numerical variables
sbn.catplot(x='continent', y='lifeExp', data=input_data)
Python3
# importing modules
import pandas as pnd
import matplotlib.pyplot as plt
import seaborn as sbn
# fetching data from the url
url_data='http://bit.ly/2cLzoxH'
# using pandas for reading the data file
# storing the data in input_data variable
input_data=pnd.read_csv(url_data)
# using seaborn module to show the relation betwwen
# categorical variables and numerical variables
sbn.catplot(x='continent', y='lifeExp', data=input_data)
- 分配数据集并将其转换为数据框。
蟒蛇3
# fetching data from the url
url_data='http://bit.ly/2cLzoxH'
# using pandas for reading the data file
# storing the data in input_data variable
input_data=pnd.read_csv(url_data)
- 显示数据框。
蟒蛇3
# head() function helps to see the first n rows of data
# by default n is 5 in head function
input_data.head(5)
输出:
- 最后,描绘插图。
蟒蛇3
# using seaborn module to show the relation betwwen
# categorical variables and numerical variables
sbn.catplot(x='continent', y='lifeExp', data=input_data)
输出:
以下是基于上述方法的完整程序:
蟒蛇3
# importing modules
import pandas as pnd
import matplotlib.pyplot as plt
import seaborn as sbn
# fetching data from the url
url_data='http://bit.ly/2cLzoxH'
# using pandas for reading the data file
# storing the data in input_data variable
input_data=pnd.read_csv(url_data)
# using seaborn module to show the relation betwwen
# categorical variables and numerical variables
sbn.catplot(x='continent', y='lifeExp', data=input_data)
输出: