From: a boy on
points = RandomReal[{-1, 1}, {100, 2}]
Graphics[{Red, Line[points], Plot[Sin[x], {x, -10 Pi, 10 Pi}]},
Axes -> True]

The code doesn't work. how to draw them together? ImageCompose ?


From: David Park on
If you had the Presentations package your instincts would have been right
and you could write:

Needs["Presentations`Master`"]

points = RandomReal[{-1, 1}, {100, 2}];

Draw2D[
{Red, Line[points],
Black, Draw[Sin[x], {x, -10 Pi, 10 Pi}]},
AspectRatio -> .5,
Axes -> True]

Here Draw replaces Plot and everything is a graphics primitive. You could
also add other objects such as Text, ParametricDraw, ContourDraw or almost
any plot type.

With plain Mathematica you could instead use Graphics level jumping and
Show:

Show[
{Graphics[{Red, Line[points]}],
Plot[Sin[x], {x, -10 Pi, 10 Pi}]},
AspectRatio -> .5,
Axes -> True]

The Show statement picks up graphics options from the items in order and
this sometimes leads to confusion. In Presentations you don't need overall
graphics options in the Draw statements and there is cleaner behavior.

Or you could use Plot with an Epilog:

Plot[Sin[x], {x, -10 Pi, 10 Pi},
Epilog -> {Red, Line[points]}]

But here you couldn't add other plot types, such as a curve drawn with
ParametricPlot or ContourPlot.


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/





From: a boy [mailto:a.dozy.boy(a)gmail.com]

points = RandomReal[{-1, 1}, {100, 2}]
Graphics[{Red, Line[points], Plot[Sin[x], {x, -10 Pi, 10 Pi}]},
Axes -> True]

The code doesn't work. how to draw them together? ImageCompose ?




From: Murray Eisenberg on
Trying to do this kind of thing using Mathematica's built-in paradigm
for graphics causes trouble for many beginners.

Here's one way. (I changed the x-domain because otherwise the random
polygon collapses to a small blur.)

points = RandomReal[{-1,1},{100,2}];
Show[{
Plot[Sin[x],{x,-Pi,Pi}],
Graphics[{Red,Line[points]}]
}]

The Graphics has to apply only to the {Red,Line[points]}, as the result
of the Plot expression is already a Graphics object.

You don't need the Axes->True option, as that's the default for Plot.
However -- and this really drives folks nuts -- if you reverse the order
of the Graphics objects...

Show[{Graphics[{Red,Line[points]}], Plot[Sin[x], {x,-Pi,Pi}]}]

.... then the axes disappear and you have to insert the Axes->True option
within the Graphics[{Red,Line[points]}] expression:

Show[{Graphics[{Red, Line[points]}, Axes -> True],
Plot[Sin[x], {x, - Pi, Pi}]}]

No wonder this sort of thing gives so much trouble!

But a much simpler way to do the whole thing is to use the different
paradigm that's supplied by David Park's Presentations application:

Needs["Presentations`Master`"]

points = RandomReal[{-1,1},{100,2}];

Draw2D[{
Draw[Sin[x],{x,-Pi,Pi}],
{Red,Line[points]}
},
Axes->True]

Notice that the Axes->True option is for the entire Draw2D expression;
this means you'll get axes without any further ado no matter in what
order you list the two objects, Draw[Sin[x]....] and {Red,Line[points]}.

I've deliberately pretty-printed both versions in order to emphasize the
structure of the overall expression.

In the version done with Presentations, notice that all the different
objects to be drawn (by the Draw2D) are "at the same level", one after
the other, so that there's no need for wrapping the {Red,Line[points]
expression with Graphics.

With Presentations, moreover, you don't have to explicitly form pairs of
reals as coordinates of the points, but may instead form complex numbers
directly and plot a "complex line" whose vertices are the corresponding
complex points:

points = RandomComplex[{-1 - I, 1 + I}, 100];
Draw2D[{Draw[Sin[x],{x,-Pi,Pi}],Red,ComplexLine[pts]},Axes->True]


On 1/31/2010 7:53 AM, a boy wrote:
> points = RandomReal[{-1, 1}, {100, 2}]
> Graphics[{Red, Line[points], Plot[Sin[x], {x, -10 Pi, 10 Pi}]},
> Axes -> True]

--
Murray Eisenberg murray(a)math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

From: Helen Read on
On 1/31/2010 7:53 AM, a boy wrote:
> points = RandomReal[{-1, 1}, {100, 2}]
> Graphics[{Red, Line[points], Plot[Sin[x], {x, -10 Pi, 10 Pi}]},
> Axes -> True]
>
> The code doesn't work. how to draw them together? ImageCompose ?

Use Show to combine Plots and Graphics objects, like this.

points = RandomReal[{-1, 1}, {100, 2}];

plot1 = Plot[Sin[x], {x, -Pi, Pi}]

plot2= Graphics[{Red, Line[points]}]

Show[{plot1, plot2}]

Show will often benefit by setting an explicit PlotRange or use
PlotRange->Automatic

You can also add Graphics objects to a Plot with Epilog, like this.

Plot[Sin[x], {x, -Pi, Pi}, Epilog -> {Red, Line[points]}]



--
Helen Read
University of Vermont

From: Bill Rowe on
On 1/31/10 at 7:53 AM, a.dozy.boy(a)gmail.com (a boy) wrote:

>points = RandomReal[{-1, 1}, {100, 2}] Graphics[{Red, Line[points],
>Plot[Sin[x], {x, -10 Pi, 10 Pi}]},
>Axes -> True]

>The code doesn't work. how to draw them together? ImageCompose ?

Use Epilog. For example,

points = RandomReal[{-1, 1}, 100];

Plot[Sin[x], {x, -10 Pi, 10 Pi},
Epilog -> {Red,
Line[Transpose@{Rescale[Range[100], {1, 100}, {-10 Pi, 10 Pi}],
points}]}]

Notice, where things intersect, the color of the intersection is
red. Anything in the Epilog list over writes existing graphics.
If you want the plot to be dominant, you should replace Epilog
with Prolog. This can be handy for showing say the fit of a
curve to data. For example

curve = m x + b /. FindFit[Sort(a)points, m x + b, {m, b}, x];

Plot[curve, {x, 1, 100},
Prolog -> {GrayLevel[0.7], PointSize[.015],
Point[Transpose@{Range(a)100, Sort(a)points}]}, Axes -> None,
Frame -> True]