Data visualization using the Python (2)

Bar chart

If you can see the value of the variable distribution histogram, then the bar chart can help us see the features category. In the bar chart, showing the length of the elongated frequency category indicating the type width.

In Matplotlib, we use plt.bar (x, height) function, where the parameter x represents the x-axis position of the sequence, height is the y-axis numeric sequence, i.e. the height of the column.

In Seaborn, we use sns.barplot ( X = None, Y = None, Data = None ) function. Wherein the parameter is a data type DataFrame, x, y is the variable data.

1
2
3
4
5
6
x =["cat1","cat2","cat3","cat4","cat5"]
y =[5,4,8,12,7]
plt.bar(x,y)
plt.show()
sns.barplot(x,y)
plt.show()

We created x, y two arrays, representing the frequency category and category, and then displays a bar graph with Matplotlib and Seaborn, results are as follows:

1564401614271

1564401643365

Boxplot

Box plot, known as box plots, it was proposed in 1977 by the five data points: the maximum value (max), the minimum value (min), median (Median) and upper and lower quartiles ( Q3, Q1). It can help us analyze the differences in the data, such as degree of dispersion and outliers.

In Matplotlib, we use plt.boxplot (x, labels = None) function, where x is the parameter to the drawing data boxplot, Labels is the default, the label may be added as box plots.

在Seaborn中,我们使用sns.boxplot(x=None, y=None, data=None)函数。其中参数data为DataFrame类型,x、y是data中的变量。

1
2
3
4
5
6
7
data = np.random.normal(size=(10,4))
labels =['A','B','C','D']
plt.boxplot(data,labels=labels)
plt.show()
df = pd.DataFrame(data,columns=labels)
sns.boxplot(data=df)
plt.show()

这段代码中,我生成0-1之间的10*4维度数据,然后分别用Matplotlib和Seaborn进行箱线图的展示,结果如下。

1564401760747

1564401788850

饼图

饼图是常用的统计学模块,可以显示每个部分大小与总和之间的比例。在Python数据可视化中,它用的不算多。我们主要采用Matplotlib的pie函数实现它。

在Matplotlib中,我们使用plt.pie(x, labels=None)函数,其中参数x代表要绘制饼图的数据,labels是缺省值,可以为饼图添加标签。

这里我设置了lables数组,分别代表高中、本科、硕士、博士和其他几种学历的分类标签。nums代表这些学历对应的人数。

1
2
3
4
nums = [25,37,33,37,6]
labels =["high_scholl","Bachelor","Master","Ph.d","others"]
plt.pie(x=nums,labels=labels)
plt.show()

通过Matplotlib的pie函数,我们可以得出下面的饼图:

1564401904096

热力图

热力图,英文叫heat map,是一种矩阵表示方法,其中矩阵中的元素值用颜色来代表,不同的颜色代表不同大小的值。通过颜色就能直观地知道某个位置上数值的大小。另外你也可以将这个位置上的颜色,与数据集中的其他位置颜色进行比较。

热力图是一种非常直观的多元变量分析方法。

我们一般使用Seaborn中的sns.heatmap(data)函数,其中data代表需要绘制的热力图数据。

这里我们使用Seaborn中自带的数据集flights,该数据集记录了1949年到1960年期间,每个月的航班乘客的数量。

1
2
3
4
5
flights = sns.load_dataset("flights")
data = flights.pivot("year","month","passengers")
# print(data.head())
sns.heatmap(data)
plt.show()

通过seaborn的heatmap函数,我们可以观察到不同年份,不同月份的乘客数量变化情况,其中颜色越浅的代表乘客数量越多,如下图所示:

1564402034035

蜘蛛图

蜘蛛图是一种显示一对多关系的方法。在蜘蛛图中,一个变量相对于另一个变量的显著性是清晰可见的。

假设我们想要给王者荣耀的玩家做一个战力图,指标一共包括推进、KDA、生存、团战、发育和输出。那该如何做呢?

这里我们需要使用Matplotlib来进行画图,首先设置两个数组:labels和stats。他们分别保存了这些属性的名称和属性值。

因为蜘蛛图是一个圆形,你需要计算每个坐标的角度,然后对这些数值进行设置。当画完最后一个点后,需要与第一个点进行连线。

因为需要计算角度,所以我们要准备angles数组;又因为需要设定统计结果的数值,所以我们要设定stats数组。并且需要在原有angles和stats数组上增加一位,也就是添加数组的第一个元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
labels=np.array([u"推进","KDA",u"生存",u"团战",u"发育",u"输出"])
stats=[83, 61, 95, 67, 76, 88]
# 画图数据准备,角度、状态值
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))
# 用Matplotlib画蜘蛛图
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, stats, 'o-', linewidth=2)
ax.fill(angles, stats, alpha=0.25)
# 设置中文字体
font = FontProperties(fname=r"C:WindowsFontssimhei.ttf", size=14)
ax.set_thetagrids(angles * 180/np.pi, labels, FontProperties=font)
plt.show()

Code flt.figure is to create a blank figure target, aim to prepare the equivalent of a blank slate before painting. Then add_subplot (111) can be divided into a row Artboards 1. Then ax.plot and ax.fill be wired to the graphics and color. Finally, we show the attribute name in the appropriate position.

1564402530705

Original: Large column  using Python data visualization (2)


Guess you like

Origin www.cnblogs.com/chinatrump/p/11424091.html