Feeds:
Posts

## Passing a list to plot in Sage(math)

Looking on published worksheets, I found a simple code for 4th order Runge-Kutta applied to 1st order ODE here.

I took the same code and increce the number of interactions (from 2 to 100) and the array soln now was a list  of (100 x 2) data. Of course a plot is much more useful than a really long list, so the easiest way of plotting the data is adding a new line to the code,

`list_plot(soln)`

This command plots the data in the 1st column vs. data of the 2nd column. Additionally, If you want to save the plot (instead of showing it) use the filename option,

`list_plot(soln, filename='/path/to/directory/name-of-plot.jpg')`

remember that the extension could be also PDF, PNG, EPS, among others.

Thanks to omologos in the sage-dev channel of IRC

Another rather interesting way of plotting arrays  was pointed out to me by mvngu in the following code,

```sage: L1 = [randint(1, 10) for i in xrange(2)]
sage: L2 = [randint(1, 10) for i in xrange(2)]
sage: L3 = [randint(1, 10) for i in xrange(2)]
sage: L4 = [randint(1, 10) for i in xrange(2)]
sage: L5 = [randint(1, 10) for i in xrange(2)]
sage: M = matrix([L1, L2, L3, L4, L5]); M
[ 1  2]
[ 1  3]
[10  6]
[ 3  8]
[ 6  1]
sage: line2d(zip(M.column(0), M.column(1)), thickness=1)
```

The last line does the work done

For another example see the notebook by rsael at sagenb.