import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
x, y = np.random.rand(2, 100) * 4
sns.histplot(x=x, y=y, bins=4, binrange=[[0, 4], [0, 4]])
使用纯 matplotlib,可以使用plt.imshow():
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
x, y = np.random.rand(2, 100) * 4
hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]])
plt.imshow(hist.T, origin='lower', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap='Blues')
plt.xticks(xedges)
plt.yticks(yedges)
plt.grid()
plt.show()
最简单的解决方案是通过seaborn(专门用于统计可视化的matplotlib扩展):
使用纯 matplotlib,可以使用
plt.imshow()
: