Skip to content

[Bug]: scatter plot color settings discarded unless c given #23487

Closed
@adamjstewart

Description

@adamjstewart

Bug summary

When making an animation of a scatter plot, if you don't set c (the color value parameter) when initializing the artist, the color settings are ignored.

Code for reproduction

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
pts = ax.scatter([], [], cmap="gray")
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)

def update(i):
    pts.set_offsets(np.random.rand(100, 2))
    pts.set_array(np.random.rand(100))

ani = animation.FuncAnimation(fig, func=update, frames=range(10))
plt.show()

Actual outcome

Even though cmap="gray" is passed to scatter, the points use the default "viridis" color map.

Expected outcome

I would expect the points to use the "gray" color map.

Additional information

If you modify the above code to use:

pts = ax.scatter([], [], c=[], cmap="gray")

it works as expected. It seems like all color-related settings, including cmap, vmin, vmax, etc. are discarded unless c is given during the first call to scatter.

This workaround (passing an empty c) isn't problematic, but I found the default behavior quite unintuitive and it took me the better part of a day to figure out what was happening, so I figured it would be worth reporting.

Possible solutions:

  • Raise an error/warning if cmap/vmin/vmax are given but c is not
  • Store parameters like cmap/vmin/vmax even if they aren't immediately used

These changes should probably happen in _parse_scatter_color_args in lib/matplotlib/axes/_axes.py. According to git blame, @timhoffm @anntzer wrote most of this logic.

Operating system

macOS 12.4

Matplotlib Version

3.5.2

Matplotlib Backend

MacOSX

Python version

3.9.13

Jupyter version

N/A

Installation

from source (.tar.gz)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions