Skip to content
Advertisement

Color bar limits doesn’t follow the data limits

I am trying to make plot with two 2d histograms. And i need them both to have a colorbar. But, i was able to plot the colobar at the lateral of one of these histograms, but as can see in the image below, the limits of the color bar doesn’t reflect the limits of the histogram. The bin with higher number of events should have around 830 events. (ps: i had set the limits of the colorbar by hand using im2.set_clim(vmin=0, vmax=10000), but before that it was from -20 to 20) .

enter image description here

The code i used to plotis something like:

plt.set_cmap('gnuplot')


fig = plt.figure()

fig.set_size_inches(10, 10)


ax = fig.add_subplot(121,aspect='equal')

ax2 = fig.add_subplot(122, aspect='equal')



ax2.hist2d(data2,datay2,bins=(100,100), rasterized=True,range=np.array([(-7.9, 7.9), (-7.9, 7.9)]))
ax2.set_title('HMC with L=5.0 and t=1.5 ', size= 16, fontname='Comic Sans MS')
ax2.tick_params(axis='both', which='major', labelsize=10)
ax2.tick_params(axis='both', which='minor', labelsize=10)

ax.hist2d(data,datay,bins=(100,100), rasterized=True,range=np.array([(-7.9, 7.9), (-7.9, 7.9)]))
ax.set_title('NUTS ', size= 16, fontname='Comic Sans MS')
ax.tick_params(axis='both', which='major', labelsize=10)
ax.tick_params(axis='both', which='minor', labelsize=10)

#down here i am trying to plot the color map
im2 = ax2.imshow(chain,cmap ='gnuplot' ,interpolation='None')
divider = make_axes_locatable(ax2)
cax = divider.append_axes('right', size='5%', pad=0.05) 
im2.set_clim(vmin=0, vmax=10000)
fig.colorbar(im2, cax=cax, orientation='vertical');
fig.text(0.5, 0.02, 'Generalized Coordinate', ha='center', size = 16)
fig.text(0.07, 0.5, 'Generalized Momenta', va='center', rotation='vertical',size = 16)
plt.savefig('2DHMCpog.pdf') 
plt.savefig('2DHMCpog.png') 
plt.show()    

Advertisement

Answer

From this answer, you can use the return values of hist2d to set a local color bar. E.g.:

h = ax2.hist2d(data2,datay2,bins=(100,100), …
fig.colorbar(h[3], ax=ax2)
ax2.set_title('HMC with …

Do the same for the second histogram.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement