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

时间:2015-04-13 09:55:26

标签: python matplotlib

我有一些图片,我使用matplotlib.pyplot(alias 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()。在这种情况下,即使是通常显示标签的小方块也会消失。

1 个答案:

答案 0 :(得分:0)

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()

enter image description here