TypeError: Invalid dimensions for image data when plotting array with imshow()

There is a (somewhat) related question on StackOverflow: Showing an image with pylab.imshow() Here the problem was that an array of shape (nx,ny,1) is still considered a 3D array, and must be squeezed or sliced into a 2D array. More generally, the reason for the Exception TypeError: Invalid dimensions for image data is shown here: matplotlib.pyplot.imshow() needs a … Read more

How to stop a function

A simple return statement will ‘stop’ or return the function; in precise terms, it ‘returns’ function execution to the point at which the function was called – the function is terminated without further action. That means you could have a number of places throughout your function where it might return. Like this: