1.6 Plotting, Printing and Input Data
23
The characteristics of a plotted line may also be changed in many ways with 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 the k with certain other letters. For
example, using b, you get a blue line, r gives a red line, while g makes the line
green. In addition, the line style may be changed, either alone, or together with a
color change. For example,
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 to avoid destroying a previously generated plot, you may precede your
plot command by
plt.figure()
This causes a new figure to be created alongside any already present.
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.1 s, rather than every millisecond. In
ball_plot.py, we would then have to change our call to linspace 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.1 s 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
different alternatives, e.g., a circle (the lower case letter o) or a star (*). Using a star,
for example, the plot command could read
plt.plot(t, y, ’*’)
# default color, points marked with *
With these changes, the plot from Fig. 1.1 would change as seen in Fig. 1.2.
Of course, not only can we choose between different kinds of point markers, but
also their color may be specified. Some examples are:
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 points with point markers and those markers overlap
in the plot, the points will not appear as points, but rather as a very thick line. This
is hardly what you want.
23
The characteristics of a plotted line may also be changed in many ways with 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 the k with certain other letters. For
example, using b, you get a blue line, r gives a red line, while g makes the line
green. In addition, the line style may be changed, either alone, or together with a
color change. For example,
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 to avoid destroying a previously generated plot, you may precede your
plot command by
plt.figure()
This causes a new figure to be created alongside any already present.
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.1 s, rather than every millisecond. In
ball_plot.py, we would then have to change our call to linspace 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.1 s 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
different alternatives, e.g., a circle (the lower case letter o) or a star (*). Using a star,
for example, the plot command could read
plt.plot(t, y, ’*’)
# default color, points marked with *
With these changes, the plot from Fig. 1.1 would change as seen in Fig. 1.2.
Of course, not only can we choose between different kinds of point markers, but
also their color may be specified. Some examples are:
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 points with point markers and those markers overlap
in the plot, the points will not appear as points, but rather as a very thick line. This
is hardly what you want.
