Plotting

This notebook introduces methods for producing plots of data with and without uncertainties and for drawing curves to represent a function.

1. Data Plots

In this laboratory you will learn how to make data plots and graphs of curves. The first step is to load the `Graphics' package into Mathematica. This should be done first and only once during the session.

Needs["ErrorBarPlots`"];

To make a data plot first create an array with your results in it as ordered pairs of x and y values. Next, use the ListPlot command to draw the graph.

data ={{0,3.21},{1,5.42},{2,8.60},{3,11.07},{4,13.70},{5,16.21},{6,19.42},{7,21.61},{8,24.09},{9,27.59},{10,29.86}};
plot1 = ListPlot[data]

To make a prettier plot, try the following commands.

plot2=ListPlot[data,Joined->False,Frame->True,FrameLabel->{"x","y","Sample Plot",""},PlotStyle->{PointSize[0.05`],RGBColor[1,0.5`,0.2`]}]

And you can even connect the dots.

plot2=ListPlot[data,Joined->True,Frame->True,FrameLabel->{"x","y","Sample Plot",""},PlotStyle->{Thickness[0.02`],RGBColor[1,0.5`,0.2`]}]

2. Data Plots with Uncertainties

We will often deal with data which has uncertainties associated with each data point. To make a plot of such data add the uncertainty in the y value to each element in the array as shown below. Compare the arrays data and data2.

data2 = {{{0,3.21},ErrorBar[1.79]},{{1,5.42},ErrorBar[2.32]},{{2,8.6},ErrorBar[2.93]},{{3,11.07},ErrorBar[3.327]},{{4,13.70},ErrorBar[3.70]},{{5,16.21},ErrorBar[4.02]},{{6,19.42},ErrorBar[4.40]},{{7,21.61},ErrorBar[4.64]},{{8,24.09},ErrorBar[4.90]},{{9,27.59},ErrorBar[5.25]},{{10,29.86},ErrorBar[5.46]}}

Now use the ErrorListPlot command to make the plot.

plot3 = ErrorListPlot[data2]

3. Function Plots

There are two general ways to plot a function. The first is to make an array of x and y values and use ListPlot to connect the dots. See the first example below. The second way involves plotting the function directly with the Plot command. See the second example below.

Recall the height of a projectile y as a function of time t is

                                                      y(t) = -(1/2)g(t^2) + (Subscript[v, iy])t + (Subscript[y, i])

where g is the acceleration of gravity, (Subscript[v, iy]) is the initial speed in the vertical direction, and (Subscript[y, i]) is the height from which the ball is thrown. We want to make a plot of this function when (Subscript[v, iy])=3.0 m/s and (Subscript[y, i]) is 10.0 m. Execute the code and investigate the functions of the different elements of the code. Note especially how functions are defined and how comments are written into the code.

viy=9.0;
yi=1.0;
grav=9.8;
y[t_]:=-0.50*grav*(t^2)+viy*t+yi;
myfunc=Table[{t,y[t]},{t,0,2,0.2`}];ListPlot[myfunc,Joined->True,Frame->True,FrameLabel->{"t(s)","y(t)","",""}]

Now make the same plot directly with the Plot command using the function that was defined above.

Plot[y[t], {t,0,2}]

Make a fancier plot.

Plot[y[t],{t,0,2},
Frame->True,
FrameLabel->{"t(s)","y(t)","",""}]