'matplotlib set shared axis [duplicate]
Using matplotlib, it seems the only time to set the sharex
or sharey
axis parameters are during sub_plot creation (add_subplot()
, subplot()
, subplots()
). For an axes
class there are methods for getting axis sharing (get_shared_x_axes()
, get_shared_y_axes()
), but no corresponding methods for setting sharing. Maybe this is an API oversight, or perhaps it did not fit architecturally.
Is there a way to change the shared axis parameter?
For those that ask why: I'm using a matrix of plots dynamically, and can control this using view limits, but it just seems like there could be an easier way, and turning sharing on/off and using autoscale would be it.
Thanks.
Solution 1:[1]
The answer is that the way shared axes are set up is to share some of the internal state of the two axes. It is a tad tricky to get right and the code to do it on-the-fly (both linking and unlinking) doesn't exist in the library yet.
See this PR for work on-going work on un-linking axes. Help testing and developing this feature would be appreciated.
Solution 2:[2]
Just to mention that a method for sharing axes after their creation does exist by now. For two axes ax1
and ax2
you can use
ax1.get_shared_x_axes().join(ax1, ax2)
See How share x axis of two subplots after they are created?.
Solution 3:[3]
As of v3.3 there exist the new Axes.sharex
, Axes.sharey
methods:
ax1.sharex(ax2)
ax1.sharey(ax3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | tacaswell |
Solution 2 | Community |
Solution 3 |