離散型變量的可視化

一、餅圖

需要導(dǎo)入matplotlib模塊中子模塊pyplot中的pie函數(shù)

# 導(dǎo)入第三方模塊
import matplotlib.pyplot as plt

# 構(gòu)造數(shù)據(jù)
edu = [0.2515,0.3724,0.3336,0.0368,0.0057]
labels = ['中專','大專','本科','碩士','其他']

explode = [0,0.1,0,0,0]  # 生成數(shù)據(jù),用于突出顯示大專學(xué)歷人群
colors=['#9999ff','#ff9999','#7777aa','#2442aa','#dd5555']  # 自定義顏色

# 中文和數(shù)字中的負(fù)號(hào)通過(guò)rcParams處理
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False

# 將橫、縱坐標(biāo)軸標(biāo)準(zhǔn)化處理,確保餅圖是一個(gè)正圓,否則為橢圓
plt.axes(aspect='equal')
# 繪制餅圖
plt.pie(x = edu, # 繪圖數(shù)據(jù)
        explode=explode, # 突出顯示大專人群
        labels=labels, # 添加教育水平標(biāo)簽
        colors=colors, # 設(shè)置餅圖的自定義填充色
        autopct='%.1f%%', # 設(shè)置百分比的格式,這里保留一位小數(shù)
        pctdistance=0.8,  # 設(shè)置百分比標(biāo)簽與圓心的距離
        labeldistance = 1.1, # 設(shè)置教育水平標(biāo)簽與圓心的距離
        startangle = 180, # 設(shè)置餅圖的初始角度
        radius = 1.2, # 設(shè)置餅圖的半徑
        counterclock = False, # 是否逆時(shí)針,這里設(shè)置為順時(shí)針?lè)较?        wedgeprops = {'linewidth': 1.5, 'edgecolor':'green'},# 設(shè)置餅圖內(nèi)外邊界的屬性值
        textprops = {'fontsize':10, 'color':'black'}, # 設(shè)置文本標(biāo)簽的屬性值
        )
# 添加圖標(biāo)題
plt.title('失信用戶的受教育水平分布')
# 顯示圖形
plt.show()

二、條形圖

餅圖不擅長(zhǎng)對(duì)比差異不大的離散型變量時(shí)選用條形圖

分為垂直條形圖、水平條形圖、堆疊條形圖、水平交錯(cuò)條形圖

離散型變量在各水平上的差異就是比較柱形的高低,越高值越大

需要導(dǎo)入matplotlib模塊中子模塊pyplot中的bar函數(shù)

1.垂直條形圖

# 條形圖的繪制--垂直條形圖
# 讀入數(shù)據(jù)
import pandas as pd
GDP = pd.read_excel('F:\Province GDP 2017.xlsx')
GDP
# 設(shè)置繪圖風(fēng)格(不妨使用R語(yǔ)言中的ggplot2風(fēng)格),否則背景為白底
plt.style.use('ggplot')
# 繪制條形圖
plt.bar(left = range(GDP.shape[0]), # 指定條形圖x軸的刻度值
        height = GDP.GDP, # 指定條形圖y軸的數(shù)值
        tick_label = GDP.Province, # 指定條形圖x軸的刻度標(biāo)簽
        color = 'steelblue', # 指定條形圖的填充色
       )
# 添加y軸的標(biāo)簽
plt.ylabel('GDP(萬(wàn)億)')
# 添加條形圖的標(biāo)題
plt.title('2017年度6個(gè)省份GDP分布')
# 為每個(gè)條形圖添加數(shù)值標(biāo)簽
#前兩個(gè)參數(shù)用于定位字符在圖形中的位置,第三個(gè)參數(shù)表示呈現(xiàn)的具體字符值,第四個(gè)參數(shù)表示字符的水平對(duì)齊方式為居中對(duì)齊
for x,y in enumerate(GDP.GDP):
    plt.text(x,y+0.1,'%s' %round(y,1),ha='center')
# 顯示圖形    
plt.show()

2.水平條形圖

上述條形圖按升序處理

# 條形圖的繪制--水平條形圖
# 對(duì)讀入的數(shù)據(jù)作升序排序
GDP.sort_values(by = 'GDP', inplace = True)
# 繪制條形圖
plt.barh(bottom = range(GDP.shape[0]), # 指定條形圖y軸的刻度值
        width = GDP.GDP, # 指定條形圖x軸的數(shù)值
        tick_label = GDP.Province, # 指定條形圖y軸的刻度標(biāo)簽
        color = 'steelblue', # 指定條形圖的填充色
       )
# 添加x軸的標(biāo)簽
plt.xlabel('GDP(萬(wàn)億)')
# 添加條形圖的標(biāo)題
plt.title('2017年度6個(gè)省份GDP分布')
# 為每個(gè)條形圖添加數(shù)值標(biāo)簽
for y,x in enumerate(GDP.GDP):
    plt.text(x+0.1,y,'%s' %round(x,1),va='center')
# 顯示圖形    
plt.show()

水平條形圖的y軸刻度值是從下往上,升序即從小到大

3.堆疊條形圖

不管是垂直條形圖還是水平條形圖,只反映單個(gè)離散變量的統(tǒng)計(jì)圖形,當(dāng)需要傳遞兩個(gè)離散變量時(shí),用堆疊條形圖,橫坐標(biāo)代表一個(gè)維度的離散變量,堆疊起來(lái)的”塊“代表另一個(gè)維度的離散變量,可以方便比較累積和。

# 條形圖的繪制--堆疊條形圖
# 讀入數(shù)據(jù)
Industry_GDP = pd.read_excel('F:\Industry_GDP.xlsx')
Industry_GDP
# 取出四個(gè)不同的季度標(biāo)簽,用作堆疊條形圖x軸的刻度標(biāo)簽
Quarters = Industry_GDP.Quarter.unique()
# 取出第一產(chǎn)業(yè)的四季度值
Industry1 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第一產(chǎn)業(yè)']
# 重新設(shè)置行索引
Industry1.index = range(len(Quarters))
# 取出第二產(chǎn)業(yè)的四季度值
Industry2 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第二產(chǎn)業(yè)']
# 重新設(shè)置行索引
Industry2.index = range(len(Quarters))
# 取出第三產(chǎn)業(yè)的四季度值
Industry3 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第三產(chǎn)業(yè)']

# 繪制堆疊條形圖
# 各季度下第一產(chǎn)業(yè)的條形圖
plt.bar(left = range(len(Quarters)), height=Industry1, color = 'steelblue', label = '第一產(chǎn)業(yè)', tick_label = Quarters)
# 各季度下第二產(chǎn)業(yè)的條形圖
plt.bar(left = range(len(Quarters)), height=Industry2, bottom = Industry1, color = 'green', label = '第二產(chǎn)業(yè)')
# 各季度下第三產(chǎn)業(yè)的條形圖
plt.bar(left = range(len(Quarters)), height=Industry3, bottom = Industry1  + Industry2, color = 'red', label = '第三產(chǎn)業(yè)')
# 添加y軸標(biāo)簽
plt.ylabel('生成總值(億)')
# 添加圖形標(biāo)題
plt.title('2017年各季度三產(chǎn)業(yè)總值')
# 顯示各產(chǎn)業(yè)的圖例
plt.legend()
# 顯示圖形
plt.show()

分別針對(duì)三種產(chǎn)業(yè)的產(chǎn)值繪制3次條形圖,第二產(chǎn)業(yè)的條形圖是在第一產(chǎn)業(yè)的基礎(chǔ)上做了疊加,第三產(chǎn)業(yè)的條形圖又是疊加在第一和第二產(chǎn)業(yè)之上

取出三個(gè)產(chǎn)業(yè)的值后,要重新設(shè)置行索引,是因?yàn)楦骷径认旅恳环N產(chǎn)業(yè)值前的行索引都不相同,會(huì)導(dǎo)致無(wú)法進(jìn)行Industry1+Industry2的和計(jì)算

4.水平交錯(cuò)條形圖

可以將堆疊條形圖的”塊“水平排開(kāi),可以輕易區(qū)分”塊“之間的差異

import numpy as np
# 取出四個(gè)不同的季度標(biāo)簽,用作x軸的刻度標(biāo)簽
Quarters = Industry_GDP.Quarter.unique()
# 取出第一產(chǎn)業(yè)的四季度值
Industry1 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第一產(chǎn)業(yè)']
# 取出第二產(chǎn)業(yè)的四季度值
Industry2 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第二產(chǎn)業(yè)']
# 取出第三產(chǎn)業(yè)的四季度值
Industry3 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第三產(chǎn)業(yè)']
# 繪制水平交錯(cuò)條形圖
bar_width = 0.4
plt.bar(left = np.arange(len(Quarters)), height = Industry1, label = 'Industry1', color = 'steelblue', width = bar_width)
plt.bar(left = np.arange(len(Quarters))+bar_width, height = Industry2, label = 'Industry2', color = 'indianred', width = bar_width)
plt.bar(left = np.arange(len(Quarters))+bar_width+bar_width, height = Industry3, label = 'Industry3', color = 'green', width = bar_width)
# 添加刻度標(biāo)簽(向右偏移0.225)
plt.xticks(np.arange(5)+0.4, Quarters)
# 添加y軸標(biāo)簽
plt.ylabel('生成總值(億)')
# 添加圖形標(biāo)題
plt.title('2017年各季度三產(chǎn)業(yè)總值')
# 顯示各產(chǎn)業(yè)的圖例
plt.legend()
# 顯示圖形
plt.show()

如上水平交錯(cuò)條形圖,實(shí)質(zhì)是使用兩次bar函數(shù),第二次bar函數(shù)使得條形圖往右偏移0.4個(gè)單位,第三次bar函數(shù)使得條形圖再往右偏移0.4個(gè)單位。

每一個(gè)bar函數(shù),必須控制條形圖的寬度,width=bar_width,否則會(huì)導(dǎo)致條形圖的重疊。

使用xticks函數(shù)使刻度標(biāo)簽的位置向右移0.4個(gè)單位

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容