我希望使用matplotlib制作多个x y数据的hexbin密度图,类似于这个例子:http://matplotlib.org/1.4.0/examples/pylab_examples/hexbin_demo.html。但是我想将每个六边形的计数除以给定数字(我的密度图中的最高峰值),以便所有的密度图都有相同的颜色,并且颜色条对于所有的图都是[0,1]范围内的。请问是否可以给我一个工作的示例?谢谢!
plt.subplot(111)
hb = plt.hexbin(x,y, cmap=plt.cm.YlOrRd_r)
plt.cla()
plt.hexbin(x, y,
C=np.ones_like(y, dtype=np.float) / hb.get_array().max(),
cmap=plt.cm.YlOrRd_r,
reduce_C_function=np.sum)
plt.axis([xmin, xmax, ymin, ymax])
cb = plt.colorbar()
C
数组才能使用reduce_C_function
选项。在这种情况下,C=np.ones_like(y) / hb.get_array().max()
就足够了,因为您只需要对值求和。plt.gca().set_axis_bgcolor(plt.cm.YlOrRd_r(0))
。plt.subplot(111)
hb = plt.hexbin(x,y, cmap=plt.cm.YlOrRd_r)
plt.axis([xmin, xmax, ymin, ymax])
cb = plt.colorbar()
cb.set_ticks(np.linspace(hb.get_array().min(), hb.get_array().max(), 6))
cb.set_ticklabels(np.linspace(0, 1., 6))
@farenorth的第二种方法非常好。 也可以使用以下方法来获取六边形密度图:
cb.set_ticks(np.linspace(hb.get_array().min(), hb.get_array().max(), 6))
cb.set_ticklabels(['%.4f'%x for x in np.linspace(hb.get_array().min()/hb.get_array().sum(), hb.get_array().max()/hb.get_array().sum(), 6)])