Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
joshuacwnewton opened this issue Sep 18, 2023 · 3 comments · Fixed by #26834
Labels
Good first issue Open a pull request against these issues if there are no active ones! status: has patch patch suggested, PR still needed topic: mplot3d
Milestone

Comments

@joshuacwnewton
Copy link

joshuacwnewton commented Sep 18, 2023

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

@ksunden
Copy link
Member

ksunden commented Sep 18, 2023

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.

@timhoffm
Copy link
Member

timhoffm commented Sep 19, 2023

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

@oscargus oscargus added this to the v3.8.1 milestone Sep 19, 2023
@tacaswell tacaswell added Good first issue Open a pull request against these issues if there are no active ones! status: has patch patch suggested, PR still needed labels Sep 19, 2023
@github-actions
Copy link

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.

MridulS pushed a commit to networkx/networkx that referenced this issue Dec 5, 2023
…trations (#7040)

* Initial Create 3d_rotation_anime.py

Check how it appears in the docs

* remove text 3d_rotation_anime.py

[skip active]

* complete image_segmentation_spectral_graph_partiion.py.py

* plot directly image_segmentation_spectral_graph_partiion.py.py

* format + sample known plot image_segmentation_spectral_graph_partiion.py.py

* errors fixed image_segmentation_spectral_graph_partiion.py.py

* Rename image_segmentation_spectral_graph_partiion.py.py to plot_image_segmentation_spectral_graph_partiion.py.py

* sklearn>=1.2 example.txt

* sklearn>=1.3.1 example.txt

* scikit-learn>=1.3.1 example.txt

* error sc name plot_image_segmentation_spectral_graph_partiion.py.py

* error + enumerate plot_image_segmentation_spectral_graph_partiion.py.py

* last enumerate plot_image_segmentation_spectral_graph_partiion.py.py

* matplotlib 3.8.0 example.txt

* minor plot_image_segmentation_spectral_graph_partiion.py.py

* color=tuple(X[i] / 255)  plot_image_segmentation_spectral_graph_partiion.py.py

matplotlib/matplotlib#26821

* error, style, comments plot_image_segmentation_spectral_graph_partiion.py.py

* remove matplotlib example.txt

* added Plot the RGB dataset as an image.

* style/comment edits plot_image_segmentation_spectral_graph_partiion.py.py

* lint " plot_image_segmentation_spectral_graph_partiion.py.py

* thumbnail #3 plot_image_segmentation_spectral_graph_partiion.py.py

* move to Algorithms plot_image_segmentation_spectral_graph_partiion.py

* Delete examples/3d_drawing/plot_image_segmentation_spectral_graph_partiion.py.py

* data animation added plot_image_segmentation_spectral_graph_partiion.py

* anime fix + lint plot_image_segmentation_spectral_graph_partiion.py

* add graph animation plot_image_segmentation_spectral_graph_partiion.py
padath314 pushed a commit to padath314/networkx that referenced this issue Dec 5, 2023
…trations (networkx#7040)

* Initial Create 3d_rotation_anime.py

Check how it appears in the docs

* remove text 3d_rotation_anime.py

[skip active]

* complete image_segmentation_spectral_graph_partiion.py.py

* plot directly image_segmentation_spectral_graph_partiion.py.py

* format + sample known plot image_segmentation_spectral_graph_partiion.py.py

* errors fixed image_segmentation_spectral_graph_partiion.py.py

* Rename image_segmentation_spectral_graph_partiion.py.py to plot_image_segmentation_spectral_graph_partiion.py.py

* sklearn>=1.2 example.txt

* sklearn>=1.3.1 example.txt

* scikit-learn>=1.3.1 example.txt

* error sc name plot_image_segmentation_spectral_graph_partiion.py.py

* error + enumerate plot_image_segmentation_spectral_graph_partiion.py.py

* last enumerate plot_image_segmentation_spectral_graph_partiion.py.py

* matplotlib 3.8.0 example.txt

* minor plot_image_segmentation_spectral_graph_partiion.py.py

* color=tuple(X[i] / 255)  plot_image_segmentation_spectral_graph_partiion.py.py

matplotlib/matplotlib#26821

* error, style, comments plot_image_segmentation_spectral_graph_partiion.py.py

* remove matplotlib example.txt

* added Plot the RGB dataset as an image.

* style/comment edits plot_image_segmentation_spectral_graph_partiion.py.py

* lint " plot_image_segmentation_spectral_graph_partiion.py.py

* thumbnail networkx#3 plot_image_segmentation_spectral_graph_partiion.py.py

* move to Algorithms plot_image_segmentation_spectral_graph_partiion.py

* Delete examples/3d_drawing/plot_image_segmentation_spectral_graph_partiion.py.py

* data animation added plot_image_segmentation_spectral_graph_partiion.py

* anime fix + lint plot_image_segmentation_spectral_graph_partiion.py

* add graph animation plot_image_segmentation_spectral_graph_partiion.py
cvanelteren pushed a commit to cvanelteren/networkx that referenced this issue Apr 22, 2024
…trations (networkx#7040)

* Initial Create 3d_rotation_anime.py

Check how it appears in the docs

* remove text 3d_rotation_anime.py

[skip active]

* complete image_segmentation_spectral_graph_partiion.py.py

* plot directly image_segmentation_spectral_graph_partiion.py.py

* format + sample known plot image_segmentation_spectral_graph_partiion.py.py

* errors fixed image_segmentation_spectral_graph_partiion.py.py

* Rename image_segmentation_spectral_graph_partiion.py.py to plot_image_segmentation_spectral_graph_partiion.py.py

* sklearn>=1.2 example.txt

* sklearn>=1.3.1 example.txt

* scikit-learn>=1.3.1 example.txt

* error sc name plot_image_segmentation_spectral_graph_partiion.py.py

* error + enumerate plot_image_segmentation_spectral_graph_partiion.py.py

* last enumerate plot_image_segmentation_spectral_graph_partiion.py.py

* matplotlib 3.8.0 example.txt

* minor plot_image_segmentation_spectral_graph_partiion.py.py

* color=tuple(X[i] / 255)  plot_image_segmentation_spectral_graph_partiion.py.py

matplotlib/matplotlib#26821

* error, style, comments plot_image_segmentation_spectral_graph_partiion.py.py

* remove matplotlib example.txt

* added Plot the RGB dataset as an image.

* style/comment edits plot_image_segmentation_spectral_graph_partiion.py.py

* lint " plot_image_segmentation_spectral_graph_partiion.py.py

* thumbnail networkx#3 plot_image_segmentation_spectral_graph_partiion.py.py

* move to Algorithms plot_image_segmentation_spectral_graph_partiion.py

* Delete examples/3d_drawing/plot_image_segmentation_spectral_graph_partiion.py.py

* data animation added plot_image_segmentation_spectral_graph_partiion.py

* anime fix + lint plot_image_segmentation_spectral_graph_partiion.py

* add graph animation plot_image_segmentation_spectral_graph_partiion.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good first issue Open a pull request against these issues if there are no active ones! status: has patch patch suggested, PR still needed topic: mplot3d
Projects
None yet
5 participants