Skip to content

[Bug]: Polar plot wrong Y values when set_ylim is used #24790

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
afarcy opened this issue Dec 20, 2022 · 7 comments · May be fixed by #24817
Open

[Bug]: Polar plot wrong Y values when set_ylim is used #24790

afarcy opened this issue Dec 20, 2022 · 7 comments · May be fixed by #24817

Comments

@afarcy
Copy link

afarcy commented Dec 20, 2022

Bug summary

Plotting 2 trivial lines in a polar axes. Those lines are quarter-arcs with constant y values:

  • line1 is only y=5
  • line2 is only y=9
    The figure is plotted using a canvas.draw() call.

If line1 is deleted, set_ylim(8, 10) will make line2 disappear. In fact it can be found at y=13 for some reason.
Inspecting the object values (get_data) returns the correct values. It is as if the data was correct but the rendering off by just "4"

The issue is that set_ylim on a polar plot seems to apply an offset on the remaining lines after some workflow.

Code for reproduction

import numpy
import matplotlib.pyplot as plt


x = numpy.linspace(0, numpy.pi/2., num=90)
y1 = numpy.full(90, 5)
y2 = numpy.full(90, 9)

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.set_xlim(0, numpy.pi/2.)
ax.set_ylim(4, 10)
ax.plot(x, y1)
ax.plot(x, y2)
fig.canvas.draw()

ax.lines.pop(0) # If popping the 2nd line no issue encountered
if True:
    ax.set_ylim(8, 10) # This moves the 2nd line from y=9 to y=13

plt.show()

See simpler example at #24790 (comment)

Actual outcome

line2 rendering is done as if line2 had all its y values = 13

Expected outcome

line2 should be rendered as a if its values were y=9 (same as get_data return values)

Additional information

  • Deleting the other line instead gives no issue. Only deleting a line that contains data inferior to every other lines triggers the issue
  • Not calling canvas.draw before is not an option: My application is interactive the user is able to delete/add lines interactively
  • The issue seems to come from set_ylim on a polar plot

Operating system

Debian 11.6

Matplotlib Version

3.3.4; 3.4.2; 3.5.3; 3.6.2

Matplotlib Backend

tkAgg; Qt5Agg

Python version

3.7.10; 3.9.2

Jupyter version

No response

Installation

Linux package manager

@dstansby
Copy link
Member

Weird - I can reproduce this on the current main branch, thanks for the reproducible example.

@dstansby
Copy link
Member

Although I think the second line is rendered at r=5, not r=13 - can you confirm if that's correct for you too?

@afarcy
Copy link
Author

afarcy commented Dec 21, 2022

@dstansby Thanks for the quick reply!
No I have something different than you: line2 has r=13 after removing line1.
I should also mention that you can have several lines, with different r's. Then removing the "smallest" line will apply an offset to all other remaining lines. Maybe this can shed more light on the issue?

@dstansby
Copy link
Member

dstansby commented Dec 22, 2022

Ah, I was using different y-lims, I think the line is being moved to the lower ylim + 5 (which in the original post case is 8 + 5 = 13).

@dstansby
Copy link
Member

Here's a slightly simpler example (sorry for the train of thought comments, but hopefully helpful!)

import numpy
import matplotlib.pyplot as plt

x = numpy.linspace(0, numpy.pi/2., num=90)
y = numpy.full(90, 5)

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.set_ylim(4, 10)
# Line drawn correctly at y=5
ax.plot(x, y)
fig.canvas.draw()
# Line now drawn incorrectly at y=1
ax.set_ylim(0, 10)

plt.show()

@dstansby dstansby linked a pull request Dec 26, 2022 that will close this issue
6 tasks
@Rainald62
Copy link

Rainald62 commented Mar 15, 2025

I can't reproduce the original example because of an exception, AttributeError: 'ArtistList' object has no attribute 'pop'.
I can reproduce the simpler example, still with the wrong result (matplotlib 3.10.0).
At the time of writing, this page was the only search result for get_data+canvas.draw.

@timhoffm
Copy link
Member

I can't reproduce the original example because of an exception, AttributeError: 'ArtistList' object has no attribute 'pop'.

ax.lines was never be thought to be user-modifiable. It's been switched to read-only meanwhile. The relvant example is this https://github.com/dstansby

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

Successfully merging a pull request may close this issue.

4 participants