簡體   English   中英

Matplotlib-設置兩個x軸之間的距離

[英]Matplotlib - Set distance between two x axes

我顯示的圖像(圖)具有imshow,其寬度是其高度的5倍。 我使用此代碼在此下方添加第二個x軸:

newax = figure.add_axes(ax.get_position())
newax.patch.set_visible(False)
newax.yaxis.set_visible(False)
for spinename, spine in newax.spines.items():
   if spinename != 'bottom':
       spine.set_visible(False)

但是,第二個x軸遠低於第一個x軸。

圖片
第一個xAxis
(巨大的空間)
第二個xAxis

將ylim設置為[0,0]不起作用。 如何將第二個x軸放置在第一個x軸下僅幾個像素的位置?

編輯:添加了最少的示例,以幫助重現該問題。

import numpy as np
import matplotlib.pyplot as plt

def addReferenceToPlot(figure, ax, xlim):
    newax = figure.add_axes(ax.get_position())
    newax.patch.set_visible(False)
    newax.yaxis.set_visible(False)
    for spinename, spine in newax.spines.items():
        if spinename != 'bottom':
            spine.set_visible(False)
    newax.axvline(0.5, color='k', ls='dashed')

data = np.ones((25, 111))

specFig, specAx = plt.subplots()

addReferenceToPlot(specFig, specAx, data.shape[1])
specAx.imshow(data, origin="lower", interpolation="nearest")
specAx.set_xlim([0, data.shape[1]])
plt.show()

編輯(添加的圖片看起來像預期的結果): 預期結果

現在,較低的x軸遠低於較高的x軸,虛線從繪制的矩陣中移出。 但是,兩個軸應彼此靠近,並且虛線應在繪制的矩陣的“頂部”處停止。

def plot_mix(image1, image2):
    ax.append(plt.subplot2grid((3, 2), (1, 0)))
    ax[-1].imshow(image1)
    ax[-1].set_title('Mix Image 1')

    ax.append(plt.subplot2grid((3, 2), (2, 0)))
    ax[-1].imshow(image2)
    ax[-1].set_title('Mix Image 2')

    plt.tight_layout()
    plt.show()

多虧cphlewis和酸橙!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM