Python 子批次奇数的Matplotlib/Pyplot共享轴

Python 子批次奇数的Matplotlib/Pyplot共享轴,python,matplotlib,axis-labels,subplot,Python,Matplotlib,Axis Labels,Subplot,我使用下面的代码生成奇数个图,我希望这些图“对接”在一起并共享轴 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(4,2,1) ax1.set_yscale('log') ax1.set_xscale('log') plt.subplot(4,2,2, sharex=ax1, sharey=ax1) plt.subplot(4,2,3, sharex=ax1, sharey=ax1) plt.s

我使用下面的代码生成奇数个图,我希望这些图“对接”在一起并共享轴

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(4,2,1)
ax1.set_yscale('log')
ax1.set_xscale('log')
plt.subplot(4,2,2, sharex=ax1, sharey=ax1)
plt.subplot(4,2,3, sharex=ax1, sharey=ax1)
plt.subplot(4,2,4, sharex=ax1, sharey=ax1)
plt.subplot(4,2,5, sharex=ax1, sharey=ax1)
plt.subplot(4,2,6, sharex=ax1, sharey=ax1)
plt.subplot(4,2,7, sharex=ax1, sharey=ax1)

plt.suptitle('The main title')
plt.xlabel('Some Value')
plt.ylabel("Value")


for ax in plt.gcf().axes:                           #To suppress Tick labels in subsequent subplots and keep only the left and bottom ones.
    print ax
    try:
        ax.label_outer()
    except:       
        pass

plt.subplots_adjust(wspace=0, hspace=0)

plt.show()
经过多次搜索,我发现使用pyplot.gcf().axes只能获得外部标签,这样标签就不会重复

这正是我想要的,当图像数量为偶数时效果很好

但是,当我有奇数个子批次(例如,定义了4x2,但只有7个子批次)时,如示例所示,我希望x轴TIC也出现在右下绘图的x轴上,而不仅仅出现在左侧子批次上


不幸的是,我是新的,我不允许发布图片。希望我的描述是清楚的。如果您可以想象一幅与此图类似的图像,您可以根据x和y记号标签在图中的位置手动打开和关闭它们。这有一些详细信息

import matplotlib.pyplot as plt

fig = plt.figure()

# Add subplots
nRows = 4
nCols = 2
nPlots = 7
ax1 = fig.add_subplot(nRows,nCols,1)
ax1.set_yscale('log')
ax1.set_xscale('log')

for n in range(1, nPlots+1):
    plt.subplot(nRows,nCols,n, sharex=ax1, sharey=ax1)

# Turn off tick lables where needed. 
index = 0
for r in range(1, nRows +1):
     for c in range(1, nCols + 1):
         index += 1
         # Turn off y tick labels for all but the first column.
         if ((c != 1) and (index <= nPlots)):  
             ax = plt.subplot(nRows, nCols, index, sharex=ax1, sharey=ax1)
             plt.setp(ax.get_yticklabels(), visible=False)
          # Turn off x tick lables for all but the bottom plot in each 
          # column. 
         if ((nPlots - index) >= nCols):
             ax = plt.subplot(nRows, nCols, index, sharex=ax1, sharey=ax1) 
             plt.setp(ax.get_xticklabels(), visible=False)

plt.subplots_adjust(wspace=0, hspace=0)

plt.show()
导入matplotlib.pyplot作为plt
图=plt.图()
#添加子批次
nRows=4
nCols=2
nPlots=7
ax1=图add_子批次(nRows、nCols、1)
ax1.set_yscale('log')
ax1.setxscale('log')
对于范围内的n(1,nPlots+1):
plt.子批次(nRows、NCOL、n、sharex=ax1、sharey=ax1)
#在需要的地方关闭勾号标签。
索引=0
对于范围内的r(1,nRows+1):
对于范围(1,nCols+1)内的c:
指数+=1
#为除第一列以外的所有列禁用y记号标签。
如果((c!=1)和(index=nCols):
ax=plt.子批次(nRows、NCOL、索引、sharex=ax1、sharey=ax1)
plt.setp(ax.getxticklabels(),visible=False)
plt.子批次调整(wspace=0,hspace=0)
plt.show()

非常好的解决方案。正是我想要的。谢谢!