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 8527499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:34:27+00:00 2026-06-11T08:34:27+00:00

Ok, so I feel like there should be an easy way to create a

  • 0

Ok, so I feel like there should be an easy way to create a 3-dimensional scatter plot using matplotlib. I have a 3D numpy array (dset) with 0’s where I don’t want a point and 1’s where I do, basically to plot it now I have to step through three for: loops as such:

for i in range(30):
    for x in range(60):
        for y in range(60):
            if dset[i, x, y] == 1:
                ax.scatter(x, y, -i, zdir='z', c= 'red')

Any suggestions on how I could accomplish this more efficiently? Any ideas would be greatly appreciated.

  • 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-11T08:34:28+00:00Added an answer on June 11, 2026 at 8:34 am

    If you have a dset like that, and you want to just get the 1 values, you could use nonzero, which “returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension.”.

    For example, we can make a simple 3d array:

    >>> import numpy
    >>> numpy.random.seed(29)
    >>> d = numpy.random.randint(0, 2, size=(3,3,3))
    >>> d
    array([[[1, 1, 0],
            [1, 0, 0],
            [0, 1, 1]],
    
           [[0, 1, 1],
            [1, 0, 0],
            [0, 1, 1]],
    
           [[1, 1, 0],
            [0, 1, 0],
            [0, 0, 1]]])
    

    and find where the nonzero elements are located:

    >>> d.nonzero()
    (array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]), array([0, 0, 1, 2, 2, 0, 0, 1, 2, 2, 0, 0, 1, 2]), array([0, 1, 0, 1, 2, 1, 2, 0, 1, 2, 0, 1, 1, 2]))
    >>> z,x,y = d.nonzero()
    

    If we wanted a more complicated cut, we could have done something like (d > 3.4).nonzero() or something, as True has an integer value of 1 and counts as nonzero.

    Finally, we plot:

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x, y, -z, zdir='z', c= 'red')
    plt.savefig("demo.png")
    

    giving

    demo 3d image

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

Sidebar

Related Questions

I feel like there should be a simple way to do this but I
I feel like there's a pretty simple way to do this, but I'm not
I've tried [myButton setKeyEquivalent:@\n] but that didn't work. I feel like there should be
Is there an easy way to marshal a PHP associative array to and from
I feel like there's a simple solution to this, but I'm not really sure
I just had the jQuery epiphany the other day and still feel like there
I feel like I'm re-inventing the wheel, and that there is a good chance
I feel like I'm taking crazy pills here. Usually there's always a million library
I feel there is probably a well trodden design pattern to handle situations like
OK, this is working but I feel there is a better way to do

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.