Skip to content Skip to sidebar Skip to footer

How To Enable/disable Easily Shared Axis Using Matplotlib

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 re

Solution 1:

You can do:

ax2.get_shared_x_axes().remove(ax1)

References:


Solution 2:

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.


Solution 3:

You can change the Grouper object that defines which plots are sharing their axes. The Grouper object for x axes can be found in ax1._shared_x_axes as well as in ax2._shared_x_axes, since it's the same object.

To stop sharing the x axis of the both plots, you can do ax2._shared_x_axes.remove(ax2).

In order to joining them again, do ax2._shared_x_axes.join(ax1,ax2).

If you want to understand better, you can check the documentation here. ax1._shared_x_axes is a Grouper object, in which ax1 and ax2 are the joint elements.

With the ax2.get_shared_x_axes() method you just can read it, but not change it. For aplying modifications, you need to access this object directly with ax2._shared_x_axes.


Post a Comment for "How To Enable/disable Easily Shared Axis Using Matplotlib"