How to enable/disable easily shared axis using matplotlib

matplotlib, plot, python

Solution

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.

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.

Problem

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) ``` to create the shared axis and then ``` 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 ? Thanks !

Original source