2

In my plotting application written in Python, I use matplotlib (with TkAgg backend) to draw some data.

This plot is interactive, so the user can drag/zoom the plot region. However, sometimes the application needs to clear the curves and plot them anew - and as a result, the plot rescales to the extent of the curves.

It would be better to keep the user-defined limits. However, calling e.g. ax.get_xlim() returns the limits that were originally set by ax.set_xlim(...), not the current limits that the user has changed by their mouse.

I tried hard to find the solution in the documentation and examples, yet still I could not resolve this simple task: How to get the current limits of interactive matplotlib plot?

dominecf
  • 384
  • 4
  • 13
  • If you are interested, I can strip my program of ca. 800 lines of code and provide also a minimum working example of what I need. But I believe it is quite clear. – dominecf Mar 10 '17 at 13:44
  • The examples [here](http://stackoverflow.com/questions/31490436/matplotlib-finding-out-xlim-and-ylim-after-zoom) don't work? – berna1111 Mar 10 '17 at 13:50
  • @berna1111 Will check this out. – dominecf Mar 10 '17 at 13:53
  • You can also try to [turn off auto-scaling](http://stackoverflow.com/questions/38629830/how-to-turn-off-autoscaling-in-matplotlib-pyplot): `plt.autoscale(False)` – berna1111 Mar 10 '17 at 13:55
  • There must be some serious trouble in my codes, I guess. None of these two hints makes any change in my program. Matplotlib is great for very complex plotting, but some trivial settings can take hours of trial and error... – dominecf Mar 10 '17 at 14:15
  • Try and replicate the situation in a simpler example, you might catch what's amiss. Are you sure you are asking for the limits of the correct `ax` (you are not using that name for anything else)? Place the `plt.autoscale(False)` right after the `import matplotlib.pyplot/pylab as plt`, or try `ax.autoscale(False)` after creating the axis. – berna1111 Mar 10 '17 at 14:26
  • I am somewhat tight on time now, so I will make further testing later. Currently, I have established that directly in the resize event handler, `axes.get_xlim()` gives the correct value, but in the plotting routine, the `ax.get_xlim()` is again wrong. This appears really weird to me, but I could fix it for now. I am still puzzled why it behaves this way. – dominecf Mar 10 '17 at 14:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137801/discussion-between-berna1111-and-dominecf). – berna1111 Mar 11 '17 at 00:51

0 Answers0