-
-
大哥很IT 数据老手Lv5
发表于2018-9-20 11:32
楼主
本帖最后由 大哥很IT 于 2018-9-20 11:50 编辑
用Python进行可视化需要做些什么?
进口数据集:复制代码
直方图:复制代码
盒图复制代码
小提琴情节复制代码
条形图
复制代码
线图复制代码
叠柱图复制代码
散点图复制代码
[tr]气泡图复制代码
饼图复制代码
热图复制代码
尾注
用Python进行可视化需要做些什么?
基于Python的绘图库提供matplotlib 一个完整的2D支持和有限的三维图形支持。在跨平台交互环境中生成出版物质量图是非常有用的。它也可以用于动画。
Seborn是一个库,用于在python中创建信息丰富和有吸引力的统计图形。这个库基于matplotlib。Seborn提供了多种特性,如内置主题、调色板、函数和工具来可视化单变量、双变量、线性回归、数据矩阵、统计时间序列等,使我们能够构建复杂的可视化。
我能做什么不同的视觉化?下面是python代码及其输出。我使用了以下数据集来创建这些可视化:

- import matplotlib.pyplot as plt
- import pandas as pd
- df=pd.read_excel("E:/First.xlsx", "Sheet1")
直方图:
- fig=plt.figure() #Plots in matplotlib reside within a figure object, use plt.figure to create new figure
- #Create one or more subplots using add_subplot, because you can't create blank figure
- ax = fig.add_subplot(1,1,1)
- #Variable
- ax.hist(df['Age'],bins = 7) # Here you can play with number of bins
- Labels and Tit
- plt.title('Age distribution')
- plt.xlabel('Age')
- plt.ylabel('#Employee')
- plt.show()

盒图
- import matplotlib.pyplot as plt
- import pandas as pd
- fig=plt.figure()
- ax = fig.add_subplot(1,1,1)
- #Variable
- ax.boxplot(df['Age'])
- plt.show()

小提琴情节
- import seaborn as sns
- sns.violinplot(df['Age'], df['Gender']) #Variable Plot
- sns.despine()

条形图
- var = df.groupby('Gender').Sales.sum() #grouped sum of sales at Gender level
- fig = plt.figure()
- ax1 = fig.add_subplot(1,1,1)
- ax1.set_xlabel('Gender')
- ax1.set_ylabel('Sum of Sales')
- ax1.set_title("Gender wise Sum of Sales")
- var.plot(kind='bar')

线图
- var = df.groupby('BMI').Sales.sum()
- fig = plt.figure()
- ax1 = fig.add_subplot(1,1,1)
- ax1.set_xlabel('BMI')
- ax1.set_ylabel('Sum of Sales')
- ax1.set_title("BMI wise Sum of Sales")
- var.plot(kind='line')

叠柱图
- var = df.groupby(['BMI','Gender']).Sales.sum()
- var.unstack().plot(kind='bar',stacked=True, color=['red','blue'], grid=False)

un堆栈()返回一个具有新级别列标签的数据帧,其内部最高级由旋转索引标签组成。
散点图
- fig = plt.figure()
- ax = fig.add_subplot(1,1,1)
- ax.scatter(df['Age'],df['Sales']) #You can also add more variables here to represent color and size.
- plt.show()

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

饼图
- var=df.groupby(['Gender']).sum().stack()
- temp=var.unstack()
- type(temp)
- x_list = temp['Sales']
- label_list = temp.index
- pyplot.axis("equal") #The pie chart is oval by default. To make it a circle use pyplot.axis("equal")
- #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()

热图
- import numpy as np
- #Generate a random number, you can refer your data values also
- data = np.random.rand(4,2)
- rows = list('1234') #rows categories
- columns = list('MF') #column categories
- fig,ax=plt.subplots()
- #Advance color controls
- ax.pcolor(data,cmap=plt.cm.Reds,edgecolors='k')
- ax.set_xticks(np.arange(0,2)+0.5)
- ax.set_yticks(np.arange(0,4)+0.5)
- # Here we position the tick labels for x and y axis
- ax.xaxis.tick_bottom()
- ax.yaxis.tick_left()
- #Values against each labels
- ax.set_xticklabels(columns,minor=False,fontsize=20)
- ax.set_yticklabels(rows,minor=False,fontsize=20)
- plt.show()

您可以尝试根据两个变量绘制热量,如x轴上的性别、Y轴上的BMI和作为数据点的销售值。请在注释部分分享您的代码和输出。
尾注
到现在为止,你肯定已经意识到,使用可视化技术可以显示多么漂亮的数据。我发现与本文中的R相比,用Python执行可视化要容易得多,我们讨论了如何在Python中派生各种可视化。在此过程中,我们在python中使用了matplotlib和seABOIN。在随后的文章中,我们将探讨python中的地图可视化和Word云。
你对可视化方法/技术/选择有什么疑问吗?请回帖和我们讨论哟!
本文来源:https://www.analyticsvidhya.com/blog/2015/05/data-visualization-python/