Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been very busy these days with giving the VertexCoordinates of the cities the geographic coordinates from CityData["CityName","Coordinates"] of the cities. Could somebody please help me on writing algorithm?

Graph[{"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", 
  "UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby", 
  "UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby", 
  "Stockholm" -> "Boo", "Boo" -> "Stockholm", 
  "Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm", 
  "Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm", 
  "Stockholm" -> "Taby", "Taby" -> "Stockholm"}, 
 VertexLabels -> "Name"]
share|improve this question
Which Graph[]? Can you maybe show what you've already done in Mathematica? – J. M. Apr 21 '13 at 11:37
please see result – Alex Apr 21 '13 at 12:32
Are you saying that you would like to plot the graph laid out in such a way that the vertices are placed according to their geographic location? – Mark McClure Apr 21 '13 at 12:56
Exactly!!My graph is just simple here but yes my problem is how to locate vertices on their geographical coordinates and second step to show it in the map.Not just a Plot but the graph itself!!!When I want to show the flow on the vertices. – Alex Apr 21 '13 at 13:09
Try ref/VertexCoordinates and ref/CityData in the help? – chris Apr 21 '13 at 14:14
up vote 4 down vote accepted

You could extract coordinates using CityData and set it by SetProperty.

g = Graph[{"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", 
"UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby", 
"UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby", 
"Stockholm" -> "Boo", "Boo" -> "Stockholm", 
"Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm", 
"Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm", 
"Stockholm" -> "Taby", "Taby" -> "Stockholm"}, 
  VertexLabels -> "Name", ImagePadding -> 40];

coords = CityData[#, "Coordinates"] & /@ VertexList[g];

SetProperty[g, {VertexCoordinates -> Reverse[coords, 2], 
  Prolog -> {Gray, CountryData["Sweden", "Polygon"]}}]

enter image description here

If SetProperty doesn't work (v9.0.0)

cities = {"Uppsala", "Marsta", "UpplandsVasby", "Sollentuna", 
"Stockholm", "Boo", "Lidingo", "Taby"};

coords = CityData[#, "Coordinates"] & /@ cities;

g = Graph[
cities, {"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", 
 "UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby", 
 "UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby", 
 "Stockholm" -> "Boo", "Boo" -> "Stockholm", 
 "Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm", 
 "Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm", 
 "Stockholm" -> "Taby", "Taby" -> "Stockholm"}, 
   VertexCoordinates -> Reverse[coords, 2], 
   Prolog -> {Gray, CountryData["Sweden", "Polygon"]}, 
   VertexLabels -> "Name", ImagePadding -> 40]
share|improve this answer
thats great !!But when i am evaluating your proposed codes, it just gives me the simple Graph that I had!!Without Sweden Map and coordinates!!What could be wrong?Do you have output in the Graph? – Alex Apr 21 '13 at 15:27
Which version of Mathematica are you using? In v 9.0.1 it works fine. If it doesn't work, you can just add option when you construct graph or reconstruct graph like: Graph[VertexList[g], EdgeList[g], VertexCoordinates->.., Prolog->...] – halmir Apr 21 '13 at 16:06
black means "it's defined", so nothing wrong with it. did you try to build graph with coords? I added that version.. – halmir Apr 21 '13 at 18:24

I tend to use GraphPlot[] instead if I need to depict the graph with other primitives, so here's my take:

cityGraph = {"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", "UpplandsVasby" -> "Sollentuna",
             "Sollentuna" -> "UpplandsVasby", "UpplandsVasby" -> "Marsta",
             "Marsta" -> "UpplandsVasby", "Stockholm" -> "Boo", "Boo" -> "Stockholm",
             "Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm",
             "Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm",
             "Stockholm" -> "Taby", "Taby" -> "Stockholm"}

GraphPlot[cityGraph, Background -> ColorData["Legacy", "PowderBlue"], 
          EdgeRenderingFunction -> ({Blue, Line[#]} &), MultiedgeStyle -> 1/20,
          PlotRange -> {{17, 19}, {58, 60}}, 
          Prolog -> {Gray, CountryData["Sweden", {"FullPolygon", "Equirectangular"}]}, 
          VertexCoordinateRules ->
          Map[# -> Reverse[CityData[{#, "Sweden"}, "Coordinates"]] &,
              VertexList[Graph[cityGraph]]], VertexLabeling -> Tooltip, 
          VertexRenderingFunction -> ({Directive[AbsolutePointSize[4], Red],
                                       Tooltip[Point[#1], #2]} &)]

Swedish city graph

(If executed in Mathematica, there should be tooltips associated with each vertex.)

share|improve this answer
I suppose it ought to be explicitly noted that the coordinates in the polygon returned by CountryData[] are in (long, lat) format, while CityData[] coordinates return in (lat, long) format. – J. M. Apr 21 '13 at 15:07
Your coordinates comment about CountryData and CityData and their differences is really amazing !!!Many Thanks!!!Thats very helpful to know that!!But about your suggestion to use GraphPlot the point is I want to show the optimized flow in the my Graph then better to use new version 9 availabilities.I wonder by using GraphPlot I can use "FlowGraph" capabilities?! – Alex Apr 21 '13 at 15:54
Can you maybe edit your question to show the fancy Graph[] with flows that you want to be embedded into a map? – J. M. Apr 21 '13 at 16:06
Many thanks for the suggestion .I will definitely do that But on that way I have to explain many other things not related to issue.After developing whole GraphNetwork I will upload it as sample later. – Alex Apr 21 '13 at 16:45
It doesn't have to be the entire network. You've already supplied a minimal example; now, how would you put the flows into this small graph you've presented? – J. M. Apr 21 '13 at 16:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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