Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Autoscaling incorrect when scale set before adding collection #23129

Open
mwaskom opened this issue May 25, 2022 · 4 comments · May be fixed by #28021
Open

[Bug]: Autoscaling incorrect when scale set before adding collection #23129

mwaskom opened this issue May 25, 2022 · 4 comments · May be fixed by #28021

Comments

@mwaskom
Copy link

mwaskom commented May 25, 2022

Bug summary

When using ax.add_collection and ax.set_{axis}scale, autoscaling works properly only when they are called in that order, but not in the reverse order.

Code for reproduction

import numpy as np, matplotlib as mpl, matplotlib.pyplot as plt
ax = plt.figure().subplots()
xy = np.c_[np.arange(50), np.linspace(1, 100, 50)]
lines = mpl.collections.LineCollection(segments=[xy])
ax.set_yscale("log")
ax.add_collection(lines)
ax.autoscale_view()

Actual outcome

image

Expected outcome

Swap the order of those calls:

import numpy as np, matplotlib as mpl, matplotlib.pyplot as plt
ax = plt.figure().subplots()
xy = np.c_[np.arange(50), np.linspace(1, 100, 50)]
lines = mpl.collections.LineCollection(segments=[xy])
ax.add_collection(lines)
ax.set_yscale("log")
ax.autoscale_view()

image

Additional information

This may be the same, or a related, issue as discussed here: #11898

@jklymak's suggested workaround does appear to work in my actual usecase, albeit with minimal testing. I don't feel like I understand this corner of matplotlib quite well enough to say for sure.

Matplotlib Version

3.5.2

@WeatherGod
Copy link
Member

WeatherGod commented May 25, 2022 via email

@mwaskom
Copy link
Author

mwaskom commented May 25, 2022

It is still non-intuitive because other plotting methods don't have the same order-dependency. e.g. change the example to use plt.plot (or add a Line2D directly, then autoscale) and the data limits are correct either way.

@jklymak
Copy link
Member

jklymak commented May 25, 2022

Its definitely a transform problem which is hard to back out #11898 (comment)

@Gerson-M-Ferreira
Copy link

Hello everyone,

After investigating this issue further, I've noticed that the datalim values in add_collection() are affected by a non-affine transformation in get_datalim(), from class Collection (lines 266-267 in collections.py):

    if not transform.is_affine:
        paths = [transform.transform_path_non_affine(p) for p in paths]

I'm curious about the specific purpose of this transformation and why the shape of the data matters for obtaining its bounding limits. While removing this transformation resolves the problem, I'd like to understand its original intent better. Despite its removal, the overall behavior remains consistent, and all tests pass seamlessly.

Gerson-M-Ferreira added a commit to Gerson-M-Ferreira/matplotlib that referenced this issue Apr 4, 2024
Gerson-M-Ferreira added a commit to Gerson-M-Ferreira/matplotlib that referenced this issue Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants