2
$\begingroup$

Hello Mathematica stackexchange community! I need your help on a research project. I'm looking to create a graph g with vertices in specifies places and then use an image I have as the background of the graph. However I want the vertices to be positioned relative to the image background.

Let me use the code from another question here:

"How to impose custom style to the edges of a graph" asked on Feb.29.1012 by PlatoManiac. Link to the question here: How to impose custom style to the edges of a graph Let's use the graph that he generates:

node = {11, 12, 13, 14, 15, 16};
edges = {11 -> 14, 11 -> 16, 12 -> 16, 12 -> 15, 13 -> 15, 13 -> 16, 
     14 -> 16, 14 -> 15, 15 -> 16, 15 -> 13, 16 -> 15, 16 -> 14};
vertexposition = {{6.51493919050084`, 44.04756585632944`}, 
{75.59445680043342`, 50.47455242214042`}, 
{87.32825501506514`, 13.395648943951699`}, 
{28.795707353492418`, 3.420138063734413`}, 
{60.729164933330765`, 16.915777496473908`}, 
{51.85158892659126`, 25.803692768150313`}};

Show[
Graph[node, edges, VertexCoordinates -> vertexposition, 
      VertexSize -> {"Scaled", .02},VertexLabels -> "Name"],
Frame -> True, FrameTicks -> True, ImageSize -> 600]

My question is then...

How can I make the background of this graph be the image that I want, the federal district of Brazil (linked to an image of it here:http://www.wolframalpha.com/share/clip?f=d41d8cd98f00b204e9800998ecf8427e53vfptvv3q) However I want to import my own image and not necessarily call it from the wolfram server, just using it now for the example.

But be able to reduce the size of the image such that the vertices change relative to the background image. My first thought for this is that I need to make the vertices be a percentage instead of an exact coordinate. The second part of the question is how can I designate the precise location for certain nodes and not others. For example If I make two nodes (two cities) and they are connected by a water pump in-between the cities as shown here: http://www.wolframalpha.com/share/clip?f=d41d8cd98f00b204e9800998ecf8427eetdfek9rli I only want a fixed coordinate for the two cities (acting as A & C) but don't care where the "B" node (or nodes) ends up lining up (if it changes anything, it will be a directed & weighted graph).

I can't post images yet so I created them in wolfram alpha and put their links.

$\endgroup$

1 Answer 1

2
$\begingroup$

EDITED

I believe the problem you describe occurs when you change coordinates values for a given edge, what causes the whole Graph to be automatically adjusted to "best fit" on the screen. You can avoid that by using AxesOrigin->{0,0}, Axes->True, AxesStyle->Transparent together.

So, in order to get the best result you have to, at the first step, convert the Wolframalpha RawBoxes to an actual image:

img = WolframAlpha["Distrito Federal", {{"WolframMap", 1}, "Content"}];
Export["temp.jpg", img];
img2 = Import["temp.jpg"];

Now you can try this:

Manipulate[
  node = {11, 12, 13, 14, 15, 16};
  edges = {11 -> 14, 11 -> 16, 12 -> 16, 12 -> 15, 13 -> 15, 13 -> 16, 
    14 -> 16, 14 -> 15, 15 -> 16, 15 -> 13, 16 -> 15, 16 -> 14};
  Overlay[{Show[img2, ImageSize -> {500, 250}, ImagePadding -> 0], 
    Graph[node, edges, 
      VertexCoordinates -> {{x, y}, {75.59445680043342`, 
      50.47455242214042`}, {87.32825501506514`, 
      13.395648943951699`}, {28.795707353492418`, 
      3.420138063734413`}, {60.729164933330765`, 
      16.915777496473908`}, {51.85158892659126`, 
      25.803692768150313`}}, VertexSize -> {"Scaled", .02}, 
      VertexLabels -> "Name", ImageSize -> {400, 250}, 
      AxesOrigin -> {0, 0}, Axes -> True, 
      AxesStyle -> Transparent]}], {{x, 1}, 1, 80}, {{y, 1}, 1, 50}]

enter image description here

+++++++++++++++++++++++++++++++++

You could do something like this:

img = WolframAlpha["Distrito Federal", {{"WolframMap", 1}, "Content"}]

enter image description here

You can now input your data:

node = {11, 12, 13, 14, 15, 16};
edges = {11 -> 14, 11 -> 16, 12 -> 16, 12 -> 15, 13 -> 15, 13 -> 16, 
 14 -> 16, 14 -> 15, 15 -> 16, 15 -> 13, 16 -> 15, 16 -> 14};
vertexposition = {{6.51493919050084`, 44.04756585632944`}, 
{75.59445680043342`, 50.47455242214042`}, 
{87.32825501506514`, 13.395648943951699`}, 
{28.795707353492418`, 3.420138063734413`}, 
{60.729164933330765`, 16.915777496473908`}, 
{51.85158892659126`, 25.803692768150313`}};

Finally, show both images:

Overlay[{img, 
Graph[node, edges, VertexCoordinates -> vertexposition, 
VertexSize -> {"Scaled", .02}, VertexLabels -> "Name", 
ImageSize -> 400]}]

Result:

enter image description here

$\endgroup$
10
  • $\begingroup$ Yes like this, Thanks. However can I link the frames of both image and graph so that when I change imagesize the map expands proportionally too? $\endgroup$
    – E3labs
    Jul 2, 2013 at 21:37
  • $\begingroup$ @user8351 You can export the image, and then import it and resize: Export["temp.jpg", %] and then ImageResize[Import["temp.jpg"], 1000]. $\endgroup$
    – Rod
    Jul 2, 2013 at 21:44
  • $\begingroup$ Thanks I'm doing that once it's done and ready for "final" export. What I meant to say in my last comment was.. How do I link the frames so that when I change the GRAPH the image re-sizes proportionally. For example try changing the coordinates of node 11 by half and you'll see what I mean. $\endgroup$
    – E3labs
    Jul 2, 2013 at 21:56
  • $\begingroup$ Can you explain the last bit of the code: {{x, 1}, 1, 80}, {{y, 1}, 1, 50}] The extra "]" is throwing my off, and when I manage to run it the graph won't show up (it might be behind the image) $\endgroup$
    – E3labs
    Jul 3, 2013 at 15:07
  • $\begingroup$ @user8351 They are the defined intervals for the variables x and y, inside the Manipulate[] function. To better understand, you can use {{x, 1}, 1, 80, Appearance -> "Labeled"}, {{y, 1}, 1, 50, Appearance -> "Labeled"}. $\endgroup$
    – Rod
    Jul 3, 2013 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.