Search code examples
pythonpython-3.xnumpymatplotlibcolorbar

What are numbers of the colorbar in a 2d histogram?


I have plot a 2D histogram with the following python code:

import numpy as np
import matplotlib.pyplot as plt
import pylab
import matplotlib.ticker as ticker
x,y,z,a = np.loadtxt('bca_16_t1.txt', unpack=True, delimiter=',')
plt.hist2d(a, z,bins=(200, 200), cmap=plt.cm.jet)
plt.ylim([2.0, 4.6])
plt.xlim([700, 1300])
ax = plt.axes()
ax.xaxis.set_major_locator(ticker.MultipleLocator(50))
ax.yaxis.set_major_locator(ticker.MultipleLocator(0.2))
plt.grid()
plt.colorbar(fraction=0.15, shrink=1.0, aspect=20)
plt.show()

I have the following questions about it:

  1. How do i remove the white space at the left most end of the plot?
  2. What is the unit of the numbers against the colour bar and what do those numbers mean?

Any help regarding this will be much appreciated. Thank you


Solution

  • I figured it out.

    1. How do i remove the white space at the left most end of the plot?

      A. The white space is due to improper X-axis scale. Remove the lower limit of the X-axis scale to get rid of white space. To set only upper limit of the X-axis scale, add the following line:

      plt.xlim(xmax=1300)

    2. What is the unit of the numbers against the colour bar and what do those numbers mean?

      A. The unit of the colour bar is binned 'Events' or 'Data points'. You can change the scale by changing the 'bins' parameter in plt.hist2d.