Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet114/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   110   111   112   113   114   115   116   117   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

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

.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   110   111   112   113   114   115   116   117   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish