3

I have a 3x10 2d ndarray that I would like to do a matplotlib hist plot. I want a hist plot of each array row in one subplot. I tried supplying the ndarray directly but discovered matplotlib would provide hist plots of each column of the ndarray, which is not what I want. How can I achieve my objective? Presently, I have to explicitly declare the hist() commands for each row and I would prefer to avoid this approach.

import numpy as np
import matplotlib.pyplot as plt

d = np.array([[1, 2, 2, 2, 3, 1, 3, 1,   2,  4, 5],
              [4, 4, 5, 5, 3, 6, 6,  7,   6,  5, 7],
              [5, 6, 7, 7, 8, 8, 9, 10, 11, 12, 10]] )

print( '\nd', d )
             
fig, ax = plt.subplots(4, 1)
dcount, dbins, dignored = ax[0].hist( d, bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d' )
d0count, d0bins, d0ignored = ax[1].hist( d[0,:], bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d0', alpha=0.2 )
d1count, d1bins, d1ignored = ax[2].hist( d[1,:], bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d1', alpha=0.2 )
d2count, d2bins, d2ignored = ax[3].hist( d[2,:], bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d2', alpha=0.2 )
ax[0].legend()
ax[1].legend()
ax[2].legend()
ax[3].legend()
print( '\ndcount', dcount )
print( '\ndbins', dbins )
print( '\ndignored', dignored )
print( '\nd0count', d0count )
print( '\nd0bins', d0bins )
print( '\nd0ignored', d0ignored )
print( '\nd1count', d0count )
print( '\nd1bins', d0bins )
print( '\nd1ignored', d0ignored )
print( '\nd2count', d0count )
print( '\nd2bins', d0bins )
print( '\nd2ignored', d0ignored )
plt.show()

figure

2 Answers 2

2
# import needed packages
import numpy as np
import matplotlib.pyplot as plt

Create data to plot

Using list comprehension and numpy.random.normal:

gaussian0=[np.random.normal(loc=0, scale=1.5) for _ in range(100)]
gaussian1=[np.random.normal(loc=2, scale=0.5) for _ in range(100)]

gaussians = [gaussian0, gaussian1]

Plot with one hist call only

for gaussian in gaussians:
    plt.hist(gaussian,alpha=0.5)
plt.show()

Resulting in:

enter image description here

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

1 Comment

I think there should be a simpler way to plot a 2d numpy array than to use a for-loop.
1

I found a simpler way. Transpose d. That is, replace

dcount, dbins, dignored = ax[0].hist( d, bins=[2, 4, 6, 8, 10, 12], histtype='bar', label='d' )

with

dcount, dbins, dignored = ax[0].hist( d.T, bins=[2, 4, 6, 8, 10, 12], histtype='bar', label=['d0', 'd1','d2'], alpha=0.5 )

fig1a

I was hoping for matplotlib's hist() command would have some command to do it but did not find it. Transposing the numpy array worked. I wonder if this is the usual way matplotlib user to do so?

Comments

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.