Chris - 7 months ago 99

Python Question

Let f be a function that depends on an independent variable x and on one (or more) parameter b.

My goal is to draw f for several values of b into the same image. And I want that b is a global value (so that I can quickly change it).

Consider the code

`import numpy as np`

import pylab as pl

b=0.5

def f(x,b):

return np.sin(x*b)*b

X = np.linspace(0,10,1000)

F = f(X,b)

pl.figure(figsize=(12, 16), num="My sine")

pl.plot(X, F, label='b = %f' %b)

pl.legend(loc="best")

This code can be evaluated multiple times, just with another value for b. Then all the curves are drawn into the same figure.

Now the problem:

If in the above code I put

`pl.subplot2grid((4, 1), (0, 0), rowspan=3)`

between

`pl.plot`

`pl.legend`

So, how can I realise that with using

`pl.subplot2grid`

Answer

Maybe you are looking for something like this?:

```
import numpy as np
import pylab as pl
def f(x,b):
return np.sin(x*b)*b
def draw(b):
X = np.linspace(0,10,1000)
F = f(X,b)
ax1.plot(X, F, label='b = %f' %b)
# Main program
fig = pl.figure(figsize=(12, 16), num="My sine")
ax1 = pl.subplot2grid((4,3), (0,0), colspan=2)
for b in [0.4, 0.5, 0.6]:
draw(b)
pl.legend(loc="best")
pl.show()
```