Python matplotlib 共享 x 轴但不显示两者的 x 轴刻度标签,只有一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4209467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib share x axis but don't show x axis tick labels for both, just one
提问by thenickname
I'm using python + matplotlib and I'm having two plots share an axis. If you try to set graph1.set_xticklabels([])while sharing an axis, it has no effect because it is shared. Is there a way to share the axis AND be able to hide the x axis of one plot?
我正在使用 python + matplotlib 并且我有两个图共享一个轴。如果您graph1.set_xticklabels([])在共享轴时尝试设置,则无效,因为它是共享的。有没有办法共享轴并能够隐藏一个图的 x 轴?
采纳答案by Joe Kington
This is a common gotcha when using shared axes.
这是使用共享轴时的常见问题。
Fortunately, there's a simple fix: use plt.setp(ax.get_xticklabels(), visible=False)to make the labels invisible on just one axis.
幸运的是,有一个简单的解决方法:用于plt.setp(ax.get_xticklabels(), visible=False)使标签仅在一个轴上不可见。
This is equivalent to [label.set_visible(False) for label in ax.get_xticklabels()], for whatever it's worth. setpwill automatically operate on an iterable of matplotlib objects, as well as individual objects.
这相当于[label.set_visible(False) for label in ax.get_xticklabels()],无论它值多少钱。 setp将自动对可迭代的 matplotlib 对象以及单个对象进行操作。
As an example:
举个例子:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax1.plot(range(10), 'b-')
ax2 = fig.add_subplot(2,1,2, sharex=ax1)
ax2.plot(range(10), 'r-')
plt.setp(ax1.get_xticklabels(), visible=False)
plt.show()


回答by esmit
Per a thread on matplotlib-users, you could use
根据matplotlib-users上的一个线程,您可以使用
import matplotlib.pyplot as plt
for ax in plt.gcf().axes:
try:
ax.label_outer()
except:
pass
回答by ImportanceOfBeingErnest
You can share the axes during subplot creation with plt.subplotsas
您可以在子图创建期间使用plt.subplotsas共享轴
fig, axes = plt.subplots(nrows=2, sharex=True)
This will automatically turn the ticklabels for inner axes off.
这将自动关闭内轴的刻度标签。
Complete example:
完整示例:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, sharex=True)
axes[0].plot([1,2,3])
axes[1].plot([3,2,1])
plt.show()
回答by truhanen
You could use Axes.tick_params():
您可以使用Axes.tick_params():
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212, sharex=ax1)
ax1.tick_params(labelbottom=False)
回答by Mitch
Unfortunately, I am not allowed to comment on esmit's answer (which is the best solution in my opinion, thanks esmit), so I have to write my comment as a new answer: I put his solution into a simple function
不幸的是,我不允许对 esmit 的答案发表评论(这是我认为最好的解决方案,谢谢 esmit),所以我必须将我的评论写为新答案:我将他的解决方案放入一个简单的函数中
def remove_inner_ticklabels(fig):
for ax in fig.axes:
try:
ax.label_outer()
except:
pass
which you can call before plt.show(). Joe Kington's answer did not work for me for some reason.
您可以在此之前调用plt.show()。由于某种原因,乔金顿的回答对我不起作用。

