Matplotlib/Pyplot共享轴,用于奇数个子图 [英] Matplotlib/Pyplot shared axis for odd number of subplots

查看:370
本文介绍了Matplotlib/Pyplot共享轴,用于奇数个子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来生成奇数个图,这些图我想对接" 在一起并共享坐标轴.

I am using the following code to produce an odd number of plots that I want 'butted' together and sharing axes.

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 ,我只能获得外部标签,从而不会重复标签.

I found out after many searches that using pyplot.gcf().axes, I can obtain only the outer labels such that labels are not repeated.

这正是我想要的,并且在有偶数图像时效果很好.

This is exactly what I want and works well when there are even number of images.

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

However, when I have odd number of subplots (e.g. 4x2 defined but with only 7 subplots), as shown in the example, I want the x-axis tics to appear on the x-axis of the bottom right plot as well and not only on the left-hand side subplot.

很遗憾,我是新手,所以我不能发布图像.希望我的描述清楚.如果您能想象出类似于此

Unfortunately, I am new and I am not allowed to post an image. Hopefully, my description is clear. If you can imagine an image similar to the one on this link

推荐答案

您可以根据它们在图中的位置手动打开和关闭x和y刻度标签.此演示具有更多信息.

You can turn the x and y tick labels on and off manually based on their location in the figure. This demo has some more information.

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共享轴,用于奇数个子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆