KOS1110 Python - Introduction To Plotting: 1. Getting Started - What Do You Need?
KOS1110 Python - Introduction To Plotting: 1. Getting Started - What Do You Need?
For this tutorial, you’ll be using two python modules called Numpy and Matplotlib.
Numpy – this is the module which does most array and mathematical
manipulation
Matplotlib – this is the module you’ll be using for plotting
Pylab – is a module that gets installed alongside matplotlib
We will use Googlecolab to do the plotting. Click Here.
2. Basic plots
Two basic plot types which you will find very often are (x,y) line and scatter plots.
Some codes for making these two types of plots is included in this section.
import numpy as np
import pylab as pl
OUTPUT:
1
Fig. 1 – Line plot
Alternatively, you may want to plot quantities which have an x and y position. For example,
plotting the location of stars or galaxies in a field for example such as the plot in Fig. 2. Try
running the script below to do this:
# scatterplot.py
import numpy as np
import pylab as pl
OUTPUT:
2
2.3 Making things look pretty
pl.plot(x, y)
to
pl.plot(x, y, ‘r’)
This should give you the same line as before, but it should now be red. The other colours you
can easily use are:
plot(x,y, ‘—‘)
This should give you a dashed line now. Other linestyles you can use can be found on the
Matplotlib webpage
This should give you blue star-shaped markers. The table below gives some more options for
setting marker types:
3
2.3.4 Plot the axis titles and limits
It is very important to always label the axes of plots to tell the viewer what they are looking at.
You can do this in python by using the commands:
pl.xlabel('put text here')
pl.ylabel('put text here')
You can change the x and y ranges displayed on your plot by:
pl.xlim(x_low, x_high)
pl.ylim(y_low, y_high)
#lineplotAxis.py
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
OUTPUT:
4
Fig.3 made with lineplotAxis.py
2.3.5 Plotting more than one plot at the same set of axes
It is very easy to plot more than one plot on the same axes. You just need to define the x and y
arrays for each of your plots and then:
plot(x1, y1,'r')
plot(x2, y2,'g')
5
# make axis labels
pl.xlabel('x axis')
pl.ylabel('y axis')
# make legend
pl.legend()
Often you will have data in ascii (text) format which you will need to plot in some way. In this
section we will briefly discuss how to open, read from, and write to, files.
There are many ways to read data from a file in python. Here I am going to illustrate one
simple method, but you should read the Python, Numpy and Matplotlib manuals to find out
about the other ways which might sometimes apply better to you particular situation. You can
use Numpy to read in numerical values from a text file. For example, let’s take the text file
called fakedata.txt which contains the following data (2 columns of numbers):
# data.txt
0 0
1 1
2 4
3 9
4 16
5 25
6 36
6
7 49
8 64
9 81
We can read this into a numpy 2D array and plot the second column against the first using the
macro readFileAndPlot.py and shown in Fig. 12:
#readFileAndPlot.py
import numpy as np
import pylab as pl
data = np.loadtxt('fakedata.txt')
# plot the first column as x, and second column as y
pl.plot(data[:,0], data[:,1], 'ro')
pl.xlabel('x')
pl.ylabel('y')
pl.xlim(0.0, 10.)
pl.show()
OUTPUT:
There are also various ways to write text to a text file. Here we show you one possible way to
do it. You first have to open a file to write to, then write what you want to the file, and then,
do not forget to close the file! See the code below, writeToFile.py to see how to do this:
# writeToFile.py
import numpy as np
print('x=', x )
print('y=', y )
7
# Now open a file to write the data to
# 'w' means open for ’writing’
file = open('testdata.txt', 'w')