简体   繁体   English

如何在matplotlib.pyplot.imshow中标记单元格(绘制单元格边框)

[英]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.

相关问题 如何在matplotlib.pyplot.imshow中使用“范围” - how to use 'extent' in matplotlib.pyplot.imshow matplotlib.pyplot.imshow 中的神器 - Artifact in matplotlib.pyplot.imshow 使用matplotlib.pyplot.imshow()时如何确定颜色? - How to determine the colours when using matplotlib.pyplot.imshow()? python matplotlib.pyplot.imshow tkinter错误 - python matplotlib.pyplot.imshow tkinter error 为什么 matplotlib.pyplot.imsave() 和 matplotlib.pyplot.imshow() 图像质量不同? 如何保证同样的质量? - Why matplotlib.pyplot.imsave() and matplotlib.pyplot.imshow() image qualities are different? How to ensure the same quality? 使用Jupyter重新绘制matplotlib.pyplot.imshow - Redraw matplotlib.pyplot.imshow in place using Jupyter Python - matplotlib.pyplot.imshow 在 if//while 中不起作用 - Python - matplotlib.pyplot.imshow won't work in a if//while 在保留宽高比的同时更改matplotlib.pyplot.imshow中的轴 - Change axes in matplotlib.pyplot.imshow while retaining aspect ratio 当没有足够的分辨率显示所有数据时,matplotlib.pyplot.imshow如何“压缩”数据? - How does matplotlib.pyplot.imshow 'squeeze' the data when there is not enough resolution to show all data? 在子图环境中使用时,matplotlib.pyplot.imshow的边距 - Margins of matplotlib.pyplot.imshow when used in subplot environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM