Skip to content

[Bug]: matplotlib.pyplot.scatter does not respect mask rules with datetime #24545

Closed

Description

@Benblob688

Bug summary

Plotting a numpy masked array against a list of datetime objects, plt.plot functions as expected, but plt.scatter still displays the masked data. Auto-axis limits do respect the masked data though.

Code for reproduction

import matplotlib.pyplot as plt
import numpy.ma as ma
from datetime import datetime

# Create a list of datetime objects
time_unmasked = [datetime(2022, 11, 24, 4, 49, 52),
                 datetime(2022, 11, 24, 4, 49, 53),
                 datetime(2022, 11, 24, 4, 49, 54),
                 datetime(2022, 11, 24, 4, 49, 55),
                 datetime(2022, 11, 24, 4, 49, 56),
                 datetime(2022, 11, 24, 4, 49, 57),
                 datetime(2022, 11, 24, 4, 49, 58)]

# Create a masked array with three masked variables, one datapoint far from others
data_masked = ma.array([15,14,50,15,15,15,16], mask=[0,0,1,1,1,0,0])

# Line plot (expected behaviour)
plt.plot(time_unmasked, data_masked)
plt.show()

# Scatter plot (plots masked points, but ignores masked values when auto-setting axis limits)
plt.scatter(time_unmasked, data_masked)
plt.show()

Actual outcome

plot behaves as expected.

enter image description here

scatter unexpectedly draws the masked datapoints, but axes limits are still respecting the masked array as the masked y=50 value is not visible.
enter image description here

Expected outcome

Scatter should not plot the masked data even though all timestamps are unmasked.
enter image description here

Additional information

It seems as though there is a check within matplotlib.pyplot.scatter that is not catching. I'd expect that logic to check that if either x or y were masked, to not plot that point. The datetime object is able to slip through. I've also tried putting time_unmasked within a numpy masked array with the same mask as masked_data (so both x and y have the same mask), but the same unexpected behaviour occurs.

Operating system

MacOS 11.3.1 (20E241)

Matplotlib Version

3.5.3

Matplotlib Backend

module://ipykernel.pylab.backend_inline

Python version

3.8.11

Jupyter version

5.7.10

Installation

conda

Activity

  1. changed the title [-][Bug]: matplotlib.pyplot.scatter does not respect mask rules with datetime[/-] [+][Bug]: `matplotlib.pyplot.scatter` does not respect mask rules with datetime[/+] on Nov 25, 2022
  2. changed the title [-][Bug]: `matplotlib.pyplot.scatter` does not respect mask rules with datetime[/-] [+][Bug]: `matplotlib.pyplot.scatter` does not respect mask rules with `datetime`[/+] on Nov 25, 2022
  3. rcomer commented on Nov 25, 2022

    @rcomer
    Member

    Thanks @Benblob688 for the report, and for providing such a clear minimal example. I've reproduced this bug on main (development version) and back to version 3.3.

  4. rcomer commented on Nov 25, 2022

    @rcomer
    Member

    A workaround for the meantime may be to use something like

    plt.plot(time_unmasked, data_masked, 'o')
  5. added this to the v3.6.3 milestone on Dec 15, 2022
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