5
$\begingroup$

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?

$\endgroup$

1 Answer 1

6
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ 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 $\endgroup$
    – Alex
    Sep 22, 2013 at 12:26
  • $\begingroup$ @alex - I don't know how to do that... :( $\endgroup$
    – cormullion
    Sep 22, 2013 at 19:12
  • 2
    $\begingroup$ *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"] $\endgroup$
    – halmir
    Sep 23, 2013 at 13:33
  • $\begingroup$ @halmir That's the way - thanks! I'll update my answer with your solution. $\endgroup$
    – cormullion
    Sep 23, 2013 at 15:14
  • $\begingroup$ @halmir thanks for the answer $\endgroup$
    – Alex
    Sep 23, 2013 at 19:22

Your Answer

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

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