如何使用图例(matplotlib)自动缩放图形? [英] How to autoscaled graphs with picking legend (matplotlib)?
问题描述
我正在使用matplotlib编写采摘图例.目的是通过单击图例来隐藏和显示曲线.我找到了这个解决方案(显示pyplot图后隐藏行),效果很好.
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?
它们是我在以下图片上想要的示例.首先,图像1代表图形.然后,单击图例5 * sin(x),橙色曲线如预期那样消失(图2).在图像2上,y轴未优化.预期结果在图像3上.
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.
非常感谢您,
推荐答案
添加对relim方法的调用,且visible_only标志设置为True,并更新轴即可执行所需的操作.这将需要添加到您的on_click/update方法中.
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.
这篇关于如何使用图例(matplotlib)自动缩放图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!