Skip to content

[Bug]: Type hints for where argument in ax.fill_between does not allow np.ndarrays #29454

@Illviljan

Description

@Illviljan

Bug summary

The where argument in ax.fill_between does not allow boolean np.ndarrays

def fill_between(
self,
x: ArrayLike,
y1: ArrayLike | float,
y2: ArrayLike | float = ...,
where: Sequence[bool] | None = ...,
interpolate: bool = ...,
step: Literal["pre", "post", "mid"] | None = ...,
*,
data=...,
**kwargs
) -> FillBetweenPolyCollection: ...

Code for reproduction

from typing import Sequence

import numpy as np
from numpy.typing import ArrayLike, NDArray
import matplotlib
import matplotlib.pyplot as plt


x = np.array([0, 1, 2, 3, 4])
y = np.array([0.5, 0.75, 0.99, 0.98, 0.8])

fig, ax = plt.subplots(1, 1, layout="constrained")
# ax.plot(x, y)
ax.fill_between(x, 0, 1, where=y > 0.95, alpha=0.2, transform=ax.get_xaxis_transform())
# error: Argument "where" to "fill_between" of "Axes" has incompatible type "ndarray[Any, dtype[numpy.bool]]"; expected "Sequence[builtins.bool] | None"  [arg-type]

# Options that works:
where_opt1: Sequence[bool] | NDArray[np.bool] | None = y > 0.95  # Too narrow, what about cupy?
where_opt2: Sequence[bool] | ArrayLike | None = y > 0.95  # Too wide, allows all dtypes.

Actual outcome

# mypy:
# error: Argument "where" to "fill_between" of "Axes" has incompatible type "ndarray[Any, dtype[numpy.bool]]"; expected "Sequence[builtins.bool] | None"  [arg-type]

# pyright:
# error: Argument of type "NDArray[bool]" cannot be assigned to parameter "where" of type "Sequence[bool] | None" in function "fill_between"
#   Type "NDArray[bool]" is not assignable to type "Sequence[bool] | None"
#     "ndarray[Any, dtype[bool]]" is not assignable to "Sequence[bool]"
#     "ndarray[Any, dtype[bool]]" is not assignable to "None" (reportArgumentType)

Expected outcome

No mypy/pyright errors

Additional information

Same problem with fill_betweenx.

I suppose adding ArrayLike is the easiest solution but not narrow enough to only catch boolean arrays.

Operating system

No response

Matplotlib Version

3.9.2

  • numpy: 2.1.1
  • mypy: 1.11.2
  • pyright: 1.1.379
  • matplotlib 3.9.2

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy