该图像由以下代码生成:
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib import cmlabels = ['name1','name2','name3','name4','name5','name6']data = np.array( [[ 0.000,0.120,0.043,0.094,0.037,0.045],[ 0.120,0.000,0.108,0.107,0.105,0.108],[ 0.043,0.083,0.042],[ 0.094,0.089],[ 0.037,2.440],[ 0.045,0.042,0.089,2.440,0.000]])mask = np.tri(data.shape[0],k=-1)data = np.ma.array(data,mask=mask) # Mask out the lower triangle of data.fig,ax = plt.subplots(sharex=True)im = ax.pcolor(data,edgecolors='black',linewidths=0.3)# Formatfig = plt.gcf()fig.set_size_inches(10,10)ax.set_yticks(np.arange(data.shape[0]) + 0.5,minor=False)ax.set_xticks(np.arange(data.shape[1]) + 0.5,minor=False)# Turn off the frame.ax.set_frame_on(False)ax.set_aspect('equal') # Ensure heatmap cells are square.# Want a more natural,table-like display.ax.invert_yaxis()ax.yaxis.tick_right()ax.xaxis.tick_top()ax.set_xticklabels(labels,minor=False)ax.set_yticklabels(labels,minor=False)# Rotate the upper labels.plt.xticks(rotation=90)ax.grID(False)ax = plt.gca()for t in ax.xaxis.get_major_ticks(): t.tick1On = False t.tick2On = Falsefor t in ax.yaxis.get_major_ticks(): t.tick1On = False t.tick2On = Falsefig.colorbar(im)fig.savefig('out.png',transparent=False,bBox_inches='tight',pad_inches=0)
我想应用自定义色图,以便值:
> 0-1之间是蓝色和白色的线性渐变
> 1-3之间
白色和红色的线性渐变.
任何帮助将不胜感激.
解决方法 这样做的方法不止一种.在您的情况下,最简单的方法是使用linearSegmentedcolormap.from_List并指定颜色的相对位置以及颜色名称. (如果你有均匀间隔的变化,你可以跳过元组,只做from_List(‘我的cmap’,[‘blue’,’white’,’red’]).)然后你需要指定一个手动min和最大数据(vmin和vmax kwargs到imshow / pcolor / etc).举个例子:
import matplotlib.pyplot as pltimport numpy as npfrom matplotlib.colors import linearSegmentedcolormapdata = np.array( [[ 0.000,0.000]])mask = np.tri(data.shape[0],k=-1)data = np.ma.masked_where(mask,data)vmax = 3.0cmap = linearSegmentedcolormap.from_List('mycmap',[(0 / vmax,'blue'),(1 / vmax,'white'),(3 / vmax,'red')] )fig,ax = plt.subplots()im = ax.pcolor(data,cmap=cmap,vmin=0,vmax=vmax,edgecolors='black')cbar = fig.colorbar(im)cbar.set_ticks(range(4)) # Integer colorbar tick locationsax.set(frame_on=False,aspect=1,xticks=[],yticks=[])ax.invert_yaxis()plt.show()总结
以上是内存溢出为你收集整理的在python中使用matplotlib制作自定义色彩映射表全部内容,希望文章能够帮你解决在python中使用matplotlib制作自定义色彩映射表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)