我正在学习Hans Petter Langtangen的书“A Primer on Scientific with Python Petter”。这本书使用了python2,但我在python3中应用了它。书中广泛使用了scotools.std库,但我不能在python3中导入或安装它。有没有适用于python3的scitools.std的替代方案?(这可能会解决我跟着书走的困难。)
特别是在这个问题中,我正在寻找Easyviz的替代方案,因为我不能通过修改问题中的参数s来制作具有高斯函数图的电影。
书中介绍的python2代码是:
from scitools.std import *
import time
def f(x, m, s):
return (1.0/(sqrt(2*pi)*s))*exp(-0.5*((x-m)/s)**2)
m = 0
s_start = 2
s_stop = 0.2
s_values = linspace(s_start,s_stop, 30)
x = linspace(m - 3*s_start, m + 3*s_start, 1000)
# f is max for x=m; smaller s gives larger max value
max_f = f(m, m s_stop)
# Show the movie on the screen
# and make hardcopies of frames simultaneously.
counter = 0
for s in s_values:
y = f(x, m, s)
plot(x, y, axis=[x[0], x[-1], -0.1, max_f],
xlabel='x', ylabel='f', legend='s=%4.2f' % s,
savefig='tmp%04d.png' % counter)
counter += 1
#time.sleep(0.2)
# Make movie file the simplest possible way:
movie('tmp*.png')我在python3中的不完整版本是:
import time
import numpy as np
import matplotlib.pyplot as plt
def f(x, m, s):
return (1.0/(np.sqrt(2*np.pi)*s))*np.exp(-0.5*((x-m)/s)**2)
m = 0
s_start = 2
s_stop = 0.2
s_values = np.linspace(s_start, s_stop, 30)
x = np.linspace(m - 3*s_start, m + 3*s_start, 1000)
# f is max for x=m; smaller s gives larger max value
max_f = f(m, m, s_stop)
# Show the movie on the screen
# and make hardcopies of frames simultaneosly.
counter = 0
for s in s_values:
y = f(x, m, s)
plt.plot(x, y)
plt.xlim(x[0], x[-1])
plt.ylim(-0.1, max_f + 0.1)
plt.xlabel('x')
plt.ylabel('f')
plt.legend('s=%4.2f' % s)
plt.savefig('tmp%04d.png' % counter)
counter += 1
#time.sleep(0.2)
plt.show()这会正确地生成30个图像,但不会继续生成电影。*请注意,我使用了plt.show (),并且我必须关闭30个窗口,如果我不使用每个生成的文件,它将在同一图形中显示累积曲线。
所以我认为有三种方法可以解决我的问题:
1)能够正确安装和导入scitools.std (这将是非常好的,因为这个问题贯穿了整本书!);
2)获得scitools.std和Easyviz模块的替代方案;
3)遵循我在不完整的代码版本中采用的路径,即用在我的代码中运行良好的内容替换本书中呈现的命令电影('tmp * .png')。
发布于 2020-04-27 11:30:58
是的,有一个叫做scitools3的模块。
可以通过以下方式进行安装: pip install scitools3
然后重启你的电脑。
有关更多信息,请访问https://pypi.org/project/scitools3/
/masa
https://stackoverflow.com/questions/60012571
复制相似问题