Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Missing axes limits auto-scaling support for LineCollection #29219

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

Open
carlosgmartin opened this issue Dec 2, 2024 · 13 comments
Open

Comments

@carlosgmartin
Copy link

Bug summary

Matplotlib is missing auto-scale support for LineCollection.

Related issues:

Code for reproduction

from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

lc = LineCollection([[(x, x ** 2) for x in range(5)]])
ax = plt.gca()
ax.add_collection(lc)
# ax.autoscale()  # need to manually call this
plt.show()

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

macOS 14.6.1

Matplotlib Version

3.9.3

Matplotlib Backend

macosx

Python version

3.12.7

Jupyter version

7.2.2

Installation

pip

@jklymak
Copy link
Member

jklymak commented Dec 4, 2024

Is this a bug? add_collection has an explicit autolim parameter, which is False by default.. Ooops sorry, it's True by default, so I'm not sure why this is broken.

@li-ruihao
Copy link

li-ruihao commented Dec 5, 2024

Hello, would this be a good first issue? This is my first time contributing to Matplotlib, can I take a look at this? I know issues will not be assigned and PRs are considered on a first come first serve basis, but I need to work on an issue for a school project so I am wondering how much learning curve this requires.

@QuLogic
Copy link
Member

QuLogic commented Dec 5, 2024

There is already a PR open to fix this, linked above. I have not looked at it myself to see what state it is in.

@rcomer
Copy link
Member

rcomer commented Dec 6, 2024

Looking through the axes._axes module, the methods which add a collection all call _request_autoscale_view after add_collection. So I think the autolim parameter works to update the data limits, but the axises still need to be told those limits have changed. This would appear to suggest that the current behaviour is intentional but if nothing else we should add a docstring to add_collection and make the expected behaviour clear.

Edit: autoscaling is discussed explicitly in this example.

@carlosgmartin
Copy link
Author

@rcomer "Intentional" or not, the current behavior is not user-friendly and should be improved.

@rcomer
Copy link
Member

rcomer commented Dec 7, 2024

@carlosgmartin I agree that the current behaviour is unlikely to be what anyone wants. Indeed, if you plot something else that has autoscaling after adding the collection then it autoscales both. Whether something gets autoscaled should not depend on whether you add something after.

from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

lc = LineCollection([[(x, x ** 2) for x in range(5)]])
ax = plt.gca()
ax.add_collection(lc)
ax.plot([1, 0])
plt.show()

image

However, it does appear to have been deliberately written this way and I think we should try to understand why it was written this way. Understanding that can help us scope what kind of task it would be to change it.

@rcomer
Copy link
Member

rcomer commented Dec 7, 2024

If we simply add a call to _request_autoscale_view within add_collection, we get two test failures:

  1. test_path_collection where the current image is this:
    scatter-expected_eps
    but the new behaviour gives this, because the y-axis is now much taller
    scatter_eps
    This test was added relatively recently by @oscargus so perhaps he has an opinion on what to do with it.

  2. test_arc_in_collection tests consistency between add_collection and add_patch. We can make this one pass by also calling _request_autoscale_view within add_patch, but then we get about a dozen additional test failures...

@timhoffm
Copy link
Member

timhoffm commented Dec 7, 2024

We have a lazy limit update mechanism, but I don’t know the details and which part of the code are responsible for which aspects of that. I believe we have to clarify this to find out, what the right solution is. It might also be that this is currently not handled consistently across functions.

@oscargus
Copy link
Member

oscargus commented Dec 7, 2024

  1. This test was added relatively recently by @oscargus so perhaps he has an opinion on what to do with it.

The purpose of the test was to get coverage, so the test image is just randomly generated. My impression is though that everything is shown in the test image so the compression is not caused by things that are not drawn. I guess seeing the axis here would be interesting to understand what happens. (As the data is randomly generated, I guess I just tried things until I got a reasonable looking image.)

Note that this test was for a previously untested part of the ps-backend, so it may very well be that there is a bug in the ps-backend and that one should run the same code with other backends so see if that is the case...

@oscargus
Copy link
Member

oscargus commented Dec 7, 2024

Running the code in the qt-backend gives a differently looking image. The scatter looks quite similar, but the PathCollection is rendered differently, larger size and no black edges.

image

Saving it as PDF leads to a similar image as the test image, but no black edges.

image

(both are screen shots)

So something is quite strange here...

Edit: this is on 3.8.3

@rcomer
Copy link
Member

rcomer commented Dec 7, 2024

I think the black edges are just because the test runs in "classic" mode.

@oscargus
Copy link
Member

oscargus commented Dec 7, 2024

I think the black edges are just because the test runs in "classic" mode.

Could be. Documentation may lead one to believe differently:

default: [rcParams["patch.edgecolor"]] (default: 'black')

The return value of get_edgecolor() is array([], shape=(0, 4), dtype=float64) though.

@oscargus
Copy link
Member

oscargus commented Dec 7, 2024

I read up a bit in the source and there is rcParam['patch.force_edgecolor'] which if True, the statement in the documentation is correct, otherwise the edge color is set to 'none'

I can sort out a correction to the documentation if nothing else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants