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 - 23 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 23 -

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

Text of the Page - 23 -

1.6 Plotting,PrintingandInputData 23 Thecharacteristicsofaplotted linemayalsobechangedinmanywayswith just minor modifications of the plot command. For example, a black line is achieved with plt.plot(t, y, ’k’) # k - black, b - blue, r - red, g - green, ... Other colors could be achieved by exchanging thekwith certain other letters. For example, using b, you get a blue line, r gives a red line, while gmakes the line green. In addition, the line style may be changed, either alone, or together with a colorchange.Forexample, plt.plot(t, y, ’--’) # default color, dashed line plt.plot(t, y, ’r--’) # red and dashed line plt.plot(t, y, ’g:’) # green and dotted line Note that toavoiddestroyingapreviouslygeneratedplot,youmayprecedeyour plotcommandby plt.figure() Thiscausesa newfigure to becreatedalongsideanyalreadypresent. Plotting Points Only When there are not too many data points, it is sometimes desirable to plot each data point as a β€œpoint”, rather than representing all the data points with a line. To illustrate, we may consider our case with the ball again, but this time computing the height each 0.1s, rather than every millisecond. In ball_plot.py,we would thenhave to changeourcall tolinspace into t = np.linspace(0, 1, 11) # 11 values give 10 intervals of 0.1 Note that we need to give 11 as the final argument here, since there will be 10 intervals of 0.1s when 11 equally distributed values on [0,1] are asked for. In addition, we would have to change the plot command to specify the plotting of data points as β€œpoints”. To mark the points themselves, we may use one of many differentalternatives,e.g.,acircle (the lowercase lettero)orastar (*).Usingastar, forexample, theplotcommandcould read plt.plot(t, y, ’*’) # default color, points marked with * With thesechanges, the plot fromFig.1.1wouldchangeas seen inFig.1.2. Ofcourse,notonlycanwechoosebetweendifferentkindsofpointmarkers,but also their colormaybespecified.Someexamplesare: plt.plot(t, y, ’r*’) # points marked with * in red plt.plot(t, y, ’bo’) # points marked with o in blue plt.plot(t, y, ’g+’) # points marked with + in green When are the data points β€œtoo many” for plotting data points as points (and not as a line)? If plotting the data pointswith point markersand those markersoverlap in the plot, the pointswill not appear as points, but rather as a very thick line. This ishardlywhatyouwant.
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