Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot a function and data in Mathematica?

Simple question but I can't find the answer.

I want to combine a ListLinePlot and a regular Plot (of a function) onto one plot. How do I do this?

Thanks.

like image 377
Nick Avatar asked Sep 26 '11 06:09

Nick


People also ask

How do you plot a function?

Graphing A Function Rule To graph a function, you have to select x-values and plug them into the equation. Once you plug those values into the equation, you will get a y-value. Your x-values and your y-values make up your coordinates for a single point.

How do you graph a function with two variables?

Two common ways of representing the graph of a function of two variables are the surface plot and the contour plot. The first is simply a representation of the graph in three-dimensional space. The second, draws the level curves f(x,y)=C for several values of C in the x,y -plane.


2 Answers

Use Show, e.g.

Show[Plot[x^2, {x, 0, 3.5}], ListPlot[{1, 4, 9}]]

Show output

Note, if plot options conflict Show uses the first plot's option, unless the option is specified in Show. I.e.

Show[Plot[x^2, {x, 0, 3.5}, ImageSize -> 100], 
 ListPlot[{1, 4, 9}, ImageSize -> 400]]

shows a combined plot of size 100.

Show[Plot[x^2, {x, 0, 3.5}, ImageSize -> 100], 
 ListPlot[{1, 4, 9}, ImageSize -> 400], ImageSize -> 300]

Shows a combined plot of size 300.

like image 110
Chris Degnen Avatar answered Oct 19 '22 08:10

Chris Degnen


An alternative to using Show and combining two separate plots, is to use Epilog to add the data points to the main plot. For example:

data = Table[{i, Sin[i] + .1 RandomReal[]}, {i, 0, 10, .5}];
Plot[Sin[x], {x, 0, 10}, Epilog -> Point[data], PlotRange -> All]

or

Plot[Sin[x], {x, 0, 10}, Epilog -> Line[data], PlotRange -> All]
like image 35
Simon Avatar answered Oct 19 '22 08:10

Simon