9种常用的Python数据可视化方法

2962
5

大哥很IT 数据老手Lv5

发表于2018-9-20 11:32

楼主
本帖最后由 大哥很IT 于 2018-9-20 11:50 编辑

用Python进行可视化需要做些什么?
基于Python的绘图库提供matplotlib 一个完整的2D支持和有限的三维图形支持。在跨平台交互环境中生成出版物质量图是非常有用的。它也可以用于动画。
Seborn是一个库,用于在python中创建信息丰富和有吸引力的统计图形。这个库基于matplotlib。Seborn提供了多种特性,如内置主题、调色板、函数和工具来可视化单变量、双变量、线性回归、数据矩阵、统计时间序列等,使我们能够构建复杂的可视化。
我能做什么不同的视觉化?
下面是python代码及其输出。我使用了以下数据集来创建这些可视化:
进口数据集:
  1. import matplotlib.pyplot as plt
  2. import pandas as pd
  3. df=pd.read_excel("E:/First.xlsx", "Sheet1")
复制代码


直方图:
  1. fig=plt.figure() #Plots in matplotlib reside within a figure object, use plt.figure to create new figure
  2. #Create one or more subplots using add_subplot, because you can't create blank figure
  3. ax = fig.add_subplot(1,1,1)
  4. #Variable
  5. ax.hist(df['Age'],bins = 7) # Here you can play with number of bins
  6. Labels and Tit
  7. plt.title('Age distribution')
  8. plt.xlabel('Age')
  9. plt.ylabel('#Employee')
  10. plt.show()
复制代码

盒图
  1. import matplotlib.pyplot as plt
  2. import pandas as pd
  3. fig=plt.figure()
  4. ax = fig.add_subplot(1,1,1)
  5. #Variable
  6. ax.boxplot(df['Age'])
  7. plt.show()
复制代码


小提琴情节
  1. import seaborn as sns
  2. sns.violinplot(df['Age'], df['Gender']) #Variable Plot
  3. sns.despine()
复制代码


条形图
  1. var = df.groupby('Gender').Sales.sum() #grouped sum of sales at Gender level
  2. fig = plt.figure()
  3. ax1 = fig.add_subplot(1,1,1)
  4. ax1.set_xlabel('Gender')
  5. ax1.set_ylabel('Sum of Sales')
  6. ax1.set_title("Gender wise Sum of Sales")
  7. var.plot(kind='bar')
复制代码

线图
  1. var = df.groupby('BMI').Sales.sum()
  2. fig = plt.figure()
  3. ax1 = fig.add_subplot(1,1,1)
  4. ax1.set_xlabel('BMI')
  5. ax1.set_ylabel('Sum of Sales')
  6. ax1.set_title("BMI wise Sum of Sales")
  7. var.plot(kind='line')
复制代码

叠柱图
  1. var = df.groupby(['BMI','Gender']).Sales.sum()
  2. var.unstack().plot(kind='bar',stacked=True,  color=['red','blue'], grid=False)
复制代码
un堆栈()返回一个具有新级别列标签的数据帧,其内部最高级由旋转索引标签组成。

散点图
  1. fig = plt.figure()
  2. ax = fig.add_subplot(1,1,1)
  3. ax.scatter(df['Age'],df['Sales']) #You can also add more variables here to represent color and size.
  4. plt.show()
复制代码

[tr]气泡图
  1. fig = plt.figure()
  2. ax = fig.add_subplot(1,1,1)
  3. ax.scatter(df['Age'],df['Sales'], s=df['Income']) # Added third variable income as size of the bubble
  4. plt.show()
复制代码

饼图
  1. var=df.groupby(['Gender']).sum().stack()
  2. temp=var.unstack()
  3. type(temp)
  4. x_list = temp['Sales']
  5. label_list = temp.index
  6. pyplot.axis("equal") #The pie chart is oval by default. To make it a circle use pyplot.axis("equal")
  7. #To show the percentage of each pie slice, pass an output format to the autopctparameter plt.pie(x_list,labels=label_list,autopct="%1.1f%%") plt.title("Pastafarianism expenses") plt.show()
复制代码

热图
  1. import numpy as np
  2. #Generate a random number, you can refer your data values also
  3. data = np.random.rand(4,2)
  4. rows = list('1234') #rows categories
  5. columns = list('MF') #column categories
  6. fig,ax=plt.subplots()
  7. #Advance color controls
  8. ax.pcolor(data,cmap=plt.cm.Reds,edgecolors='k')
  9. ax.set_xticks(np.arange(0,2)+0.5)
  10. ax.set_yticks(np.arange(0,4)+0.5)
  11. # Here we position the tick labels for x and y axis
  12. ax.xaxis.tick_bottom()
  13. ax.yaxis.tick_left()
  14. #Values against each labels
  15. ax.set_xticklabels(columns,minor=False,fontsize=20)
  16. ax.set_yticklabels(rows,minor=False,fontsize=20)
  17. plt.show()
复制代码

您可以尝试根据两个变量绘制热量,如x轴上的性别、Y轴上的BMI和作为数据点的销售值。请在注释部分分享您的代码和输出。

尾注
到现在为止,你肯定已经意识到,使用可视化技术可以显示多么漂亮的数据。我发现与本文中的R相比,用Python执行可视化要容易得多,我们讨论了如何在Python中派生各种可视化。在此过程中,我们在python中使用了matplotlib和seABOIN。在随后的文章中,我们将探讨python中的地图可视化和Word云。

你对可视化方法/技术/选择有什么疑问吗?请回帖和我们讨论哟!
本文来源:https://www.analyticsvidhya.com/blog/2015/05/data-visualization-python/


最近看过此主题的会员

esen_4KV7VZK2YIQI

ligaopeng

石雄飞

5个回答

只看楼主

韦韬然 数据老手Lv5

发表于2018-9-20 13:45

只看该作者

取消 关注该作者的回复

沙发

感觉python的有点难看。。。。。

辰哥 管理员

发表于2018-9-20 13:47

只看该作者

取消 关注该作者的回复

板凳

韦韬然 发表于 2018-9-20 13:45
感觉python的有点难看。。。。。

你觉得什么的好看,分享下!

韦韬然 数据老手Lv5

发表于2018-9-20 14:18

只看该作者

取消 关注该作者的回复

地板

辰哥 发表于 2018-9-20 13:47
你觉得什么的好看,分享下!

bi上的h5的图呀~这个python可以用吗?

社区小辣妹 小试身手Lv3

发表于2018-9-20 14:20

只看该作者

取消 关注该作者的回复

5#

韦韬然 发表于 2018-9-20 14:18
bi上的h5的图呀~这个python可以用吗?

BI上的H5图制作方法,要漂亮的那种,可以写分享文章吗?

韦韬然 数据老手Lv5

发表于2018-9-20 14:23

只看该作者

取消 关注该作者的回复

6#

社区小辣妹 发表于 2018-9-20 14:20
BI上的H5图制作方法,要漂亮的那种,可以写分享文章吗?

我的审美我自己都不信

登录后可回答问题,请登录注册

快速回复 返回顶部 返回列表

小时

全天响应

分钟

快速处理问题

工程师强势助力

明星产品
解决方案
联系合作

400咨询:400-0011-866

技术支持QQ:400-0011-866(工作日9:00-18:00)

产品建议邮箱yixin@esensoft.com

关注我们

扫TA学习更多干货

一对一专家交流

版权所有© 2006-2024 北京亿信华辰软件有限责任公司 京ICP备07017321号 京公网安备11010802016281号