matplotlib: How can you specify colour levels in a 2D historgram

落花浮王杯 提交于 2019-12-04 08:12:01

Add vmin and vmax parameters with equal absolute values

plt.pcolormesh(xedges, yedges, Z, cmap=CM.RdBu_r, vmin=-7, vmax=7)

and see if you like the result

Thanks to http://nbviewer.ipython.org/gist/pelson/5628989

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import from_levels_and_colors

x = np.random.rand(400)
y = np.random.rand(400)
Z, xedges, yedges = np.histogram2d(x, y, bins=10)
Z = Z - 2.
#  -1 0 3 6 9
cmap, norm = from_levels_and_colors([-1, 0, 3, 6, 9, 12], ['r', 'b', 'g', 'y', 'm']) # mention levels and colors here
plt.pcolormesh(xedges, yedges, Z, cmap=cmap, norm=norm)
plt.colorbar()
plt.show()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!