norm = matplotlib.colors.Normalize(vmin=0, vmax=60)
import pandas as pdimport numpy as npimport matplotlibimport matplotlib.cm as cmimport matplotlib.pyplot as pltplt.rcParams['font.family'] = ['Arial']
test_data = pd.read_excel('GBRT_SVR_DNN_esti_save_angle_process.xlsx')
x = test_data['true_data'].values.ravel() y = test_data['model01_estimated'].values.ravel()y2 = test_data['SVR_estimated'].values.ravel()y3 = test_data['DNN_model01_estimated'].values.ravel()
nbins = 150H, xedges, yedges = np.histogram2d(x, y, bins=nbins)H = np.rot90(H)H = np.flipud(H)Hmasked = np.ma.masked_where(H==0,H)
H2, xedges2, yedges2 = np.histogram2d(x, y2, bins=nbins)H2 = np.rot90(H2)H2 = np.flipud(H2)Hmasked2 = np.ma.masked_where(H2==0,H2)
H3, xedges3, yedges3 = np.histogram2d(x, y3, bins=nbins)H3 = np.rot90(H3)H3 = np.flipud(H3)Hmasked3 = np.ma.masked_where(H3==0,H3)
fig, (ax1, ax2,ax3) = plt.subplots(nrows=1,ncols=3,figsize=(12,3),dpi=200)
norm = matplotlib.colors.Normalize(vmin=0, vmax=60)
im1 = ax1.pcolormesh(xedges, yedges, Hmasked, cmap=cm.get_cmap('jet'),norm=norm)
im2 = ax2.pcolormesh(xedges2, yedges2, Hmasked2, cmap=cm.get_cmap('jet'), norm=norm)
im3 = ax3.pcolormesh(xedges3, yedges3, Hmasked3, cmap=cm.get_cmap('jet'), norm=norm,)
fig.subplots_adjust(right=0.9)position = fig.add_axes([0.92, 0.12, 0.015, .78 ])cb = fig.colorbar(im3, cax=position)
colorbarfontdict = {"size":15,"color":"k",'family':'Times New Roman'}cb.ax.set_title('Counts',fontdict=colorbarfontdict,pad=8)cb.ax.tick_params(labelsize=11,direction='in')cb.ax.set_yticklabels(['0','10','20','30','40','50','>60'],family='Times New Roman')fig.suptitle('One Colorbar for Multiple Plot ',size=20,family='Times New Roman',x=.5,y=1.05)plt.savefig(r'E:\Data_resourses\DataCharm 公眾號\Python\學術圖表繪製\scatter_One_Colorbar.png', width=7,height=5,dpi=900,bbox_inches='tight')plt.show()colorbarfontdict = {"size":15,"color":"k",'family':'Times New Roman'}cb.ax.set_title('Counts',fontdict=colorbarfontdict,pad=8)cb.ax.tick_params(labelsize=11,direction='in')cb.ax.set_yticklabels(['0','10','20','30','40','50','>60'],family='Times New Roman')此外,我們設置colorbar也不是只繪製最後一個子圖的colorbar,而其他子圖不繪製,那樣容易導致子圖大小不一。這裡單獨繪製了colorbar,代碼如下:fig.subplots_adjust(right=0.9)position = fig.add_axes([0.92, 0.12, 0.015, .78 ])cb = fig.colorbar(im3, cax=position)這也是多子圖共用一個colorbar避免大小不一的一個小技巧,希望大家可以記住。可以看出顏色密集部分出現「光滑」處理,其實就是通過核密度估計函數將 真實值和預測值之間進行密度值估計,再進行一個排序即可,詳細代碼如下:
import numpy as npimport matplotlib.pyplot as pltfrom scipy.stats import gaussian_kde
x = test_data['true_data'].values.ravel() y = test_data['model01_estimated'].values.ravel()xy = np.vstack([x,y])z = gaussian_kde(xy)(xy)idx = z.argsort()x, y, z = x[idx], y[idx], z[idx]fig, ax = plt.subplots(figsize=(7,5),dpi=200)ax.scatter(x, y, c=z, s=3, edgecolor='')plt.savefig(r'E:\Data_resourses\DataCharm 公眾號\Python\學術圖表繪製\scatter_gaussian_kde.png', width=7,height=5,dpi=900,bbox_inches='tight')plt.show()idx = z.argsort()x, y, z = x[idx], y[idx], z[idx]
如需聯繫EasyShu團隊
請加微信:EasyCharts
微信公眾號【EasyShu】博文代碼集合地址
https://github.com/Easy-Shu/EasyShu-WeChat
《Python數據可視化之美》-配套原始碼下載地址
https://github.com/Easy-Shu/Beautiful-Visualization-with-python
《R語言數據可視化之美》-增強版配套原始碼下載地址
https://github.com/Easy-Shu/Beautiful-Visualization-with-R