当然,只需执行以下操作:
import matplotlib.pyplot as plt
import numpy as np
data = 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 plt
import numpy as np
data = 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 http://matplotlib.org/users/annotations_guide.html