Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:18:31+00:00 2026-06-17T21:18:31+00:00

I have a 2D numpy array made from zeros and ones that I use

  • 0

I have a 2D numpy array made from zeros and ones that I use as a mask for other arrays. 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. 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. (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.

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],...]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:18:33+00:00Added an answer on June 17, 2026 at 9:18 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a numpy array out of data from an image. I want
I am creating an object in python. I have a numpy array from an
I have a NumPy array that looks like this: arr = np.array([100.10, 200.42, 4.14,
I have a Numpy array that looks like >>> a array([[ 3. , 2.
I'm trying to get a better grip on numpy arrays, so I have a
I made a function similar to numpy's array . It converts lists to arrays,
I have a numpy array of shape (height, width, 3) loaded from an image.
I have a numpy array that I would like to share between a bunch
I am relatively new to python. I have a numpy array that has 3
I have a numpy array called landuse that's a series of numbers 1-3 representing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.