且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Matplotlib中从两个轴取消设置`sharex`或`sharey`

更新时间:2023-11-09 22:07:34

您可以使用ax.get_shared_x_axes()获取包含所有链接轴的Grouper对象.然后使用group.remove(ax)从该组中删除指定的轴.您还可以group.join(ax1, ax2)添加新共享.

You can use ax.get_shared_x_axes() to get a Grouper object that contains all the linked axes. Then use group.remove(ax) to remove the specified axis from that group. You can also group.join(ax1, ax2) to add a new share.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2, 10, sharex='row', sharey='row', squeeze=False)

data = np.random.rand(20, 2, 10)
for row in [0,1]:
    for col in range(10):
        n = col*(row+1)
        ax[row, col].plot(data[n,0], data[n,1], '.')

a19 = ax[1,9]

shax = a19.get_shared_x_axes()
shay = a19.get_shared_y_axes()
shax.remove(a19)
shay.remove(a19)

a19.clear()
d19 = data[-1] * 5
a19.plot(d19[0], d19[1], 'r.')

plt.show()

这仍然需要一些调整来设置刻度,但是右下图现在有其自身的局限性.

This still needs a little tweaking to set the ticks, but the bottom-right plot now has its own limits.