Note that there are some explanatory texts on larger screens.

plurals
  1. POoverlay matplotlib imshow with line plots that are arranged in a grid
    text
    copied!<p>I would like to plot a number of curves over an image</p> <p>Using this code I am reasonably close:</p> <pre><code>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]) </code></pre> <p>The result that I am getting looks like this: <img src="https://i.stack.imgur.com/sN8iA.png" alt="lineplots over image"></p> <p>The problem is that while I seem to be able to get rid of all the padding in the horizontal (x) direction, there is different amount of padding in the image and the stacked plots in the vertical direction.</p> <p>I tried using </p> <pre><code>ax = plt.gca() ax.autoscale_view('tight') </code></pre> <p>but that didn't reduce the margin either.</p> <p>How can I get a grid of m-by-n line plots to line up precisely with a blown up (by factor f) version of an image with dimensions (f<em>m)-by-(f</em>n)? </p> <hr> <p><strong>UPDATE and Solution</strong>: The answer by @RutgerKassies works quite well. I achieved it using his code like so:</p> <pre><code>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) </code></pre> <p><img src="https://i.stack.imgur.com/e2HI7.png" alt="improved image"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload