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

  • SEARCH
  • Home
  • 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 6728803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:12:49+00:00 2026-05-26T10:12:49+00:00

I have a numpy array that contains some image data. I would like to

  • 0

I have a numpy array that contains some image data. I would like to plot the ‘profile’ of a transect drawn across the image. The simplest case is a profile running parallel to the edge of the image, so if the image array is imdat, then the profile at a selected point (r,c) is simply imdat[r] (horizontal) or imdat[:,c] (vertical).

Now, I want to take as input two points (r1,c1) and (r2,c2), both lying inside imdat. I would like to plot the profile of the values along the line connecting these two points.

What is the best way to get values from a numpy array, along such a line? More generally, along a path/polygon?

I have used slicing and indexing before, but I can’t seem to arrive at an elegant solution for such a where consecutive slice elements are not in the same row or column. Thanks for your help.

  • 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-05-26T10:12:49+00:00Added an answer on May 26, 2026 at 10:12 am

    @Sven’s answer is the easy way, but it’s rather inefficient for large arrays. If you’re dealing with a relatively small array, you won’t notice the difference, if you’re wanting a profile from a large (e.g. >50 MB) you may want to try a couple of other approaches. You’ll need to work in “pixel” coordinates for these, though, so there’s an extra layer of complexity.

    There are two more memory-efficient ways. 1) use scipy.ndimage.map_coordinates if you need bilinear or cubic interpolation. 2) if you just want nearest neighbor sampling, then just use indexing directly.

    As an example of the first:

    import numpy as np
    import scipy.ndimage
    import matplotlib.pyplot as plt
    
    #-- Generate some data...
    x, y = np.mgrid[-5:5:0.1, -5:5:0.1]
    z = np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)
    
    #-- Extract the line...
    # Make a line with "num" points...
    x0, y0 = 5, 4.5 # These are in _pixel_ coordinates!!
    x1, y1 = 60, 75
    num = 1000
    x, y = np.linspace(x0, x1, num), np.linspace(y0, y1, num)
    
    # Extract the values along the line, using cubic interpolation
    zi = scipy.ndimage.map_coordinates(z, np.vstack((x,y)))
    
    #-- Plot...
    fig, axes = plt.subplots(nrows=2)
    axes[0].imshow(z)
    axes[0].plot([x0, x1], [y0, y1], 'ro-')
    axes[0].axis('image')
    
    axes[1].plot(zi)
    
    plt.show()
    

    enter image description here

    The equivalent using nearest-neighbor interpolation would look something like this:

    import numpy as np
    import matplotlib.pyplot as plt
    
    #-- Generate some data...
    x, y = np.mgrid[-5:5:0.1, -5:5:0.1]
    z = np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)
    
    #-- Extract the line...
    # Make a line with "num" points...
    x0, y0 = 5, 4.5 # These are in _pixel_ coordinates!!
    x1, y1 = 60, 75
    num = 1000
    x, y = np.linspace(x0, x1, num), np.linspace(y0, y1, num)
    
    # Extract the values along the line
    zi = z[x.astype(np.int), y.astype(np.int)]
    
    #-- Plot...
    fig, axes = plt.subplots(nrows=2)
    axes[0].imshow(z)
    axes[0].plot([x0, x1], [y0, y1], 'ro-')
    axes[0].axis('image')
    
    axes[1].plot(zi)
    
    plt.show()
    

    enter image description here

    However, if you’re using nearest-neighbor, you probably would only want samples at each pixel, so you’d probably do something more like this, instead…

    import numpy as np
    import matplotlib.pyplot as plt
    
    #-- Generate some data...
    x, y = np.mgrid[-5:5:0.1, -5:5:0.1]
    z = np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)
    
    #-- Extract the line...
    # Make a line with "num" points...
    x0, y0 = 5, 4.5 # These are in _pixel_ coordinates!!
    x1, y1 = 60, 75
    length = int(np.hypot(x1-x0, y1-y0))
    x, y = np.linspace(x0, x1, length), np.linspace(y0, y1, length)
    
    # Extract the values along the line
    zi = z[x.astype(np.int), y.astype(np.int)]
    
    #-- Plot...
    fig, axes = plt.subplots(nrows=2)
    axes[0].imshow(z)
    axes[0].plot([x0, x1], [y0, y1], 'ro-')
    axes[0].axis('image')
    
    axes[1].plot(zi)
    
    plt.show()
    

    enter image description here

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

Sidebar

Related Questions

I have a Numpy array that looks like >>> a array([[ 3. , 2.
I have a numpy array of pixel data that I want to draw at
I have a 2D numpy array that I want to plot in a colorbar.
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a matrix in the type of a Numpy array. How would I
I have a problem with some numpy stuff. I need a numpy array to
I'v got two numpy arrays. The first array contains some zeros (which are distributed
I have a 2D numpy array. Some of the values in this array are
I have a numpy array and want to convert it into an ITK image
In my code I have for loop that indexes over a multidimensional numpy array

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.