Skip to content

[Bug]: Nan values in scatter 3d plot show in black colour when alpha parameter is passed. #25446

Closed

Description

@ricmperes

Bug summary

In a projection='3d' axis, a scatter plot with the c argument to colour the points is showing black markers in the position of nan values if the alpha argument is passed. If the alpha argument is not present, the nan values are not displayed, as expected. If the alpha argument is present, even with alpha = 1, the markers are coloured black.

Code for reproduction

n_x = 200
n_y = 1
n_z = 200

_x = np.linspace(-10,10,n_x)
_y = 0 
_z = np.linspace(-10,10,n_z)

_xx, _yy, _zz = np.meshgrid(_x, _y, _z, indexing='ij')
c = np.array(np.arange(n_x*n_y*n_z, dtype = float).reshape(n_x,n_y,n_z))
c[int(n_x/4):int(n_x/2),:,:] = np.nan

fig = plt.figure(figsize=(12, 5))
ax0 = fig.add_subplot(121, projection='3d')
ax0.scatter(_xx, _yy, _zz, c = c, marker = 's')
ax0.set_title('`alpha` OFF')
ax1 = fig.add_subplot(122, projection='3d')
ax1.set_title('`alpha` ON')
scat3d = ax1.scatter(_xx, _yy, _zz, c = c, 
                     marker = 's', alpha = 1)

plt.show()

Actual outcome

download

Expected outcome

download

Additional information

No response

Operating system

Ubuntu

Matplotlib Version

3.7.0

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.10.9

Jupyter version

6.5.2

Installation

conda

Activity

  1. ricmperes commented on Mar 13, 2023

    @ricmperes
    ContributorAuthor

    In a 2d projection the issue does not happen and both alpha and nan values work as expected.

    download

  2. tacaswell commented on Mar 14, 2023

    @tacaswell
    Member

    The default color for "bad" in the color maps is (0, 0, 0, 0) which if alpha gets set to 1 is black.

    My guess (without digging into the code) is that we are setting the alpha rather than multiplyin the alpha when it is set by the user....

  3. evgepab commented on Mar 14, 2023

    @evgepab

    Hello! Me and @eirinikafourou are students at the Department of Management Science and Technology in Athens University of Economics and Business and as a part of our class (Applied Software Engineering) we should contribute to an OS project. Could we handle this issue?

  4. tacaswell commented on Mar 15, 2023

    @tacaswell
    Member

    We do not really assign issues, so please feel free to work on this (or any other issue).

  5. oscargus commented on Mar 16, 2023

    @oscargus
    Member

    Seems like @tacaswell guess is correct:

    def _maybe_depth_shade_and_sort_colors(self, color_array):
    color_array = (
    _zalpha(color_array, self._vzs)
    if self._vzs is not None and self._depthshade
    else color_array
    )
    if len(color_array) > 1:
    color_array = color_array[self._z_markers_idx]
    return mcolors.to_rgba_array(color_array, self._alpha)

    The original alpha values are dropped at the first line and then a single alpha value is set at the last line.

    The solution would probably be to store the original alpha values and then concatenate them back (multiplied with self._alpha) at the last line.

  6. added this to the v3.11.0 milestone on May 7, 2026
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions