在同一颜色条上映射两个子图

问题描述 投票:0回答:1

我有一个问题,将不同范围(8.53,9.09)和(9.55,10.83)的两个直方图子图映射到一个颜色条。但是使用pcolormesh从第一个或第二个子图绘制颜色条,并且因为它们没有重叠,所以颜色条不显示正确的颜色。

#first histogram subplot

binx=np.linspace(9.1,11.35,20)
biny=np.linspace(-1.45,0.8,20)
median=np.random.uniform(9.55,10.83, size=(20,20))
#finding min and max values of median
dim=np.size(median)
median1=np.reshape(median, (dim, ))
median1=median1[median1!=0]
vmin1=np.sort(median1)[0]
vmax1=np.sort(median1)[-1]  


hist1=ax1.pcolormesh(binx, biny, median.T, norm=LogNorm(), cmap='viridis')
hist1.set_clim(vmin1,vmax1)

#the second subplot histogram
man_mass=np.linspace(9.1,11.35,16)
man_sfr=np.linspace(-1.45,0.8,16)

man_med=np.array([[0.,0.,0.,0.,0.,0.,0.,8.56,0.,0.,0.,0.,0.,0.,0.,0.],
                 [0.,0.,0.,0.,0.,8.7,8.7,8.65,8.58,8.53,0.,0.,0.,0.,0.,0.],
                 [0.,0.,8.77,8.76,8.73,8.75,8.76,8.71,8.69,8.64,8.58,8.53,0.,0.,0.,0.],
                 [0.,8.83,8.82,8.82,8.81,8.79,8.79,8.77,8.78,8.74,8.69,8.66,8.59,0.,0.,0.],
                 [8.90,8.90,8.90,8.88,8.88,8.87,8.86,8.85,8.83,8.81,8.79,8.72,8.69,8.64,8.63,0.],
                 [8.98,8.96,8.95,8.94,8.94,8.93,8.93,8.92,8.90,8.88,8.85,8.82,8.77,8.72,8.71,0.],
                 [9.02,9.01,8.99,8.98,8.98,8.98,8.98,8.97,8.96,8.94,8.92,8.89,8.85,8.82,8.79,0.],
                 [9.05,9.04,9.03,9.02,9.02,9.02,9.01,9.01,9.01,8.99,8.97,8.96,8.92,8.88,8.86,8.84],
                 [0.0,9.06,9.05,9.04,9.04,9.03,9.03,9.03,9.03,9.03,9.02,8.99,8.98,8.94,8.90,0.0],
                 [0.0,9.08,9.07,9.05,9.05,9.05,9.05,9.05,9.05,9.05,9.04,9.04,9.01,9.0,8.97,8.93],
                 [0.0,9.09,9.07,9.06,9.06,9.06,9.06,9.06,9.06,9.06,9.06,9.05,9.04,9.03,9.0,8.98],
                 [0.0,0.0,9.09,9.07,9.06,9.07,9.06,9.06,9.06,9.06,9.07,9.07,9.06,9.05,9.04,9.02],
                 [0.0,0.0,0.0,9.09,9.08,9.08,9.07,9.07,9.07,9.07,9.07,9.07,9.06,9.07,9.06,9.04],
                 [0.0,0.0,0.0,0.0,0.0,9.09,9.07,9.07,9.07,9.07,9.07,9.06,9.07,9.07,9.07,0.0],
                 [0.0,0.0,0.0,0.0,0.0,0.0,9.09,9.08,9.08,9.07,9.07,9.07,9.07,9.08,9.05,0.0],
                 [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.06,9.07,9.07,9.06,9.06,0.0,0.0,0.0] ])

#max and min values of second median
dim=np.size(man_med)
man_med1=np.reshape(man_med, (dim, ))
man_med1=man_med1[man_med1!=0]
#print np.partition(median1,1)[1]
vmin2=np.sort(man_med1)[0]
vmax2=np.sort(man_med1)[-1]

hist2=ax2.pcolormesh(man_mass, man_sfr, man_med.T, norm=LogNorm(), cmap='viridis')
hist2.set_clim(vmin1,vmax1)

#pozition and spacing of subplots
plt.subplots_adjust(wspace=0,hspace=0,left=0.2, right=0.85, bottom=0.3)

p0 = ax1.get_position().get_points().flatten()
p1 = ax2.get_position().get_points().flatten()


#colorbar
ax_cbar = fig.add_axes([p0[0], 0.1, p1[2]-p0[0], 0.03])
cbar=plt.colorbar(hist1, cax=ax_cbar, orientation='horizontal')


plt.show()
plt.close()

我希望我的颜色条显示从最小中值到最大值(从vmin2 = 8.53到vmax1 = 10.83)的值(颜色),并且直方图显示正确的颜色。这个代码显示:colorbar映射到第一个直方图(颜色范围(9.55,10.83)),子图的颜色没有连接所以我在第一个子图上有9.55的深蓝色,在第二个子图上有8.53个,最亮的是黄色在第一个子图上为10.83,在第二个子图上为9.09。

请注意:这篇文章Set Colorbar Range in matplotlib应该回答我的问题,但对我不起作用,我不知道为什么。在它的颜色范围重叠,所以他们可以使用plt.colorbar中的图像。我不能这样做,因为我的范围不重叠,我只是想扩展颜色条以包括两个范围。

请帮忙!

编辑:

enter image description here

python matplotlib colorbar
1个回答
1
投票

以下应该是解决方案问题的最小示例,即对两个图使用相同的norm

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

x = np.arange(11)

a = np.linspace(1,5,100).reshape((10,10))
b = np.linspace(6,10,100).reshape((10,10))

fig, (ax1, ax2) = plt.subplots(ncols=2)

norm=LogNorm(min(a.min(),b.min()), max(a.max(),b.max()))
p1 = ax1.pcolormesh(x,x,a, norm=norm)
p2 = ax2.pcolormesh(x,x,b, norm=norm)

fig.colorbar(p1, ax=ax1)
fig.colorbar(p2, ax=ax2)

fig.tight_layout()
plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.