Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 26 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 26 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 26 -

Image of the Page - 26 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python