FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[ENH]: scatter 3D numpy array using their (x,y,z) projectories · Issue #24277 · matplotlib/matplotlib · GitHub

Repository navigation

[ENH]: scatter 3D numpy array using their (x,y,z) projectories #24277

Closed

Description

Problem

I often come across the situation where I have some 3d numpy array like: data = np.random.rand(10, 10, 10).

Is there any way I can scatter data using their (x,y,z) projectories. for example: data(1,1,4) = 514, and make a 3D plot which plots point(1,1,4) as the color data cdata corresponding to the value 514.

What is the pythonic way to go from data to x, y, z and cdata?

Proposed solution

No response

Activity

  1. ksunden commented on Oct 26, 2022

    Member
    >>> import numpy as np
    >>> from matplotlib import pyplot as plt
    >>> fig = plt.figure()
    >>> ax = fig.add_subplot(projection="3d")
    >>> x = np.arange(5)
    >>> y = np.arange(8)
    >>> z = np.arange(10)
    >>> x,y,z = np.meshgrid(x,y,z)
    >>> ax.scatter(x,y,z,c=np.random.rand(5,8,10))
    <mpl_toolkits.mplot3d.art3d.Path3DCollection object at 0x7f2e4d6c5c50>
    >>> plt.show()

    I think this does what you want:

    (I changed shapes just to make it obvious that order does matter when the shapes are not equal)

  2. Firestar-Reimu commented on Oct 27, 2022

    Author

    Thanks! This solve my problem.

    If we can plot them like boxes will be better.

    for example, in the graph above, every plot is a 1x1x1 box with the same color?

  3. ksunden commented on Oct 27, 2022

    Member

    Ah, then this sounds like voxels

    norm = mpl.colors.Normalize(np.min(data), np.max(data))
    ax.voxels(np.ones_like(data), facecolors=mpl.colormaps["viridis"](norm(data)))

    (with the same setup for axes with 3d projection)

    Now, you won't be able to see inside of the shape.

    Note that voxels are more commonly used to show/exclude boxes rather than to show data as the color, so the ones_like is just saying "show every box" and the facecolors kwarg is calling the colormap directly to get color information in a format voxels can use.
    Norm just normalizes the data to a range of 0 to 1, if you really only care about np.random.rand data, then that may be omitted, as that is already in that range.

    Be aware that most of the boxes will not be visible, only those on the surface will be.

  4. scottshambaugh commented on Nov 1, 2022

    Contributor

    Closing since a pythonic solution was provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL