python—matplotlib雷達圖和柱狀圖
阿新 • • 發佈:2018-11-11
一丶雷達圖
import numpy as np
import matplotlib.pyplot as plt
#標籤
labels = np.array(['智力','戰鬥力','敏捷度','身高','飯量','體重','酒量'])
#資料個數
dataLenth = 7
#資料
data = np.array([8,9,5,8,9,9,10])
angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False)
data = np.concatenate((data, [data[0]])) # 閉合 # #將資料結合起來
angles = np.concatenate((angles, [angles[0]])) # 閉合
fig = plt.figure()
ax = fig.add_subplot(121, polar=True)# polar引數!!代表畫圓形!!!!
#111代表總行數總列數位置
ax.plot(angles, data, 'bo-', linewidth=1)# 畫線四個引數為x,y,標記和顏色,閒的寬度
ax.fill(angles, data, facecolor='r', alpha=0.1)# 填充顏色和透明度
ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties="SimHei")
ax.set_title("老齊屬性分析", va='baseline', fontproperties="SimHei")
ax.set_rlim(0,10)
ax.grid(True)
plt.show()
函式和引數說明:
‘bo-’:分別代表標記顏色,標記形狀,和線型,,‘bo’代表blue和實心圓
set_rlim這是引數一個值域範圍
set_thetagrids方法用於設定極座標角度網格線顯示
grid為是否畫出圓形內部網格
fill為填充顏色和填充透明度
!!!在畫圖中如果不顯示漢字,後面可以加上fontproperties=‘SimHei’
關於標記顏色形狀和線性可參考:
https://blog.csdn.net/cjcrxzz/article/details/79627483
雷達圖如下圖所示:
二丶柱狀圖
a=[1,0.5,8,]
ax=fig.add_subplot(111)
name=['a','b','c']
#引數:顏色和標籤
ax.bar(range(len(a)),a,color='rgb',tick_label=name)
如下圖所示:
三丶在同一圖中畫雷達圖和柱狀圖
'''
matplotlib雷達圖
'''
import numpy as np
import matplotlib.pyplot as plt
#=======自己設定開始============
#標籤
labels = np.array(['智力','戰鬥力','敏捷度','身高','飯量','體重','酒量'])
#資料個數
dataLenth = 7
#資料
data = np.array([8,9,5,8,9,9,10])
#========自己設定結束============
angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False)
data = np.concatenate((data, [data[0]])) # 閉合 # #將資料結合起來
angles = np.concatenate((angles, [angles[0]])) # 閉合
fig = plt.figure()
ax = fig.add_subplot(121, polar=True)# polar引數!!111代表總行數總列數位置
ax.plot(angles, data, 'bo-', linewidth=1)# 畫線四個引數為x,y,標記和顏色,閒的寬度
ax.fill(angles, data, facecolor='r', alpha=0.1)# 填充顏色和透明度
ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties="SimHei")
ax.set_title("屬性分析", va='baseline', fontproperties="SimHei")
ax.set_rlim(0,10)
ax.grid(True)
a=[1,0.5,8,]
ax=fig.add_subplot(122)
name=['a','b','c']
ax.bar(range(len(a)),a,color='rgb',tick_label=name)
ax.set_title("柱狀圖",va='bottom', fontproperties="SimHei")
plt.show()
注:主要是add_subplot(122)設定,分別代表一幅圖中子影象行數,列數,和位置
如下圖: