Page - 25 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 25 -
Text of the Page - 25 -
1.6 Plotting,PrintingandInputData 25
t = np.linspace(-2, 2, 100) # choose 100 points in time interval
f_values = t**2
g_values = np.exp(t)
plt.plot(t, f_values, ’r’, t, g_values, ’b--’)
plt.xlabel(’t’)
plt.ylabel(’f and g’)
plt.legend([’t**2’, ’e**t’])
plt.title(’Plotting of two functions (t**2 and e**t)’)
plt.grid(’on’)
plt.axis([-3, 3, -1, 10])
plt.show()
In thiscode,yourecognizethecommandsexplainedjust above.Their impacton
theplotmaybe seen inFig.1.3,which is producedwhen theprogramisexecuted.
Fig. 1.3 The functionsf(t)= t2 andg(t)= et
Inaddition,yousee how
plt.plot(t, f_values, ’r’, t, g_values, ’b--’)
causesbothcurves tobe seen in the sameplot.Notice thestructurehere,within the
parenthesis, we first describe plotting of the one curve with t, f_values, ’r’,
before plotting of the second curve is specified by t, g_values, ’b--’. These
two “plot specifications” are separated by a comma. Had there been more curves
to plot in the same plot, we would simply extend the list in a similar way. For each
curve,colorandlinestyleisspecifiedindependentlyoftheothercurvespecifications
in the plot command(no specification givesdefault appearance).Furthermore,you
noticehow
plt.legend([’t**2’, ’e**t’])
creates the right labelling of the curves. Note that the order of curve specifications
in the plot command must be the same as the order of legend specifications in
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik