matplotlib.pyplot.imshow,plt.legend() 问题

matplotlib.pyplot.imshow, issue with plt.legend()

我使用 matplotlib.pyplot(别名 plt)库的函数 imshow 显示了一些图片:

# mat is the matrix defining the image
plt.imshow(mat.reshape((16,16)),interpolation="nearest",cmap=cm.binary)
plt.legend("bla bla")

当我执行这个时,我得到了一个右上角有一个小方块的图像,我看不到我在 plt.legend 中输入的字符串。我尝试在 imshow 中添加 label="bla bla",然后使用 plt.legend(),其中没有任何参数。在这种情况下,即使是正常显示标签的小方块也消失了。

imshow 没有通常用图例解释的部分。如果颜色栏或标题不起作用,您可以添加注释(从 matplotlib 画廊更改):

import matplotlib.pyplot as plt
import numpy.random as nr

M = nr.random((20,20))
ax = plt.imshow(M).get_axes()

ax.annotate('anomaly', xy=(4., 1),  xycoords='data',
                xytext=(50, -120), textcoords='offset points',
                bbox=dict(boxstyle="round", fc="0.8"),
                arrowprops=dict(arrowstyle="->",
                                shrinkA=0, shrinkB=10,
                                connectionstyle="angle,angleA=0,angleB=90,rad=10"),
                )

plt.show()