问题
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