今天使用python中的matplotlib庫繪制3D柱形圖,以下就是畫圖的完成代碼亏钩。
from mpl_toolkits.mplot3dimport Axes3D
import matplotlib.pyplotas plt
import numpyas np
#設(shè)置x軸取值
xedges = np.array([10,20,30,40,50,60,70])
#設(shè)置y軸取值
yedges = np.array([10,20,30,40,50,60,70])
#設(shè)置X,Y對(duì)應(yīng)點(diǎn)的值剑肯。即原始數(shù)據(jù)捧毛。
hist =np.array( [[0.0964,0.1024,0.1043,0.1057,0.1072,0.1100],
[0.1027,0.1039,0.1057,0.1069,0.1078,0.1109],
[0.1046,0.1059,0.1061,0.1079,0.1085,0.1114],
[0.1068,0.1079,0.1084,0.1091,0.1096,0.1127],
[0.1091,0.1089,0.1107,0.1112,0.1118,0.1131],
[0.1102,0.1113,0.1121,0.1129,0.1133,0.1157]])
#生成圖表對(duì)象。
fig = plt.figure()
#生成子圖對(duì)象,類型為3d
ax = fig.add_subplot(111,projection='3d')
#設(shè)置作圖點(diǎn)的坐標(biāo)
xpos, ypos = np.meshgrid(xedges[:-1]-2.5 , yedges[:-1]-2.5 )
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
#設(shè)置柱形圖大小
dx =5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()
#設(shè)置坐標(biāo)軸標(biāo)簽
ax.set_xlabel('R')
ax.set_ylabel('K')
ax.set_zlabel('Recall')
# x, y, z: array - like
# The coordinates of the anchor point of the bars.
# dx, dy, dz: scalar or array - like
# The width, depth, and height of the bars, respectively.
# minx = np.min(x)
# maxx = np.max(x + dx)
# miny = np.min(y)
# maxy = np.max(y + dy)
# minz = np.min(z)
# maxz = np.max(z + dz)
ax.bar3d(xpos, ypos, zpos, dx, dy, dz,color='b',zsort='average')
plt.show()
瀏覽過完整的繪圖代碼之后呀忧,接下來梳理一下制作3D柱形圖的流程师痕。
整個(gè)流程分為三個(gè)部分,導(dǎo)入繪圖數(shù)據(jù)而账,設(shè)置圖表參數(shù)以及圖片展示胰坟。
1. 導(dǎo)入繪圖數(shù)據(jù)。
一般而言泞辐,需要繪制3D柱形圖的形式為:由兩個(gè)維度的參數(shù)確定另外一個(gè)維度的值笔横。即X軸的值與Y軸的值組合而成Z軸的值。因此在繪制3D柱形圖時(shí)需要導(dǎo)入三組數(shù)據(jù)咐吼,即每個(gè)坐標(biāo)軸的取值吹缔。
#設(shè)置x軸取值
xedges = np.array([10,20,30,40,50,60,70])
#設(shè)置y軸取值
yedges = np.array([10,20,30,40,50,60,70])
#設(shè)置X,Y對(duì)應(yīng)點(diǎn)的值。即原始數(shù)據(jù)锯茄。
hist =np.array( [[0.0964,0.1024,0.1043,0.1057,0.1072,0.1100],
[0.1027,0.1039,0.1057,0.1069,0.1078,0.1109],
[0.1046,0.1059,0.1061,0.1079,0.1085,0.1114],
[0.1068,0.1079,0.1084,0.1091,0.1096,0.1127],
[0.1091,0.1089,0.1107,0.1112,0.1118,0.1131],
[0.1102,0.1113,0.1121,0.1129,0.1133,0.1157]])
需要注意的是厢塘,這里用于接收對(duì)應(yīng)坐標(biāo)軸數(shù)據(jù)的數(shù)據(jù)類型要用numpy庫中的array方法轉(zhuǎn)化,它屬于numpy.ndarray類型肌幽。這是后續(xù)簡便做圖的準(zhǔn)備之一晚碾,如果這里直接使用list類型來接收,那么后續(xù)步驟會(huì)報(bào)錯(cuò)喂急。
2. 設(shè)置圖表參數(shù)格嘁。
完成繪圖數(shù)據(jù)的導(dǎo)入之后,接下來就需要設(shè)置圖表參數(shù)煮岁。
- 首先創(chuàng)建一個(gè)圖表對(duì)象并將其類型設(shè)置為3D
#生成圖表對(duì)象讥蔽。
fig = plt.figure()
#生成子圖對(duì)象,類型為3d
ax = fig.add_subplot(111,projection='3d')
- 接著設(shè)置坐標(biāo)軸的偏移程度画机。由于是做3D圖形冶伞,則X軸與Y軸在做圖點(diǎn)上需要適當(dāng)偏離一些,這樣繪制的圖片從視覺上看起來會(huì)更自然步氏。
#設(shè)置作圖點(diǎn)的坐標(biāo)
xpos, ypos = np.meshgrid(xedges[:-1]-2.5 , yedges[:-1]-2.5 )
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
flatten()函數(shù)是將多維數(shù)組轉(zhuǎn)換成一維數(shù)組响禽,便于做圖。
- 最后需要設(shè)置柱形圖中圓柱的大小以及坐標(biāo)軸的標(biāo)簽荚醒。
#設(shè)置柱形圖大小
dx =5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()
#設(shè)置坐標(biāo)軸標(biāo)簽
ax.set_xlabel('R')
ax.set_ylabel('K')
ax.set_zlabel('Recall')
3. 圖片展示芋类。
在完成了前兩個(gè)步驟之后,接下來就需要調(diào)用繪圖函數(shù)將繪圖數(shù)據(jù)以及圖表參數(shù)相結(jié)合并展示繪制而成的圖片效果界阁。
# x, y, z: array - like
# The coordinates of the anchor point of the bars.
# dx, dy, dz: scalar or array - like
# The width, depth, and height of the bars, respectively.
# minx = np.min(x)
# maxx = np.max(x + dx)
# miny = np.min(y)
# maxy = np.max(y + dy)
# minz = np.min(z)
# maxz = np.max(z + dz)
ax.bar3d(xpos, ypos, zpos, dx, dy, dz,color='b',zsort='average')
plt.show()
關(guān)于bar3d()函數(shù)侯繁,查閱其源代碼發(fā)現(xiàn)內(nèi)置的各參數(shù)的意義,包括在第2部分中提到的柱形圖中柱子大小的設(shè)定泡躯,以及坐標(biāo)軸的取值范圍等贮竟。
不過比較尷尬的地方在于丽焊,從坐標(biāo)軸取值范圍的計(jì)算公式可知,它與柱子大小的參數(shù)綁定在一起咕别,這就不利于靈活設(shè)置坐標(biāo)軸的取值范圍(相比于2d圖形而言)技健。
上述代碼的運(yùn)行結(jié)果如下: