如何在matplotlib.pyplot.imshow中标记单元格(绘制单元格边框) [英] How to mark cells in matplotlib.pyplot.imshow (drawing cell borders)

查看:96
本文介绍了如何在matplotlib.pyplot.imshow中标记单元格(绘制单元格边框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的二维矢量要显示:

I have a small 2d vector to display:

import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(4,10)
plt.imshow(img, cmap='Reds')

如下:

但是现在我要标记一个特定的单元格,以便使读者将注意力集中在该单元格上.因为这是特别感兴趣的...

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:

有人知道如何用 matplotlib 方便地存档吗?

Does someone know how to archive this with matplotlib in a convenient way?

推荐答案

在要突出显示的像素位置放置一个矩形.

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

这篇关于如何在matplotlib.pyplot.imshow中标记单元格(绘制单元格边框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆