Seite - 20 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 20 -
Text der Seite - 20 -
20 1 TheFirstFewSteps
Thenyoucould (inprinciple)doa lot ofother things inyourcode,beforeyouplot
thesecondcurveby
plt.plot(family_member_no, H)
plt.hold(’off’)
Notice the use ofhold here. hold(’on’) tells Python to plot also the following
curve(s) in the samewindow. Python does so until it readshold(’off’). If you
donotuse thehold(’on’)orhold(’off’)command, the secondplot command
will overwrite thefirstone, i.e., yougetonly the secondcurve.
In case youwould like the two curvesplotted in two separate plots, youcando
thisbyplotting thefirst curvestraightforwardlywith
plt.plot(family_member_no, h)
thendoother things inyourcode,beforeyoudo
plt.figure()
plt.plot(family_member_no, H)
Note how the graphs are made continuous by Python, drawing straight lines be-
tween the fourdata points of each family. This is the standardwayofdoing it and
wasalsodonewhenplottingour1001heightcomputationswithball_plot.py in
Chapter1.4.However, since therewere somanydatapoints then, thecurve looked
niceandsmooth. Ifpreferred,onemayalsoplotonly thedatapoints. For example,
writing
plt.plot(h, ’*’)
willmarkonly thedatapointswith the star symbol.Other symbols like circles etc.
maybeusedaswell.
There aremany possibilities in Python for adding information to a plot or for
changing its appearance. For example,youmayadda legendby the instruction
plt.legend(’This is some legend’)
oryoumayadda title by
plt.title(’This is some title’)
Thecommand
plt.axis([xmin, xmax, ymin, ymax])
will define the plotting range for the x axis to stretch from xmin to xmax and,
similarly, theplotting range for they axis fromymin toymax. Saving thefigure to
file is achievedby thecommand
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik