3

我在 matplotlib 中使用子图。由于我所有的子图都具有相同的 x 轴,因此我只想在底部图上标记 x 轴。如何从一个轴上移除 xtics?

4

2 回答 2

4

正如这里所指出的,以下工作!

plt.tick_params(\
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    labelbottom='off') # labels along the bottom edge are off
于 2014-07-16T15:21:40.030 回答
3

丹,如果您以 OOP 方式使用

import matplotlib.pyplot as plt
fig, ax_arr = subplots(3, 1, sharex=True)

那么使用类似的东西应该很容易隐藏x轴标签

plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
# or
plt.setp([a.get_xticklabels() for a in ax_arr[:-1]], visible=False)

但是看看这个链接,一些更进一步的例子将被证明是有用的。

编辑:

如果你不能使用plt.subplots(),我仍然假设你可以做

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

ax1.plot(x1, y1)
ax2.plot(x2, y2)

plt.setp(ax1.get_xticklabels(), visible=False)

如果您有 2 个以上的子图,例如

ax1 = fig.add_subplot(N11)
ax2 = fig.add_subplot(N12)
...
axN = fig.add_subplot(N1N)

plt.setp([a.get_xticklabels() for a in (ax1, ..., axN-1)], visible=False)
于 2013-08-15T16:57:04.447 回答