答案 0 :(得分:1)
答案可能取决于您要显示的数据类型。有几种方法可以产生这样的情节,
Rectangle
imshow
pcolormesh
假设您想要绘制直方图并选择第三个选项,可能的解决方案可能看起来像这样(基于histogram2d
)
import matplotlib.pyplot as plt
import numpy as np
xedges = [0, 1, 1.5, 3, 5]
yedges = [0, 2, 3, 4, 6]
# produce histogram
x = np.random.normal(2.5, 1, 100)
y = np.random.normal(1.5, 1, 100)
H, xedges, yedges = np.histogram2d(y, x, bins=(xedges, yedges))
fig=plt.figure()
ax = fig.add_subplot(111)
ax.set_title('Something')
X, Y = np.meshgrid(xedges, yedges)
im = ax.pcolormesh(X, Y, H)
# label the histogram bins
for i in range(len(yedges)-1):
for j in range(len(xedges)-1):
ax.text( (xedges[j+1]-xedges[j])/2.+xedges[j] ,
(yedges[i+1]-yedges[i])/2.+yedges[i] ,
str(H[i, j]) , ha="center", va="center", color="w", fontweight="bold")
plt.colorbar(im)
plt.show()