Skip to content

[Bug]: ax.scatter (projection='3d') - incorrect handling of NaN  #26236

Closed

Description

@2sn

Bug summary

In axis 3D projection NaN values are not handled correctly, apparently the values are masked out (as it should be) but the mask is not applied to a color array that may not have NaN in the same position.

Code for reproduction

import numpy as np
from matplotlib import pylab as plt
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter([1,np.nan,3], [2,np.nan,4], [3, np.nan,5], color=[[.5,.5,.5,.5]]*3, s=11.5)

Actual outcome

ValueError                                Traceback (most recent call last)
Cell In[24], line 1
----> 1 ax.scatter([1,np.nan,3], [2,np.nan,4], [3, np.nan,5], color=[[.5,.5,.5,.5]]*3, s=11.5)

File ~/Python/lib/python3.11/site-packages/matplotlib/__init__.py:1442, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1439 @functools.wraps(func)
   1440 def inner(ax, *args, data=None, **kwargs):
   1441     if data is None:
-> 1442         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1444     bound = new_sig.bind(ax, *args, **kwargs)
   1445     auto_label = (bound.arguments.get(label_namer)
   1446                   or bound.kwargs.get(label_namer))

File ~/Python/lib/python3.11/site-packages/mpl_toolkits/mplot3d/axes3d.py:2275, in Axes3D.scatter(self, xs, ys, zs, zdir, s, c, depthshade, *args, **kwargs)
   2272 if np.may_share_memory(zs_orig, zs):  # Avoid unnecessary copies.
   2273     zs = zs.copy()
-> 2275 patches = super().scatter(xs, ys, s=s, c=c, *args, **kwargs)
   2276 art3d.patch_collection_2d_to_3d(patches, zs=zs, zdir=zdir,
   2277                                 depthshade=depthshade)
   2279 if self._zmargin < 0.05 and xs.size > 0:

File ~/Python/lib/python3.11/site-packages/matplotlib/__init__.py:1442, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1439 @functools.wraps(func)
   1440 def inner(ax, *args, data=None, **kwargs):
   1441     if data is None:
-> 1442         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1444     bound = new_sig.bind(ax, *args, **kwargs)
   1445     auto_label = (bound.arguments.get(label_namer)
   1446                   or bound.kwargs.get(label_namer))

File ~/Python/lib/python3.11/site-packages/matplotlib/axes/_axes.py:4602, in Axes.scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, **kwargs)
   4599 if edgecolors is None:
   4600     orig_edgecolor = kwargs.get('edgecolor', None)
   4601 c, colors, edgecolors = \
-> 4602     self._parse_scatter_color_args(
   4603         c, edgecolors, kwargs, x.size,
   4604         get_next_color_func=self._get_patches_for_fill.get_next_color)
   4606 if plotnonfinite and colors is None:
   4607     c = np.ma.masked_invalid(c)

File ~/Python/lib/python3.11/site-packages/matplotlib/axes/_axes.py:4455, in Axes._parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
   4451     else:
   4452         if len(colors) not in (0, 1, xsize):
   4453             # NB: remember that a single color is also acceptable.
   4454             # Besides *colors* will be an empty array if c == 'none'.
-> 4455             raise invalid_shape_exception(len(colors), xsize)
   4456 else:
   4457     colors = None  # use cmap, norm after collection is created

ValueError: 'c' argument has 3 elements, which is inconsistent with 'x' and 'y' with size 2.

Expected outcome

A plot with the first and 3rd data point.

Additional information

Unconditionally reproducible.

I have not seen this before, but I may never have called it this way before.

Operating system

Fedora 38

Matplotlib Version

3.7.1

Matplotlib Backend

TkAgg

Python version

3.11.4

Jupyter version

IPython 8.14.0

Installation

pip

Activity

  1. changed the title [-]ax.scatter (projection='3d') - incorrect handling of NaN [/-] [+][BUG]: ax.scatter (projection='3d') - incorrect handling of NaN [/+] on Jul 3, 2023
  2. changed the title [-][BUG]: ax.scatter (projection='3d') - incorrect handling of NaN [/-] [+][Bug]: ax.scatter (projection='3d') - incorrect handling of NaN [/+] on Jul 3, 2023
  3. rcomer commented on Jul 3, 2023

    @rcomer
    Member

    Thank you for your clear report and diagnosis @2sn. I have reproduced this with our main development branch.

  4. artemshekh commented on Jul 3, 2023

    @artemshekh
    Contributor

    Change this:

    xs, ys, zs, s, c = cbook.delete_masked_points(xs, ys, zs, s, c)

    to

    if kwargs.get('color', None):
        xs, ys, zs, s, c, kwargs['color'] = cbook.delete_masked_points(xs, ys, zs, s, c, kwargs['color'])
    else:
        xs, ys, zs, s, c = cbook.delete_masked_points(xs, ys, zs, s, c)
    

    on first sight solve the problem.
    I am willing to take this issue if no one alredy did it.

  5. story645 commented on Jul 3, 2023

    @story645
    Member

    @artemshekh go for it

  6. added this to the v3.8.0 milestone on Jul 18, 2023
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