0

I am studying the book 'A Primer on Scientific Programming with Python 2nd' by Hans Petter Langtangen. The book uses python2 but I am applying it in python3. The scotools.std library is widely used in the book, but I cannot import or install it in python3. Is there an alternative to scitools.std that works for python3? (This may solve my difficulty in following the book.)

Specifically in this question I'm looking for an alternative to Easyviz, because I can't make a movie with the graph of the Gaussian function by modifying the parameter s as desired in the question.

The python2 code presented in the book is:

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

My incomplete version in python3 is:

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

This produces the 30 images correctly, but it does not go ahead and produces the movie. *Note that I used plt.show () and I have to close 30 windows, if I don't use each generated file it shows the accumulated curves in the same graph.

So I see three ways to solve my problem:

1) Being able to correctly install and import scitools.std (this would be excellent, because the problem goes through the whole book!);

2) Getting an alternative to the scitools.std and Easyviz module;

3) Following the path I adopted in my incomplete version of the code, that is, replacing the command movie ('tmp * .png') presented by the book with something that works well in my code.

1 Answers1

0

Yes, there is a module called scitools3.

You can install by: pip install scitools3

and reboot your PC.

read more at https://pypi.org/project/scitools3/

/masa

MaSa
  • 3
  • 2
  • 3