-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Open
Labels
Description
Bug summary
The where argument in ax.fill_between does not allow boolean np.ndarrays
matplotlib/lib/matplotlib/axes/_axes.pyi
Lines 455 to 466 in 8d64f03
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