I would like to plot a 2D histogram that includes both positive and negative numbers. I have the following code which uses pcolormesh but I am unable to specify the color levels to force the white color to corresponds to zero (i.e., I want my colorbar to be symmetric around zero). I've also tried imshow.
I know you can specify colour levels in plt.contour and plt.contourf but I can't find a way to plot the 2D histogram using blocks.
Any advice would be greatly appreciated.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm as CMfig = plt.figure()# create an example histogram which is asymmetrical around zero
x = np.random.rand(400)
y = np.random.rand(400)
Z, xedges, yedges = np.histogram2d(x, y, bins=10)
Z = Z - 2.plt.pcolormesh(xedges, yedges, Z, cmap=CM.RdBu_r)plt.colorbar()
plt.savefig('test.png')