Skip to content

[Bug]: ValueError: The truth value... when an ndarray is passed to the color kwarg of axes3d.scatter #26821

Closed

Description

@joshuacwnewton

Bug summary

Passing a numpy array to the color argument of ax.scatter results in a ValueError when ax is initialized with projection='3d'. This error began occurring in our CI after the release of matplotlib 3.8.0.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(1, 0, 0, color=np.array([0, 0, 0, 1]))
plt.show()

Actual outcome

Traceback from our CI suite:

  File "/home/runner/sct_6.1.dev0/testing/cli/test_cli_sct_dmri_display_bvecs.py", line 17, in test_sct_dmri_display_bvecs_png_exists
    sct_dmri_display_bvecs.main(argv=['-bvec', sct_test_path('dmri', 'bvecs.txt')])
  File "/home/runner/sct_6.1.dev0/spinalcordtoolbox/scripts/sct_dmri_display_bvecs.py", line 176, in main
    ax.scatter(x[i], y[i], z[i], color=shell_colors[bvals[i]], alpha=0.7)
  File "/home/runner/sct_6.1.dev0/python/envs/venv_sct/lib/python3.9/site-packages/matplotlib/__init__.py", line 1465, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/home/runner/sct_6.1.dev0/python/envs/venv_sct/lib/python3.9/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 2377, in scatter
    if kwargs.get('color', None):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Expected outcome

No error.

Additional information

NB: This is legacy code I'm maintaining but haven't actually written myself -- I only began looking at it after our CI threw an error after the upgrade from matplotlib 3.7.3 to 3.8.0.

  • If I remove projection='3d', the code does not error out.
  • If I pass a list ([0, 0, 0, 1]) rather than an ndarray, the code does not error out, and displays a black, opaque point:

image

Operating system

Ubuntu 22.04

Matplotlib Version

3.8.0

Matplotlib Backend

module://backend_interagg

Python version

3.9.17, 3.10.12

Jupyter version

N/A

Installation

pip

Activity

  1. ksunden commented on Sep 18, 2023

    @ksunden
    Member

    This behavior was changed in #26249

    I think that the conditional should actually be:

    if "color" in kwargs:

    as it is using .get really only to determine if it is present or not.

  2. timhoffm commented on Sep 19, 2023

    @timhoffm
    Member

    I think the condition actually should be

    If kwargs.get("color") is not Nonr:
    

    To make explicit color=None work and comply with

    c Defaults to None. In that case the marker color is determined by the value of color, facecolor or facecolors. In case those are not specified or None, the marker color is determined by the next color of the Axes' current "shape and fill" color cycle.

    from https://matplotlib.org/devdocs/api/_as_gen/matplotlib.axes.Axes.scatter.html#matplotlib.axes.Axes.scatter

  3. added this to the v3.8.1 milestone on Sep 19, 2023
  4. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Sep 19, 2023
  5. github-actions commented on Sep 19, 2023

    @github-actions

    Good first issue - notes for new contributors

    This issue is suited to new contributors because it does not require understanding of the Matplotlib internals. To get started, please see our contributing guide.

    We do not assign issues. Check the Development section in the sidebar for linked pull requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please collaborate on the work by reviewing it rather than duplicating it in a competing PR.

    If something is unclear, please reach out on any of our communication channels.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions