[英]How to mark cells in matplotlib.pyplot.imshow (drawing cell borders)
I have a small 2d vector to display: 我要显示一个小的2D向量:
import matplotlib.pyplot as plt
import numpy as np
img = np.random.rand(4,10)
plt.imshow(img, cmap='Reds')
As folllow: 如下:
But now I want to mark a specific cell, in order to focus the reader on that cell. 但是现在我想标记一个特定的单元格,以使读者将注意力集中在该单元格上。 Because this is of specific interest... 因为这是特别感兴趣的...
Therefore something like a border of this cell would be nice: 因此,像这个单元格的边框这样的东西会很好:
Does someone know how to archive this with matplotlib
in a convenient way? 有人知道如何以方便的方式使用matplotlib
将此文件存档吗?
Put a rectangle at the position of the pixel you want to highlight. 在要突出显示的像素的位置放置一个矩形。
import matplotlib.pyplot as plt
import numpy as np
def highlight_cell(x,y, ax=None, **kwargs):
rect = plt.Rectangle((x-.5, y-.5), 1,1, fill=False, **kwargs)
ax = ax or plt.gca()
ax.add_patch(rect)
return rect
img = np.random.rand(4,10)
plt.imshow(img, cmap='Reds')
highlight_cell(2,1, color="limegreen", linewidth=3)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.