Seite - 24 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 24 -
Text der Seite - 24 -
24 1 TheFirstFewSteps
Fig. 1.2 Vertical position of the ball computed and plotted for every 0.1s
Decoratinga Plot We have seen how the code linesplt.xlabel(’t (s)’)and
plt.ylabel(’y (m)’) in ball_plot.pyput labelst (s) andy (m) on the t-
andy-axis, respectively.Thereareotherways toenrichaplotaswell.
One thing, is to add a legend so that the curve itself gets labeled. With
ball_plot.py, we could get the legend v0*t - 0.5*g*t**2, for example, by
coding
plt.legend([’v0*t - 0.5*g*t**2’])
Whenthere ismore thana singlecurve,a legendisparticularly importantofcourse
(seesectionbelowon“multiplecurves” fora plotexample).
Another thing, is to add a grid. This is useful when you want a more detailed
impressionof thecurveandmaybecoded in thisway,
plt.grid(’on’)
Aplotmayalsogeta title on top.To geta title likeThis is a great title, for
example,we couldwrite
plt.title(’This is a great title’)
Sometimes, thedefault rangesappearingon theaxesarenotwhatyouwant themto
be.Thismaythenbespecifiedbya code line like
plt.axis([0, 1.2, -0.2, 1.5]) # x in [0, 1.2] and y in [-0.2, 1.5]
All statements just explainedwill be demonstrated in the next section, when we
showhowmultiplecurvesmaybeplotted together ina singleplot.
Multiple Curves in the Same Plot Assume we want to plot f(t) = t2 and
g(t) = et in the same plot for t on the interval [−2,2]. The following script
(plot_multiple_curves.py)will accomplish this task:
import numpy as np
import matplotlib.pyplot as plt
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik