RSI 2008

The 2009 Research Science Institute



 RSI 2009 Home > Tech Help > Mathematica



Plotting simple graphs using Mathematica

To plot a function in Mathematica use the Plot command
Plot[ Sin[x],{x,-Pi,2*Pi}]
or define the function as either a variable g := Sin[x]
Plot[g,{x,-Pi,2*pi}]

or a function in Mathematica f[x_]:= Sin[x]
Plot[ f[x],{x,-Pi,2*Pi}]


If you wish to plot a data set of ordered pairs, enter the points into Mathematica as a list of ordered pairs of the form {x,y} and use the ListPlot command
data := {{1,1},{2,4},{3,9},{4,16},{5,25}}
ListPlot[ data]

If you would like to plot a line connecting consecutive pieces of data use the Joined option
ListPlot[ data,Joined->True]

To plot the data along with a function or functions, use the Show command.
Show[ ListPlot[data], Plot[ 4*x-3.5,{x,0,4}] ]

If your data are not recorded as ordered pairs you can combine them in Mathematica
hex := {1,2,3,4,5}
why := {1,4,9,16,25}
pointz := Table[ { hex[[i]],why[[i]] }, {i,1,5}]
ListPlot[pointz]


To export your graph to a PDF use the Export command
Export["amazinggraph.pdf",Show[ ListPlot[ data], Plot[ 4*x-3.5,{x,0,4}] ] ]
Mathematica will echo with     amazinggraph.pdf

To see what options are available with the commands, use the Options command
Options[Plot]
Options[ListPlot]

The help will also give a few more details.

To make a bar chart, you must include the Barchart package.
Needs["BarCharts`"]
data={1,2,5,3,-1,4}
BarChart[data]

The command Options[BarChart] will show you what options are available (color, opacity, PlotLabel, etc.)