import numpy as np
# 高斯分布
mean = [0,0]
cov = [[0,1],[1,0]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T
- 使用NumPy 的 histogram2d 函数
from matplotlib import pyplot as plt
hist, xedges, yedges = np.histogram2d(x,y)
X,Y = np.meshgrid(xedges,yedges)
plt.imshow(hist)
plt.grid(True)
plt.colorbar()
plt.show()
改变插值方法:
plt.imshow(hist, interpolation='nearest')
plt.grid(True)
plt.colorbar()
plt.show()
- 使用 matplotlib 的 hist2d 函数
plt.hist2d(x, y, bins=10)
plt.colorbar()
plt.grid()
plt.show()
改变bin大小:
plt.hist2d(x, y, bins=40)
plt.colorbar()
plt.grid()
plt.show()
- 使用 matplotlib 的 pcolor 函数
plt.pcolor(hist)
plt.colorbar()
plt.grid()
plt.show()
- 使用 matplotlib 的 matshow 函数
import numpy as np
import matplotlib.pyplot as plt
columns = ['A', 'B', 'C', 'D']
rows = ['1', '2', '3', '4']
data = np.random.random((4,4))
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(data, interpolation='nearest')
fig.colorbar(cax)
ax.set_xticklabels([''] + columns)
ax.set_yticklabels([''] + rows)
plt.show()
- 使用不同的颜色
可用颜色在http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps这里
from math import ceil
import numpy as np
# 高斯分布
mean = [0,0]
cov = [[0,1],[1,0]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T
size = len(plt.cm.datad.keys())
all_maps = list(plt.cm.datad.keys())
fig, ax = plt.subplots(ceil(size/4), 4, figsize=(12,100))
counter = 0
for row in ax:
for col in row:
try:
col.imshow(hist, cmap=all_maps[counter])
col.set_title(all_maps[counter])
except IndexError:
break
counter += 1
plt.tight_layout()
plt.show()
来源:http://blog.topspeedsnail.com/archives/707#more-707