Dead Simple Modelling and Graphs

David Preece
3 min readMar 28, 2018

--

Modelling systems — be they financial, physical or whatever — and getting a pretty graph out the other end seems to be some kind of holy grail. But it’s dead easy, and here’s the quickest way to do it.

Get Python

If you don’t have Python installed on your computer (or you’re not sure), go to the Python website and download the most obvious one starting with “3”. Once it’s up and going, get a command line and do:

pip install matplotlib numpy

(you may need to do pip3 instead of pip)

The best tool to use with Python is “PyCharm”. Sadly the ‘community’ edition of PyCharm doesn’t do the bit we want but you can get a 30 day trial so go get PyCharm, install it, and run it.

Make a blank project

In PyCharm go File, New Project. Right click on the project in the top left hand corner (probably already coloured blue) and select New, Python file and call it something challenging like “test”. Into this window, copy paste the bit that tells it we’re doing a scientific project:

import matplotlib.pyplot as plt
import numpy as np

A box will appear in the bottom right hand corner asking if we’d like to turn scientific mode on. Click “use scientific mode”.

Simulate the system

Writing any simulation consists of three steps: set it up; iterate over whatever you’re simulating, for as many times as you want; and get the results out.

As an example, say we have a bacterium that splits and reproduces once an hour. We’re interested to see how many bacteria we have during a day. So…

bacteria = 1
for _ in range(0, 8):
bacteria = bacteria * 2
print(bacteria)

So “bacteria = 1” is our initial condition; the “range (0, 8)” is how many times you’re iterating over the simulation (in this case for eight hours); and “print” writes a result.

Right click the tab at the top of the ‘test’ file and select “Run ‘test’”. Some stuff will happen and a bunch of numbers ending with 256 will appear in a box at the bottom. Press the red square to let PyCharm know we’re done for now.

But I want graphs

Fair enough. What we need to do with graphs is to collect the results into a list then tell matplotlib to plot them for us. We start a new (blank) list with ‘results = []’ and that becomes part of the initial setup. Results get added with ‘results.append(bacteria)’. And then it’s a case of telling it what to plot ‘plt.plot(results)’, and making the results visible ‘plt.show()’. All in all you’ll end up with something that looks like this:

import matplotlib.pyplot as plt
import numpy as np

bacteria = 1
results = []
for _ in range(0, 8):
bacteria = bacteria * 2
results.append(bacteria)

plt.plot(results)
plt.show()

On the right hand side of the window you’ll see the graph of our bacteria simulation, and next to it a thumbnail. Right click the thumbnail, select “Save as File”, then save it wherever but, important, add “.png” onto the end of the filename. You should have…

Now what

For adding axes, labels and what have you go to the matplotlib tutorial. Read, play, make it do what you want. You’re done!

--

--

David Preece
David Preece

Written by David Preece

Epic nerd in Wellington, NZ. Working on marine instrumentation, Zephyr, BLE etc. etc.

No responses yet