How to autoscaled graphs with picking legend (matplotlib)?

☆樱花仙子☆ 提交于 2021-02-20 04:06:20

问题


I am working on a picking legend with matplotlib. The goal is to hide and shows curves, by clicking on the legend. I found this solution (Hiding lines after showing a pyplot figure), which works very well.

I want improve this code to adapt automatically the axis after each clic on the legend. I don't know if it is possible. Do you have any hint?

Their is an example of what I want on the images below. First of all, the Image 1 represents the graph. Then after clicking on the legend 5*sin(x), the orange curve disappears as expected (Image 2). On the Image 2, the y-axis is not optimized. The result expected is on the Image 3.

Example

Thank you very much,


回答1:


Adding a call to the relim method with the visible_only flag set to True and updating the axis should perform the way you want. This will need to be added to your on_click/update method.

# Where ax2 is a reference to your second axis
ax2.relim(visible_only=True)  # Ignore the line you've hidden when rescaling.
ax2.autoscale_view()  # assuming the axis has not been altered by set_xlim etc.


来源:https://stackoverflow.com/questions/62081608/how-to-autoscaled-graphs-with-picking-legend-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!