简体   繁体   中英

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

I have some pictures that I display using the function imshow of the matplotlib.pyplot (alias plt) library :

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

When I execute this, I get an image with a little empty square at the top right and I can't see the string I put in plt.legend. I tried to add label="bla bla" in imshow and then use plt.legend() with no arguments in it. In this case, even the little square which should normally display the label disappear.

imshow doesn't have parts that one would normally explain with a legend. If a colorbar or title don't work, you can add an annotation (as altered from matplotlib gallery):

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM