如何使用Matplotlib启用/禁用轻松共享的轴 [英] How to enable/disable easily shared axis using matplotlib

查看:205
本文介绍了如何使用Matplotlib启用/禁用轻松共享的轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tcaswell的先前答案为不在同一图上的图创建共享轴是完美的:)但是,我现在想知道如何禁用共享轴并重新启用它们而不必重绘或破坏任何东西? (我有多个图形,我想添加一个按钮,用户可以单击该按钮以禁用/启用这些共享轴),我找到了一种方法:

The previous answer from tcaswell on how to create shared axis for plots which are not on the same figure was perfect :) But I'm now wondering how to disable the shared axis and reenable them without having to redraw or destroy anything ? (I have multiple graphs and I want to add a button that the user can click to disable/enable those shared axes) I found a way :

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
fig2 = plt.figure()
ax2 = fig2.add_subplot(111, sharex=ax1)

创建共享轴,然后

fig2.delaxes(ax2)
ax2 = fig2.add_subplot(111)

但这需要重绘所有内容,并且可能需要花费一些时间.我没有找到禁用链接的简单功能.有比我更轻松的方法吗?

but this require to redraw everything and can take some times. I didnt find a simple function to disable the link. Is there a lighter way than what I did ?

谢谢!

推荐答案

目前尚无简便的方法,但是我认为可以 完成(这需要一点点挖掘工作)进入matplotlib内部).在axis上简单地使用相同的定位器和格式化程序对象,即可将轴链接到下方.要链接/取消链接,您将必须复制/新建所有这些对象,并更新一些辅助结构(_shared_x_axes_shared_y_axes).查看matplotlib.axes.Axes.cla()的实现,以了解您需要做什么.

There is no existing easy way to do this, however I think it can be done (it will just take a bit of digging into the matplotlib internals). The way that the axes are linked underneath is by simply using the same locator and formatter objects for both axis. To link/unlink you will have to copy/make new all of those objects, as well as update some ancillary structures (_shared_x_axes, _shared_y_axes). Look at the implementation of matplotlib.axes.Axes.cla() for some idea of what you need to do.

针对回购,有一个拉请求,该回购添加了此功能和相关的功能请求.如果您愿意(a)从源代码进行安装b)使用master(master)和c)应用此修补程序,我想开发人员将感谢您对此进行测试.

There is a pull request against the repo that adds this functionality and the associated feature request. If you are comfortable a) installing from source b) using master and c) applying this patch I think the developers would appreciate you testing this.

这篇关于如何使用Matplotlib启用/禁用轻松共享的轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆