1

My problem is the following.

The aim

Plotting a set of 2D points on the plane having a function used to give intensity for every point.

fun(x,y):=x+y

Plus, I have a graph connecting these points. I need to display the graph on the density plot, this is definitely necessary.

The problem

Got no clue how to do it. I searched a little the Mathematica documentation but I could not find much.

Some notes

Whenever someone finds a solution to this, I have also a question. How is it piossible to use the graph functionality on the density plot diagram? For example, if I wanted to show the labels on vertices, is it possible to have some sort of parametrized solution. Maybe I am requiring too much, this is only a little note, skip it if it takes too much time.

Final notes

I am requiring a 2D graph. Not a 3D one. Just a 2D is fine.

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
Andry
  • 16,172
  • 27
  • 138
  • 246

2 Answers2

5

Graph has an option VertexCoordinates with which you can specify the coordinates of the vertices, so you could plot a ListDensityPlot and a Graph on top of each other. For example, suppose your data is something like

f[x_, y_] := x + y
pts = RandomReal[1, {40, 2}];  (* xy coordinates *)
edges = Flatten[Table[{i -> Position[pts, #][[1, 1]]} & /@ 
   Rest[Nearest[pts, pts[[i]], 4]], {i, Length[pts]}]];
edges = Union[edges, SameTest -> (SameQ[#1, #2] || SameQ[#1, Reverse[#2]] &)];

Then you could do something like

densPlot = ListDensityPlot[{##, f[##]} & @@@ pts];
graph = Graph[Range[Length[pts]], edges,
   VertexCoordinates -> pts, 
   VertexShapeFunction -> "Square", 
   VertexSize -> 1.5, VertexLabels -> "Name", 
   EdgeStyle -> Directive[Opacity[1], White]];

Show[densPlot, graph]

Mathematica graphics

Heike
  • 24,102
  • 2
  • 31
  • 45
  • 1
    @Andry You're welcome. By the way, there is a new Q&A site for Mathematica at http://mathematica.stackexchange.com which is where most of the mathematica crowd from stackoverflow hang out nowadays, so you could consider asking your questions there in future. – Heike Feb 07 '12 at 10:05
4

You probably want something like this. I am creating some random dummy data for the points and edges.

fun[x_, y_] := x + y;

points = RandomInteger[{0, 15}, {10, 2}];
edges = RandomChoice[points, {30, 2}];

Show[
  ListDensityPlot[{##, fun[##]} & @@@ points],
  Graphics[{PointSize[0.02], Point@points, Arrow /@ edges}]
]

Mathematica graphics

If your edges are in the form of Rules, you can convert them to list pairs with List @@@ edges.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • Mr.Wizard thankyou for your answer. You answered first but I gave the correct answer to Heike `cause I think its code shows more options I did not know. However good answer thank you very much. – Andry Feb 06 '12 at 23:49
  • @Andry you're welcome, and no problem; I agree here answer is more complete. I use version 7 an don't have the `Graph` function. – Mr.Wizard Feb 07 '12 at 03:41