Python利用imshow制作自定義漸變填充柱狀圖(colorbar)
在各種各樣的理論計算中,常常需要繪制各種填充圖,繪制完后需要加漸變填充的colorbar??墒怯行┸浖鏥MD,colorbar渲染后顏色分布有些失真,不能較準確的表達各顏色對應的數(shù)值。用ps中的漸變填充可以解決該問題,但很多電腦配置較低,不能很好的運行ps。Python也可以直接繪制colorbar,填充顏色就好。如cmap中的bwr漸變本人就比較常用。然而,有時候顏色范圍是負數(shù)范圍多于正數(shù)范圍(如:colorbar需要表示 [-60,40]這段,藍色表示負數(shù),紅色表示正數(shù),白色應該在colorbar由下往上60%處),bwr漸變將white置于50%處顯得不夠合理,因此需要自定義填充。本文以imshow() 函數(shù)來進行填充柱狀圖達到自定義colorbar的目的。interpolation=‘bicubic’ 可以很好的做出漸變效果。
代碼
# -*- coding: utf-8 -*-'''Created on Wed Dec 9 10:36:54 2020@author: fya'''import matplotlib.pyplot as pltimport numpy as npfrom matplotlib.colors import ListedColormap,LinearSegmentedColormapimport matplotlib as mplfig, ax = plt.subplots(dpi=96)ax.set(xlim=(1,10), ylim=(-0.1,101), autoscale_on=False) #創(chuàng)建圖像范圍a = np.array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]) #每種漸變色分成五段(array五行),數(shù)字表示在colormap對應的深淺print(a.shape)clist=[’white’,’blue’] #線性變化顏色由上面array值 小到大,越小,越白,達到上白下藍的漸變效果clist2=[’red’,’white’] #漸變色2,用于白色到紅色填充,array越小,越紅,達到上紅下白的效果newcmp = LinearSegmentedColormap.from_list(’chaos’,clist)newcmp2 = LinearSegmentedColormap.from_list(’chaos’,clist2)plt.imshow(a,cmap=newcmp,interpolation=’bicubic’,extent=(1,10,0,60))#60%都是藍色到白色漸變plt.imshow(a,cmap=newcmp2,interpolation=’bicubic’,extent=(1,10,60,100)) #白色設置在60%處frame = plt.gca() #讀取當前圖層ax.yaxis.tick_right() #縱坐標移到右邊ax.set_yticklabels((’-80’,’-60’,’-40’,’-20’,’0’,’20’,’40’)) #自定義yticks顯示的值,第一個label不顯示frame.spines[’top’].set_visible(False) #上框線不顯示frame.spines[’bottom’].set_visible(False)frame.spines[’right’].set_visible(False)frame.spines[’left’].set_visible(False)plt.xticks([]) #x坐標不要plt.show()fig.savefig(’colorbar.tif’,dpi=600,format=’tif’)print(’Done!’)#N = 10#x = np.arange(N) + 0.15#y = np.random.rand(N)#width = 0.4#for x, y in zip(x, y): #ax.imshow(a, interpolation=’bicubic’, extent=(x, x+width, 0, y), cmap=plt.cm.Blues_r)#ax.set_aspect(’auto’)#plt.show()
代碼2,漸變色分100段
# -*- coding: utf-8 -*-'''Created on Wed Dec 9 10:36:54 2020@author: fanyiang'''import matplotlib.pyplot as pltimport numpy as npfrom matplotlib.colors import ListedColormap,LinearSegmentedColormapimport matplotlib as mplimport pandas as pdimport osfig, ax = plt.subplots(dpi=96)ax.set(xlim=(1,10), ylim=(-0.1,101), autoscale_on=False)#a = np.array([[1, 1], #[2, 2], #[3, 3], #[4, 4], #[5, 5]]) #每種漸變色分成五段(array五行),數(shù)字表示在colormap對應的深淺avalue=locals() dfvalue=locals() for i in range(1,101): avalue[’a’+str(i)]=np.array([[i,i]]) #漸變色分為100段,分的更細 dfvalue[’df’+str(i)]=pd.DataFrame(avalue[’a’+str(i)]) #轉(zhuǎn)dataframe df=dfvalue[’df’+str(i)] df.to_csv('temp.csv', mode=’a’,header=None) #暫存csv文件,第一列會把每一次循環(huán)的index放進去df3=pd.read_csv(’temp.csv’,header=None)#讀取csvdf3.columns=[’序號’,’x’,’y’]#column命名,第一列廢棄df3=df3.drop(’序號’,axis=1)#刪除第一列a=np.array(df3) #轉(zhuǎn)arrayprint(df3.head())#a=np.vstack((a1,a2,a3,a4,a5,a6,a7,a8,a9,a10))print(a)clist=[’white’,’blue’] #線性變化顏色由上面array值 小到大clist2=[’red’,’white’]newcmp = LinearSegmentedColormap.from_list(’chaos’,clist)newcmp2 = LinearSegmentedColormap.from_list(’chaos’,clist2)plt.imshow(a,cmap=newcmp,interpolation=’bicubic’,extent=(1,10,0,60))plt.imshow(a,cmap=newcmp2,interpolation=’bicubic’,extent=(1,10,60,100)) #白色設置在60%處frame = plt.gca() #讀取當前圖層ax.yaxis.tick_right() #縱坐標移到右邊ax.set_yticklabels((’-80’,’-60’,’-40’,’-20’,’0’,’20’,’40’)) #自定義yticks顯示的值,第一個label不顯示frame.spines[’top’].set_visible(False) #上框線不顯示frame.spines[’bottom’].set_visible(False)frame.spines[’right’].set_visible(False)frame.spines[’left’].set_visible(False)plt.xticks([]) #x坐標不要plt.show()fig.savefig(’colorbar.tif’,dpi=600,format=’tif’)os.remove('temp.csv') #刪除臨時的csv文件print(’Done!’)#N = 10#x = np.arange(N) + 0.15#y = np.random.rand(N)#width = 0.4#for x, y in zip(x, y): #ax.imshow(a, interpolation=’bicubic’, extent=(x, x+width, 0, y), cmap=plt.cm.Blues_r)#ax.set_aspect(’auto’)#plt.show()效果
效果1
效果2
到此這篇關于Python利用imshow制作自定義漸變填充柱狀圖(colorbar)的文章就介紹到這了,更多相關Python 漸變填充柱狀圖內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章: