0

I've split an image up into 16 figures to plot regression and now I want to join it back together into one image.

I've written a for loop to do this but I'm having trouble understanding the advice from previous questions and where I'm going wrong. Please could someone explain why my input arrays do not have the same number of dimensions.

from scipy import interpolate


allArrays = np.array([])
for i in range(len(a)):

    fig = plt.figure()
    ax = fig.add_axes([0.,0.,1.,1.])


    if np.amax(a[i]) > 0:
        x, y = np.where(a[i]>0)
        f = interpolate.interp1d(y, x)
        xnew = np.linspace(min(y), max(y), num=40)
        ynew = f(xnew)   
        plt.plot(xnew, ynew, '-')
        plt.ylim(256, 0)
        plt.xlim(0,256)

        fig.canvas.draw()
        X = np.array(fig.canvas.renderer._renderer)
        myArray = color.rgb2gray(X)
        print(myArray.shape)
        allArrays = np.concatenate([allArrays, myArray])
        print(allArrays.shape)

    else:

        plt.xlim(0,256)
        plt.ylim(0,256)
        fig.canvas.draw()
        X = np.array(fig.canvas.renderer._renderer)
        myArray = color.rgb2gray(X)
        print(myArray.shape)
        allArrays = np.concatenate([allArrays, myArray])
        print(allArrays.shape)




    i += 1  

Output: myArray.shape (480, 640)

Error message: all the input arrays must have same number of dimensions

I'm sure it's really simple but I can't figure it out. Thanks.

1
  • 1
    What's the shape of allArrays? Commented Apr 21, 2019 at 15:26

1 Answer 1

2
In [226]: allArrays = np.array([])                                                   
In [227]: allArrays.shape                                                            
Out[227]: (0,)
In [228]: allArrays.ndim                                                             
Out[228]: 1

In [229]: myArray=np.ones((480,640))                                                 
In [230]: myArray.shape                                                              
Out[230]: (480, 640)
In [231]: myArray.ndim                                                               
Out[231]: 2

1 does not equal 2 in most worlds!

To concatenate with myArray on the default axis 0, allArrays would have to start as np.zeros((0,640), myArray.dtype). After n iterations it would grow to (n*480, 640).

In the linked answer, the new arrays are all 1d, so starting with shape (0,) is ok. But wim's answer is better - collect all arrays in a list, and do one concatenate at the end.

Repeated concatenate in a loop is hard to get right (you have to understand shapes and dimensions), and slower than list appends.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Sorry that was really obvious! I used allArrays = np.delete(allArrays, slice(0,(len(myArray))), axis=0) to remove the first numpy array after concatenation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.