Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am using following codes to create a graph.

 cities = CityData[{All, #}] & /@ {"Germany"}; bigcities = 
 Select[Select[Join @@ cities, FreeQ[#, ""] &], 
 CityData[#, "Population"] > 100000 &];
 cityCoords = 
 Select[CityData[#, "Coordinates"] & /@ bigcities, FreeQ[#, ""] &];
 Needs["ComputationalGeometry`"];
 pts = Reverse[cityCoords, 2];
 dt = DelaunayTriangulation[pts];
 toPairs[{m_, ns_List}] := Map[{m, #} &, ns];
 edges = Union[Sort /@ Flatten[toPairs /@ dt, 1]];
 Graph[edges, ImagePadding -> 20, GraphLayout -> "PlanarEmbedding", 
 VertexCoordinates -> pts, VertexStyle -> LightBlue, 
 VertexLabels -> bigcities[[All, 1]], 
 Prolog -> {LightBrown, EdgeForm[{Thick, Black}], 
 CountryData["Germany", "FullPolygon"]}, ImageSize -> 800]

But the problem is it doesn't change the labeling of vertexes into the name of cities.How can I solve it?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Try this inside your Graph for a quick fix:

VertexLabels -> Table[i -> First@bigcities[[i]], {i, 1, Length[bigcities]}]

germany

VertexLabels doesn't appear to like a simple list, judging from the documentation:

documentation for VertexLabels

Following halmir's excellent solution in the comments, you can do this:

rules = Table[i -> First@bigcities[[i]], {i, 1, Length[bigcities]}]

    {1 -> "Berlin", 2 -> "Hamburg", 3 -> "Munich", 4 -> "Cologne", 
     5 -> "Frankfurt", 6 -> "Stuttgart", 7 -> "Dortmund", 8 -> "Essen", 
     9 -> "Dusseldorf", 10 -> "Bremen", 11 -> "Hanover", 12 -> "Leipzig", 
     13 -> "Dresden", 14 -> "Nuremberg", 15 -> "Duisburg", 16 -> "Bochum",
     17 -> "Wuppertal", ...
Graph[
 rules[[All, 2]], (UndirectedEdge @@@ edges) /. rules,
 VertexLabels -> "Name",
 VertexCoordinates -> pts,
 VertexStyle -> LightBlue,
 Prolog -> {LightBrown, EdgeForm[{Thick, Black}],
   CountryData["Germany", "FullPolygon"]},
 ImageSize -> 800]

using the second syntax of Graph:

graph second syntax

share|improve this answer
    
many thanks this is my answer.Could you please tell me if there is anyway to change the VertexList of the current Graph to related city name here that you labeled?You know the VertexList is still that numbers.I know this is different question –  Alex Sep 22 '13 at 12:26
    
@alex - I don't know how to do that... :( –  cormullion Sep 22 '13 at 19:12
2  
*alex - you can just use city names as vertices. For example: rules = Table[i -> First@bigcities[[i]], {i, 1, Length[bigcities]}]; Graph[rules[[All, 2]], (UndirectedEdge @@@ edges) /. rules, VertexLabels -> "Name"] –  halmir Sep 23 '13 at 13:33
    
@halmir That's the way - thanks! I'll update my answer with your solution. –  cormullion Sep 23 '13 at 15:14
    
@halmir thanks for the answer –  Alex Sep 23 '13 at 19:22
add comment

Your Answer

 
discard

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.