elizevin
elizevin

Reputation: 111

Mapping two subplots on the same colorbar

I have a problem mapping the two histogram subplots with different ranges (8.53,9.09) and (9.55,10.83) to one colorbar. But colorbar is drawn using pcolormesh from first or second subplot, and because they don't have overlap the colorbar don't display correct color.

#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()

I would like my colorbar to show values (colors) from smallest of the median values to largest (from vmin2=8.53 to vmax1=10.83) and to histograms show correct colors. This code as it is shows: colorbar mapped to first histogram (range of color (9.55,10.83)), and colors of the subplots are not connected so I have dark blue for 9.55 on first subplot and for 8.53 on second, and brightest yellow for 10.83 on first subplot and for 9.09 on second.

Please note: this post Set Colorbar Range in matplotlib should answer my question, but doesn't work for me, I'm not sure why. In it the ranges of color overlap, so they can use last od the images in plt.colorbar. I can't do that because my ranges don't overlap, I just want to expand colorbar to include both ranges.

Please help!

EDIT:

enter image description here

Upvotes: 0

Views: 625

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

The following should be a minimal example of the problem with a solution, namely to use the same norm for both plots.

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

Upvotes: 1

Related Questions