我试图为某些数据生成热图。我需要积极的蓝色渐变和负红色渐变。
import matplotlib.pyplot as plt
import numpy as np
arr = array([[ 2.155, 3.093, -5.377, 7.973, 6.564, 6.348, 10.279,
1.536],
[ 0.355, -2.848, 0.65 , 6.877, 1.08 , 4.001, 11.427, 0. ],
[ -4.23 , -2.855, -2.507, 2.44 , 2.78 , 1.163, 27.868, 0. ],
[ -5.373, 1.429, -4.247, -3.534, -7.044, 0.688, 3.255, 0. ],
[ -5.364, -3.792, -3.203, -7.212, -5.303, 0.928, -3.028, 0. ],
[ 1.257, -5.088, -4.209, -8.229, -3.4 , -7.856, -29.007, 0. ],
[ -2.956, -4.787, -0.855, -7.551, -5.735, -3.882, -4.183, 0. ],
[ -4.307, -19.643, -0.422, -5.797, -12.939, -9.516, 4.866, 0. ],
[-22.003, -20.167, -17.983, -46.882, -44.439, 2.678, 3.308, 0. ],
[ -4.706, -8.41 , 0.885, -12.78 , -13.647, 10.902, 6.622, 0. ],
[ -6.822, -2.396, 2.941, 3.965, -5.588, 14.765, 11.879, 0. ],
[ -2.674, 0.173, 8.768, 9.349, 4.73 , 10.9 , 12.983, 0. ]])
x_header = [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014]
y_header = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
fig, ax = plt.subplots()
heatmap = plt.pcolor(arr, cmap = 'RdBu')
ax.set_xticks(np.arange(arr.shape[1]), minor=False)
ax.set_yticks(np.arange(arr.shape[0])+0.5, minor=False)
ax.set_xticklabels(x_header)
ax.set_yticklabels(y_header)
ax.xaxis.tick_top()
ax.invert_yaxis()
plt.show()
但是,我没有得到我选择的渐变。我得到下图。如果您注意到大多数负值都具有蓝色渐变,并且只有少数值具有红色渐变。 是否有办法强制将特定颜色渐变用于正值,将另一种颜色用于负值?
感谢。