[Bug]: `ValueError: The truth value...` when an ndarray is passed to the `color` kwarg of `axes3d.scatter` · Issue #26821 · matplotlib/matplotlib · GitHub
Skip to content

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

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

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