Python數據可視化之12種經常使用圖表的繪製(二)——樹地圖&雷達圖&箱型圖&餅圖&圓環圖&熱力圖(附代碼和效果圖)

 


經過上篇咱們已經學會了折線圖/柱形圖/條形圖/散點圖/氣泡圖/面積圖這六種常見圖表的繪製,能夠看看這篇博文
Python數據可視化之12種經常使用圖表的繪製(一)——折線圖/柱形圖/條形圖/散點圖/氣泡圖/面積圖
如今咱們來學習一下另外六種圖表——樹地圖/雷達圖/箱型圖/餅圖/圓環圖/熱力圖的繪製

 

樹地圖

老規矩,直接上代碼~python

import numpy as np
import matplotlib.pyplot as plt
import squarify
size=np.array([3.4,0.693,0.585,0.570,0.562,0.531,
               0.530,0.524,0.501,0.478,0.468,0.436])
# 每一項所佔大小
xingzuo=np.array(['未知','摩羯座','天秤座','雙魚座','天蠍座','金牛座',
                  '處女座','雙子座','射手座','獅子座','水瓶座','白羊座'])
rate=np.array(['34%','6.93%','5.85%','5.70%','5.62%','5.31%',
               '5.30%','5.24%','5.01%','4.78%','4.68%','4.36%'])
# 每一項所佔比重
colors=['steelblue','#9999ff','red','indianred',
        'green','yellow','orange']
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus']=False
plot=squarify.plot(sizes=size,
                   label=xingzuo,
                   color=colors,
                   value=rate,
                   edgecolor='white',
                   linewidth=3)
plt.title('菊粉星座分佈',fontdict={'fontsize':12})
plt.axis('off')
plt.tick_params(top='off',right='off')
plt.savefig('C:/Users/xiaoLiu/Desktop/pythoncoding/squarify.jpg')
plt.show()

效果圖以下
在這裏插入圖片描述好看不~接下來看看更加好看的雷達圖ide

雷達圖
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(111,polar=True)
# polar=True至關於創建一個極座標系
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus']=False
dataLenth=5
angles=np.linspace(0,2*np.pi,dataLenth,endpoint=False)
labels=['溝通能力','業務理解能力','邏輯思惟能力',
        '快速學習能力','工具使用能力']
data=[2,3.5,4,4.5,5]
data=np.concatenate((data,[data[0]]))
angles=np.concatenate((angles,[angles[0]]))
plt.polar(angles,data,color='r',marker='o')
plt.xticks(angles,labels)
plt.title(label="某數據分析師的綜合評級")
plt.savefig("C:/Users/xiaoLiu/Desktop/pythoncoding/polarplot.jpg")
plt.show()

效果展現
在這裏插入圖片描述
哇,真好看工具

箱型圖
import matplotlib.pyplot as plt
import numpy as np

plt.subplot(1, 1, 1)
fig = plt.figure()
plt.figure(figsize=(8, 6))
y1 = np.array([866, 2335, 5710, 6482, 6120, 1605, 3813, 4428, 4631])
y2 = np.array([433, 1167, 2855, 3241, 3060, 802, 1906, 2214, 2315])
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
x=[y1,y2]
labels = ['註冊人數', '激活人數']
plt.boxplot(x,labels=labels,vert=True,widths=[0.2,0.5])
plt.title("騰訊qq1-9月註冊用戶數", loc='center')
plt.grid(False)
plt.legend()
plt.savefig(r"C:\Users\xiaoLiu\Desktop\keshihua\9.jpg")
plt.show()

看一下效果
在這裏插入圖片描述學習

餅圖
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(1,1,1)
fig=plt.figure()
plt.figure(figsize=(8,6))
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus']=False
labels=np.array(["東區","北區","南區","西區"])
x=np.array([8566,6482,5335,7310])
explode=[0.05,0,0,0]
labeldistance=1.1
plt.pie(x,labels=labels,autopct='%.0f%%',shadow=True,
        explode=explode,radius=1.0,labeldistance=labeldistance)
plt.title("全國各分區任務量佔比",loc="center")
plt.savefig(r"C:\Users\xiaoLiu\Desktop\keshihua\10.jpg")
plt.show()

效果圖以下
在這裏插入圖片描述
挺好看的吧~,哈哈,py大法好spa

圓環圖

在餅圖的基礎上調整wedgeprops參數便可實現圓環圖
代碼以下3d

import numpy as np
import matplotlib.pyplot as plt
plt.subplot(1,1,1)
fig=plt.figure()
plt.figure(figsize=(8,6))
x1=np.array([8556,5335,7310,6482])
x2=np.array([4283,2667,3655,3241])
labels=["東區","北區","南區","西區"]
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus']=False
plt.pie(x1,labels=labels,radius=1.0,
        wedgeprops=dict(width=0.3,edgecolor='w'))
plt.pie(x2,radius=0.7,wedgeprops=dict(width=0.3,edgecolor='w'))
plt.annotate("完成量",
             xy=(0.35,0.35),xytext=(0.7,0.45),
             arrowprops=dict(facecolor='black',arrowstyle='->'))
plt.annotate("任務量",
             xy=(0.75,0.20),xytext=(1.1,0.2),
             arrowprops=dict(facecolor='black',arrowstyle='->'))
plt.title("全國各分區任務量和完成量",loc="center")
plt.savefig(r"C:\Users\xiaoLiu\Desktop\keshihua\11.jpg")
plt.show()

效果圖
在這裏插入圖片描述code

熱力圖

擼代碼blog

import numpy as np
import matplotlib.pyplot as plt
import itertools
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus']=False
cm=np.array([[1,0.082,0.031,-0.0086],
             [0.082,1,-0.063,0.062],
             [0.031,-0.09,1,0.026],
             [-0.0086,0.062,0.026,1]])
cmap=plt.cm.cool
plt.imshow(cm,cmap=cmap)
plt.colorbar()
classes=["負債率","信貸數量","年齡","家眷數量"]
tick_marks=np.arange(len(classes))
plt.xticks(tick_marks,classes)
plt.yticks(tick_marks,classes)
for i,j in itertools.product(range(cm.shape[0]),range(cm.shape[1])):
    plt.text(j,i,cm[i,j],horizontalalignment="center")
plt.grid(False)
plt.savefig(r"C:\Users\xiaoLiu\Desktop\keshihua\12.jpg")
plt.show()

在這裏插入圖片描述

怎麼樣!好看吧~
嘻嘻圖片

相關文章
相關標籤/搜索