Python数据可视化编程,Matplotlib、 直方图等你会几种?

Python数据可视化编程,Matplotlib、 直方图等你会几种?

Python数据可视化库

  • Matplotlib:其能够支持所有的2D作图和部分3D作图。能通过交互环境做出印刷质量的图像。
  • Seaborn:基于Matplotlib,seaborn提供许多功能,比如:内置主题、颜色调色板、函数和提供可视化单变量、双变量、线性回归的工具。其能帮助我们构建复杂的可视化。

数据集

EMPIDGenderAgeSalesBMIIncomeE001M34123Normal350E002F40114Overweight450E003F37135Obesity169E004M30139Underweight189E005F44117Underweight183E006M36121Normal80E007M32133Obesity166E008F26140Normal120E009M32133Normal75E010M36133Underweight40

作图

# -*- coding:UTF-8 -*-import matplotlib.pyplot as pltimport pandas as pdimport seaborn as snsimport numpy as np# 0、导入数据集df = pd.read_excel('first.xlsx', 'Sheet1')123456789# 1、直方图fig = plt.figure()ax = fig.add_subplot(111)ax.hist(df['Age'], bins=7)plt.title('Age distribution')plt.xlabel('Age')plt.ylabel('Employee')plt.show()12345678
Python数据可视化编程,Matplotlib、 直方图等你会几种?

# 2、箱线图 fig = plt.figure()ax = fig.add_subplot(111)ax.boxplot(df['Age'])plt.show()12345
Python数据可视化编程,Matplotlib、 直方图等你会几种?

# 3、小提琴图sns.violinplot(df['Age'], df['Gender'])sns.despine()plt.show()1234
Python数据可视化编程,Matplotlib、 直方图等你会几种?

# 4、条形图var = df.groupby('Gender').Sales.sum()fig = plt.figure()ax1 = fig.add_subplot(111)ax1.set_xlabel('Gender')ax1.set_ylabel('Sum of Sales')ax1.set_title('Gender wise Sum of Sales')var.plot(kind='bar')plt.show()123456789
Python数据可视化编程,Matplotlib、 直方图等你会几种?

# 5、折线图var = df.groupby('BMI').Sales.sum()fig = plt.figure()ax = fig.add_subplot(111)ax.set_xlabel('BMI')ax.set_ylabel('Sum of Sales')ax.set_title('BMI wise Sum of Sales')var.plot(kind='line')plt.show()123456789
Python数据可视化编程,Matplotlib、 直方图等你会几种?

# 6、堆积柱形图var = df.groupby(['BMI', 'Gender']).Sales.sum()var.unstack().plot(kind='bar', stacked=True, color=['red', 'blue'])plt.show()1234
Python数据可视化编程,Matplotlib、 直方图等你会几种?

# 7、散点图fig = plt.figure()ax = fig.add_subplot(111)ax.scatter(df['Age'], df['Sales'])plt.show()12345
Python数据可视化编程,Matplotlib、 直方图等你会几种?

# 8、气泡图fig = plt.figure()ax = fig.add_subplot(111)ax.scatter(df['Age'], df['Sales'], s=df['Income']) # 第三个变量表明根据收入气泡的大小plt.show()
Python数据可视化编程,Matplotlib、 直方图等你会几种?

Python-Matplotlib(3) 条形图实战

Python数据可视化编程,Matplotlib、 直方图等你会几种?

Python数据可视化编程,Matplotlib、 直方图等你会几种?

好了,今天的知识就分享到这里,欢迎关注爱编程的南风,私信关键词:学习资料,获取更多学习资源,如果文章对你有有帮助,请收藏关注,在今后与你分享更多学习python的文章。同时欢迎在下面评论区留言如何学习python。


分享到:


相關文章: