我尝试为高斯函数的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()
您看到错误的原因是因为您没有从
gaussian_Histo
返回任何值,因此试图绘制x = None
。在我已经删除了单独绘制每个柱状图的代码部分,因为这将中断网格的绘制,除非您更改创建图形的方式。因此,我使用了
np.histogram
而不是plt.hist
(plt.hist
实际上在幕后使用了np.histogram
)示例:
这将生成一个图形:
我想出了单独绘制柱状图的代码。我修改了我的绘图函数(gaussian_Histo),它返回单个绘图。在
Individual Plot Results in this link
相关问题 更多 >
编程相关推荐