Plotting
To go along with mathematical operations it is naturally often handy to plot the numerical
values as a graph or chart. To illustrate this we will use the popular Matplotlib module
which is often included with SciPy and NumPy packages. Naturally, the following
examples assume that we have installed the Matplotlib, otherwise you will get an error
from the import command. From the matplotlib module we will import pyplot, which has
lots of helpful functions that can be used to display information as charts and graphs.
from matplotlib import pyplot
To use pyplot we first create some example values in a list and then call the .plot()
function, passing the data in as an argument. This will create a line graph from the values
list, but doesn’t immediately display anything on screen.
values = [x*x for x in range(10)]
pyplot.plot(values)
To actually display the line graph on screen we call the .show() function, which should
cause a pop-up window to appear.
pyplot.show()
In order to create a graph with multiple data lines we can repeatedly use plot() with
different data lists, which will all be added to the same graph, before finally invoking
show().
valuesA = [x*x for x in range(1,10)]
valuesB = [100.0/x for x in range(1,10)]
pyplot.plot(valuesA)
pyplot.plot(valuesB)
pyplot.show()
Note that once we call show() the current graph data is completely cleared, i.e. we will
get a blank plot if we repeat show() immediately after. So far the graphs have all been
plotted by supplying height (y axis) information for the values, which are plotted in order.
However, by passing two data lists to each plot we can also specify the values for the x
axis:
xVals = range(21,30)
yVals = [100.0/x for x in range(1,10)]
pyplot.plot(xVals, yVals)
pyplot.show()
As standard the plot() function will use relatively sensible defaults to determine the
look of the graphs. However, there are many extra options that can be specified to control
the drawing of the lines, axes, legends etc. Here we create a thicker purple line by setting
the color and linewidth attributes. Also, we give the line plot a textual label which will
appear if legend() is used. For the axes we control the range of displayed values for the y
axis with ylim(), here making it wider than the default, which would only go up to the
extremes of the data range, and manually specify the values for the tick marks on the axis
using yticks() (naturally xlim() and xticks() are also available).
pyplot.plot(xVals, yVals, color='purple',
linewidth=3.0, label='DataName')
pyplot.legend()
pyplot.ylim(0, 101)
pyplot.yticks([0, 25, 50, 75, 100])
As an alternative to simply showing the graph on screen we could also write an image
out to a file. Accordingly we use savefig(), though note that we do this before we call
show(), otherwise the latter would clear the current graph of data. Here we export to PNG
format by using a file name with a ‘.png’ extension and state that we require 72 dots per
inch output resolution:
pyplot.savefig(“TestGraph.png”, dpi=72)
pyplot.show()
As well as simple line graphs Matplotlib can easily create many other types of data
display. For example, here we create a scatter plot by using scatter() (rather than plot()),
where we set the style for the marker symbol and its size (here s=40). Note that, for
illustrative purposes, the valsB list is generated by using random.gauss() to take random
samples from a normal distribution with a mean of 0.0 and standard deviation 1.0.
from random import gauss
valsA = range(100)
valsB = [gauss(0.0, 1.0) for x in valsA]
pyplot.scatter(valsA, valsB, s=40, marker='*')
pyplot.show()
We could also show the data as a bar chart, where valsB will represent the heights of
the bars:
pyplot.bar(valsA, valsB, color='green')
pyplot.show()
Although we could use bar() to show histogram data, i.e. where initial values have been
grouped into different regional ranges (bins), we could instead let the hist() function do the
work of binning values (counting the number of data points in each range). Here, we
create a histogram of the random (normally distributed) sample of points by stating we
want the data grouped into 20 bin regions between the limits of −2.0 and 2.0:
pyplot.hist(valsB, bins=20, range=(-2.0, 2.0))
pyplot.show()
As a final example, the pie() function is, as you might guess, handy for making pie
charts and naturally this has options to specify the colours and textual labels for each of
the segments:
sizes = [83, 8, 4, 5]
labels = ['Arthropoda', 'Mollusca', 'Cordata', 'Others']
colors = ['#B00000', '#D0D000', '#008000', '#4040FF']
pyplot.pie(sizes, labels=labels, colors=colors)
pyplot.show()
There’s a lot more functionality and graph types in pyplot (contour plots, colour
matrices, box-and-whisker plots etc.) that we don’t have room to cover here. However,
Matplotlib is well documented at the Matplotlib website, which we link at
http://www.cambridge.org/pythonforbiology
.
Do'stlaringiz bilan baham: |