matplotlib / matplotlib/matplotlib

[Bug]: bad autolimit behavior with fill_between and transforms

Open
#21,976 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

keep
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### Bug summary

Using a fill_between artist with a custom transform to displace data along the y-axis, the autolimiting behavior does not work as I would expect: the axes use the non-transformed data extents. A normal line plot with the same transform does work as I would expect: autolimits match the transformed data.

### Code for reproduction

```python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.transforms import Transform

class TransformDemo(Transform):

def __init__(self):

super().__init__()

self.input_dims = 2
self.output_dims = 2
self.is_separable = False

def transform_non_affine(self, values):
output = np.empty_like(values)
output[:, 0] = values[:, 0]
output[:, 1] = 100 + values[:, 1] / (1 + np.abs(values[:, 0]))

return output

# Make some random data
y = np.random.randn(1000)
x = np.arange(len(y))

fig, ax = plt.subplots(ncols=2)

T = TransformDemo()

# First with plot, everything works as expected
ax[0].plot(x, y, transform=T + ax[0].transData)

# With fill_between, limits are not inferred correctly
ax[1].fill_between(x, -np.abs(y), np.abs(y), transform=T + ax[1].transData)
```

### Actual outcome

![image](https://user-images.githubusercontent.com/1190540/146374625-ccc09b7a-28c2-4903-a705-876243b1719c.png)

### Expected outcome

If I force the ylimits on the second plot via

```python
ax[1].set_ylim(ax[0].get_ylim())
```
it looks like
![image](https://user-images.githubusercontent.com/1190540/146374806-87c3dd31-e46a-4dd0-895d-fe508ae5d571.png)

which is basically how I would expect.

### Additional information

I'm not sure that the plot behavior is actually to spec here, but the disagreement between the two artists' behavior seems to me like a bug somewhere.

### Operating system

Ubuntu

### Matplotlib Version

3.5.0

### Matplotlib Backend

module://matplotlib_inline.backend_inline

### Python version

sys.version_info(major=3, minor=9, micro=7, releaselevel='final', serial=0)

### Jupyter version

3.2.5 (dev), 3.2.4 (app)

### Installation

conda

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Python reproduction and compare the autolimit behavior of plot with fill_between when both use the custom TransformDemo and ax.transData. Trace the fill_between artist's limit calculation and add a regression test; done means transformed fill_between data produces limits consistent with the transformed line plot.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.