2. 代码

# -- 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) #创建图像范围a = np.array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]) #每种渐变色分成五段(array五行),数字表示在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.showfig.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五行),数字表示在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)]) #转dataframe df=dfvalue['df'+str(i)] df.to_csv(\"大众temp.csv\"大众, mode='a',header=None) #暂存csv文件,第一列会把每一次循环的index放进去df3=pd.read_csv('temp.csv',header=None)#读取csvdf3.columns=['序号','x','y']#column命名,第一列废弃df3=df3.drop('序号',axis=1)#删除第一列a=np.array(df3) #转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.showfig.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

代码3,变动方法2中要暂时存到dataframe的问题

# -- coding: utf-8 --\"大众\"大众\"大众Created on Fri Dec 11 10:40:53 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五行),数字表示在colormap对应的深浅avalue=locals a=[[1,1]] for i in range(2,1001): avalue['a'+str(i)]=np.array([[i,i]]) #渐变色分为100段,分的更细 a=np.vstack((a,avalue['a'+str(i)])) #直接用vstack来vertical叠加数组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.showfig.savefig('colorbar.tif',dpi=600,format='tif')#os.remove(\"大众temp.csv\"大众) #删除临时的csvprint('Done!')

注:该方法紧张改变在于

a=[[1,1]] for i in range(2,1001): avalue['a'+str(i)]=np.array([[i,i]]) #渐变色分为100段,分的更细 a=np.vstack((a,avalue['a'+str(i)])) #直接用vstack来vertical叠加数组

Python运用imshow制作自定义渐变填充柱状图/colorbar

3. 代码

效果1

效果2&3