Seite - 23 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 23 -
Text der Seite - 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.
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