Seite - 26 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 26 -
Text der Seite - 26 -
26 1 TheFirstFewSteps
the legend command. In the plot command above, we first specify the plotting of
f_values and theng_values. In the legend command,t**2 should thus appear
beforee**t (as it does).
Multiple Plots in One Figure With the subplot command you may
combine several plots into one. We may demonstrate this with the script
two_plots_one_fig.py,whichreproducesFigs.1.2and1.3asone:
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(2, 1, 1) # 2 rows, 1 column, plot number 1
v0 = 5
g = 9.81
t = np.linspace(0, 1, 11)
y = v0*t - 0.5*g*t**2
plt.plot(t, y, ’*’)
plt.xlabel(’t (s)’)
plt.ylabel(’y (m)’)
plt.title(’Ball moving vertically’)
plt.subplot(2, 1, 2) # 2 rows, 1 column, plot number 2
t = np.linspace(-2, 2, 100)
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.tight_layout() # make subplots fit figure area
plt.show()
You observe thatsubplotappears in two places, first asplt.subplot(2, 1,
1), then as plt.subplot(2, 1, 2). This may be explained as follows. With a
code line like
plt.subplot(r, c, n)
we tell Python that in an arrangement of r by c subplots, r being the number of
rows and c being the number of columns, we address subplot numbern, counted
row-wise.So, intwo_plots_one_fig.py, whenwe first write
plt.subplot(2, 1, 1)
Pythonunderstandsthatwewant toplot insubplotnumber1inanarrangementwith
tworowsandonecolumnofsubplots.Furtherdown,Python interprets
plt.subplot(2, 1, 2)
and understands that plotting now is supposed to occur in subplot number 2 of the
samearrangement.
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