当然,只需执行以下操作:
import matplotlib.pyplot as pltimport numpy as npdata = np.random.random((4, 4))fig, ax = plt.subplots()# Using matshow here just because it sets the ticks up nicely. imshow is faster.ax.matshow(data, cmap='seismic')for (i, j), z in np.ndenumerate(data): ax.text(j, i, '{:0.1f}'.format(z), ha='center', va='center')plt.show()但是,标签很难看到,因此您可能需要在它们周围放置一个框:
import matplotlib.pyplot as pltimport numpy as npdata = np.random.random((4, 4))fig, ax = plt.subplots()# Using matshow here just because it sets the ticks up nicely. imshow is faster.ax.matshow(data, cmap='seismic')for (i, j), z in np.ndenumerate(data): ax.text(j, i, '{:0.1f}'.format(z), ha='center', va='center', bbox=dict(boxstyle='round', facecolor='white', edgecolor='0.3'))plt.show()另外,在许多情况下,这样
ax.annotate做更为有用
ax.text。它在放置文本方面更加灵活,但也更加复杂。在这里看看示例:http
:
//matplotlib.org/users/annotations_guide.html



