使用排列在网格中的线图覆盖matplotlib imshow

DrS*_*SAR 3 python matplotlib

我想在图像上绘制一些曲线

使用此代码我非常接近:

G=plt.matplotlib.gridspec.GridSpec(64,1)
fig = plt.figure()
plt.imshow(img.data[:,:],cmap='gray')
plt.axis('off')
plt.axis([0,128,0,64])
for i in arange(64):
    fig.add_subplot(G[i,0])
    plt.axis('off')
    # note that vtc.data.shape = (64, 128*400=51200)
    # so every trace for each image pixel is 400 points long
    plt.plot(vtc.data[i,:])
    plt.axis([0, 51200, 0, 5])
Run Code Online (Sandbox Code Playgroud)

我得到的结果看起来像这样: 在图像上的线图

问题在于,虽然我似乎能够摆脱水平(x)方向上的所有填充,但是图像中的填充量和垂直方向上的堆积图存在不同的填充量.

我试过用

ax = plt.gca()
ax.autoscale_view('tight')
Run Code Online (Sandbox Code Playgroud)

但这也没有减少保证金.

如何通过尺寸(f m)-by-(f n)的图像的爆炸(因子f)版本精确地排列m-by-n线图的网格?


更新和解决方案:@RutgerKassies的答案非常有效.我使用他的代码实现了它:

fig, axs = plt.subplots(1,1,figsize=(8,4))
axs.imshow(img.data[:,:],cmap='gray', interpolation='none')
nplots = 64
fig.canvas.draw()
box = axs._position.bounds
height = box[3] / nplots
for i in arange(nplots):
    tmpax = fig.add_axes([box[0], box[1] + i * height, box[2], height])
    tmpax.set_axis_off()
    # make sure to get image orientation right and 
    tmpax.plot(vtc.data[nplots-i-1,:],alpha=.3)
    tmpax.set_ylim(0,5)
    tmpax.set_xlim(0, 51200)
Run Code Online (Sandbox Code Playgroud)

改善了形象

Rut*_*ies 6

我认为最简单的方法是使用'imshow axes'中的边界来手动计算所有'lineplot axes'的边界:

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(1,1,figsize=(15,10))

axs.imshow(np.random.rand(50,100) ,cmap='gray', interpolation='none', alpha=0.3)

nplots = 50

fig.canvas.draw()

box = axs._position.bounds
height = box[3] / nplots

for i in arange(nplots):

    tmpax = fig.add_axes([box[0], box[1] + i * height, box[2], height])
    tmpax.set_axis_off()

    tmpax.plot(np.sin(np.linspace(0,np.random.randint(20,1000),1000))*0.4)
    tmpax.set_ylim(-1,1)
Run Code Online (Sandbox Code Playgroud)

上面的代码似乎很好,但我确实有一些问题与autoscale切断部分情节.尝试删除最后一行以查看效果,我不知道为什么会发生这种情况.

在此输入图像描述