使用subplot循环来返回python中的函数图表

2024-09-30 05:32:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试为高斯函数的n行2列循环子批,如下面的示例代码所示。这个返回直方图和正态分布,我试了几个方法都没有成功,任何帮助都非常感谢。在

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()

Tags: inforplottitledefnprangeplt
2条回答

您看到错误的原因是因为您没有从gaussian_Histo返回任何值,因此试图绘制x = None。在

我已经删除了单独绘制每个柱状图的代码部分,因为这将中断网格的绘制,除非您更改创建图形的方式。因此,我使用了np.histogram而不是plt.histplt.hist实际上在幕后使用了np.histogram

示例:

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()

这将生成一个图形:

enter image description here

我想出了单独绘制柱状图的代码。我修改了我的绘图函数(gaussian_Histo),它返回单个绘图。在

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

相关问题 更多 >

    热门问题