您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用matplotlib在网格中显示值

使用matplotlib在网格中显示值

当然,只需执行以下操作:

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',
            b@R_13_2419@=dict(@R_13_2419@style='round', facecolor='white', edgecolor='0.3'))

plt.show()

在此处输入图片说明

另外,在许多情况下,这样ax.annotate做更为有用ax.text。它在放置文本方面更加灵活,但也更加复杂。在这里看看示例:http ://matplotlib.org/users/annotations_guide.html

其他 2022/1/1 18:16:20 有312人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶