简体   繁体   English

来自numpy数组的Python等高线图

[英]Python contour plot from numpy array

I have a 2D numpy array made from zeros and ones that I use as a mask for other arrays. 我有一个由零和一个用作其他数组的掩码的2D numpy数组。 I was trying to use matplotlib.contour to highlight an area on a plot, but every time I try it I get a zero-size array to minimum.reduce without identity error. 我试图使用matplotlib.contour突出显示绘图上的区域,但是每次尝试时,都会得到一个zero-size array to minimum.reduce without identity Any idea? 任何想法?

Since this mask is a set of rectangles, I tried to find the edges manually, but it does not work properly. 由于此蒙版是一组矩形,因此我尝试手动查找边缘,但无法正常工作。 Here's the code I use: 这是我使用的代码:

tmp1,tmp2 = [],[]
for ii in range(len(mask))[1:-2]:
    if mask[ii+1] - mask[ii] != 1: tmp1.append(mask[ii])
if mask[ii] - mask[ii-1] != 1: tmp2.append(mask[ii]-1)


rect_limits = []
for ii in range(len(tmp1)):
    rect_limits.append([- delta_cont, tmp1[ii], delta_cont, tmp2[ii]])

that way tmp1 and tmp2 should give me the max and min of the rectangles I am searching for. 这样tmp1tmp2应该给我我要搜索的矩形的最大值和最小值。 (the lateral edges of the rectangle are fixed, so no problem there). (矩形的侧边是固定的,因此没有问题)。

then I just need to use add_patch to create the contour of the rectangles I want. 那么我只需要使用add_patch即可创建所需矩形的轮廓。

Any alternative idea to find the rectangle edges? 寻找矩形边缘还有其他选择吗?

Edit: 编辑:

OK, so my mask would be something like: 好,所以我的面具会像这样:

[[0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 1 1 1 1 0 0],      
[0 0 0 0 0 0 1 1 1 1 0 0],
[0 0 0 0 0 0 1 1 1 1 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 1 1 1 1 0 0],
[0 0 0 0 0 0 1 1 1 1 0 0],
[0 0 0 0 0 0 1 1 1 1 0 0],
[0 0 0 0 0 0 1 1 1 1 0 0]]

and ideally what I would like as a result would be: 理想的结果是:

[[1,3],[6,9]]

ie, an array built with 即,用

[[y_start1,y_end1],[y_start2,y_end2],...]

answer.py answer.py

mygrid = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
          [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
          [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
          [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
          [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
          [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0]]

def findEdges(grid)
    y_start = -1
    saved = []
    for lineno, row in enumerate(grid):
        # Case where we don't have a start point
        if y_start == -1 and 1 in row:
            y_start = lineno
        # Case where we have a start point and we just hit a zero row
        if y_start != -1 and 1 not in row:           
            saved.append((y_start, lineno-1))
            y_start = -1
        # Case where we have a start point and hit the end of the table
        if lineno == len(grid)-1 and y_start > 0:
            saved.append((y_start, lineno))

    return saved

print(findEdges(mygrid))

This gives an output of: 输出为:

mike@example ~ $ python answer.py
[(1, 3), (6, 9)]

Note: This won't work if two or more rectangles are allowed to be side by side on the grid. 注意:如果允许两个或多个矩形在网格上并排,则此方法将无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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