-3

I am trying to loop subplot for n rows 2 columns for Gaussian function as shown in following example code. This returns histogram and normal distribution, I tried a couple of methods unsuccessfully, any help is most appreciated.

Speed = [0,10,20,30,40]
Torque1 = []
Torque2 = []
for i in range(5):
    Trq = np.random.normal(0, 10, 5)
    Torque1.append(Trq)
for i in range(5):
    Trq = np.random.normal(0, 10, 5)
    Torque2.append(Trq)    

def gaussian_Histo(s, Title):
    mu, sigma = np.mean(s), np.std(s, ddof=1) # mean and standard deviation
    fig = plt.figure(Title, figsize=(10, 6), dpi=80)
    count, bins, ignored = plt.hist(s, 80, normed=True)
    plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) ), linewidth=2, color='r')
    plt.grid(True)
    plt.title(Title)
    plt.show()

def main():
    nrows = 3
    fig, axes = plt.subplots(nrows, 2)

    for row in axes:
#     for i in range(3):
        x = gaussian_Histo(Torque1[i], 'Torque at'+str(Speed[i])+'RPM')
        y = gaussian_Histo(Torque2[i], 'Torque at'+str(Speed[i])+'RPM')
        plot(row, x, y)

    plt.show()

def plot(axrow, x, y):
    axrow[0].plot(x, color='red')
    axrow[1].plot(y, color='green')

main()
DavidG
  • 24,279
  • 14
  • 89
  • 82
Omkar
  • 19
  • 7
  • 2
    What's the problem? – DavidG Jul 25 '17 at 08:05
  • Hi David, I gets error : ValueError: x and y must not be None – Omkar Jul 25 '17 at 12:30
  • So do you want your grid to be populated by the Gaussian curves? Could you show an image of your expected output? – DavidG Jul 25 '17 at 12:48
  • For some reason I cant edit my question to add image of plot. But I am looking at something like mentioned in first answer of this link. https://stackoverflow.com/questions/31726643/how-do-i-get-multiple-subplots-in-matplotlib – Omkar Jul 25 '17 at 19:49

2 Answers2

0

The reason you are seeing that error is because you are not returning any value from gaussian_Histo and are therefore trying to plot x = None.

I have removed the part of the code that plots each histogram individually as this will interrupt the plotting of your grid, unless you change the way you are creating that figure. As a result I have used np.histogram rather than plt.hist (plt.hist actually uses np.histogram behind the scenes)

Example:

Speed = [0,10,20,30,40]
Torque1 = []
Torque2 = []
for i in range(5):
    Trq = np.random.normal(0, 10, 5)
    Torque1.append(Trq)
for i in range(5):
    Trq = np.random.normal(0, 10, 5)
    Torque2.append(Trq)

def gaussian_Histo(s, Title):
    mu, sigma = np.mean(s), np.std(s, ddof=1) # mean and standard deviation

    count, bins = np.histogram(s, 80, normed=True)

    test = 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) )

    return test

def main():
    nrows = 3
    fig, axes = plt.subplots(nrows, 2)

    for row in axes:

        x = gaussian_Histo(Torque1[i], 'Torque at'+str(Speed[i])+'RPM')
        y = gaussian_Histo(Torque2[i], 'Torque at'+str(Speed[i])+'RPM')

        plot(row, x, y)

    plt.show()

def plot(axrow, x, y):
    axrow[0].plot(x, color='red')
    axrow[1].plot(y, color='green')

main()

This produces the figure:

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • Thank you David for pointing out error and solution you given, this helps. Yesterday I came up with code which plots histogram individually, I will post my solution in answer section below. – Omkar Jul 27 '17 at 16:58
0

I came up with code which plots histogram individually. I modified my plotting function (gaussian_Histo) which returns individual plot.

Speed = [0,10,20,30,40]
Torque1 = []
Torque2 = []
for i in range(5):
    Trq = np.random.normal(0, 10, 5)
    Torque1.append(Trq)
for i in range(5):
    Trq = np.random.normal(0, 10, 5)
    Torque2.append(Trq)    
# print(Torque1)
def gaussian_Histo(s, Title, ax = None):
    mu, sigma = np.mean(s), np.std(s, ddof=1) # mean and standard deviation
    if ax is None:
        ax = plt.gca()
    count, bins, ignored = ax.hist(s, 80, normed=True)
    ax.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) ), linewidth=2, color='r')
    ax.grid(True)
    ax.set_title(Title)
    plt.show()
for i in range(len(Speed)):
    f, (ax1, ax2) = plt.subplots(1, 2, sharey=False, figsize=(8,6), dpi=50) 
    gaussian_Histo(Torque1[i], 'Torque1 at '+str(Speed[i])+'RPM', ax1)
    gaussian_Histo(Torque2[i], 'Torque2 at '+str(Speed[i])+'RPM', ax2)

Individual Plot Results in this link

Omkar
  • 19
  • 7