0

I have a 3D numpy array of shape (64,200,200). I want to plot a series of 2D histograms iterating along the 1st axis, i.e. I want a 2D histogram for each (200x200) slice. I have tried with :

for i in range(len(a1)-1): #a1 is the array in question plt.hist2d(a1[i,:,0],a1[i,0,:]) plt.show() However, I want to loop along the 1st axis and store each 2D array slice in an array series with names b1,b2,b3,...b64 which can be produced in the loop itself (it is difficult to do so manually). Then using imshow I can plot the desired arrays or even loop along the entire series. Kindly suggest if this is feasible or there is a more efficient way of doing this. Thanks in advance.

1 Answer 1

1

I'm not sure why you need to actually create each of the b1, b2, ..., since you can just store them in a sequence. Also, I don't really understand what you're plotting in the histogram. plt.hist2d(a1[i, :, 0], a1[i, 0, :]) will use only the first column and row of a1[i], so you're missing the majority of the data.

Regardless, if you want each of the sub-arrays of a1 in a separate histogram, the easiest way to chunk the data would be np.split.

for bi in map(np.squeeze, np.split(a1, len(a1))):
    plt.hist2d(bi[:, 0], bi[0, :])

np.split returns a list of arrays, in this case, len(a1) == 64 of them. These are a1[0], a1[1], etc.

Edit: Added np.squeeze, since np.split returns arrays of the same rank as the input.

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

2 Comments

This returns ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
@AMC Edited to account for the fact that np.split returns arrays of the same rank as its input.

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.