【问题标题】:Matplotlib- How to make color fill bias towards max & min values?Matplotlib-如何使颜色填充偏向最大值和最小值?
【发布时间】:2017-01-20 08:53:00
【问题描述】:

问题

我有两个变量的相关图,其中大部分值接近 -1 或 1。我使用的是地震颜色图(红色和蓝色,中间是白色),但大部分图是深蓝色(接近 -1)或深红色(接近 1),在最小值和最大值附近显示很少的细节。

代码

这是我用于绘图的代码块。

#Set variables
lonlabels = ['0','45E','90E','135E','180','135W','90W','45W','0']
latlabels = ['90S','60S','30S','Eq.','30N','60N','90N']
bounds = np.array([-1.0,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1.0])

#Create basemap
fig,ax = plt.subplots(figsize=(15.,10.))
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,llcrnrlon=0,urcrnrlon=360.,lon_0=180.,resolution='c')
m.drawcoastlines(linewidth=1,color='w')
m.drawcountries(linewidth=1,color='w')
m.drawparallels(np.arange(-90,90,30.),linewidth=0.3)
m.drawmeridians(np.arange(-180.,180.,45.),linewidth=0.3)   
meshlon,meshlat = np.meshgrid(lon,lat)
x,y = m(meshlon,meshlat)

#Plot variable
corre = m.pcolormesh(x,y,corrcoef,cmap='seismic', shading='gouraud',vmin=-1.0,vmax=1.0)

#Set titles & labels
#Colorbar
cbar = m.colorbar(corre,size="8%",ticks=bounds,location='bottom',pad=0.8)
cbar.set_label(label='Correlation Coefficient',size=25)
cbar.set_ticklabels(bounds)
for t in cbar.ax.get_xticklabels():
     t.set_fontsize(25)
#Titles
fig.suptitle('Correlation of Local Precipitation to Global (CanESM2)',fontsize=30,x=0.51,y=0.92)
ax.set_xlabel('Longitude',fontsize=25)
ax.set_xticks(np.arange(0,  405,45))
ax.set_xticklabels(lonlabels,fontsize=20)
ax.set_ylabel('Latitude', fontsize=25)
ax.set_yticks(np.arange(-90,120,30))
ax.set_yticklabels(latlabels,fontsize=20)

这是它生成的情节。

问题

我想调整颜色填充方案,以便颜色图的中间部分,例如 -0.9 到 0.9 范围,被压缩(几乎像中断但不完全)并且颜色填充更好地定义了值结束。我怎样才能做到这一点?类似于对称对数分布,但偏向最大值和最小值,而不是中间值。

【问题讨论】:

    标签: python matplotlib colors colormap


    【解决方案1】:

    有一个关键字参数 norm 可以与 pcolormesh 一起使用,以更改颜色映射的比例。为此,请查看matplotlib documentation。然后你可以使用参数linthresh来改变中间范围。 我还没有尝试过,但我认为它可能会解决您的问题。

    【讨论】:

    • 啊,它有效!我不仅要使用 linthresh,还要使用 linscale 来压缩中间值。否则,没有 linscale 的 linthresh 会将值压缩到最大值和最小值。我将不得不稍微调整一下数字,但效果正是我想要的。谢谢!
    • 我很高兴它有帮助 :-)
    猜你喜欢
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2015-04-03
    • 2018-03-05
    • 2022-01-02
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    相关资源
    最近更新 更多