简体   繁体   English

在matplotlib中使用带有偶数个子图的sharex

[英]using sharex with odd number of subplots in matplotlib

I have an odd number of subplots like so: 我有很多这样的子图:

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(2, 2, sharex=True)
for i, ax in enumerate(axes.flat):
    ax.plot(range(10))
fig.delaxes(axes.flat[-1])

I want them all to have the same x-axis, but easily add the x-ticks back to the plot on the right, since there is no longer a 4th plot. 我希望它们都具有相同的x轴,但由于不再有第4个图,因此可以轻松地将x-点添加回右侧的图。

It seems like there should be an easier/cleaner solution than adding each subplot manually (similar to this answer ), but I can't seem to find anything. 与手动添加每个子图相比,似乎应该有一个更简单/更干净的解决方案(类似于此答案 ),但我似乎找不到任何东西。 Thanks. 谢谢。

you can use setp to make the xtick labels visible for ax[0][1] like this 您可以使用SETP使XTICK标签可见ax[0][1]这样的

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(2, 2, sharex=True)
for i, ax in enumerate(axes.flat):
    ax.plot(range(10))
# for matploltib version 2.0.1
plt.setp(axes[0][1].get_xticklabels(), visible=True)
# for matplotlib version 2.1.1
axes[0][1].xaxis.set_tick_params(which='both', labelbottom=True, labeltop=False)
fig.delaxes(axes.flat[-1])
plt.show()

which will result in 这将导致

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM