Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: plotting weighted graphs

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55666] Re: plotting weighted graphs
  • From: yifanhu at wolfram.com
  • Date: Fri, 1 Apr 2005 05:37:28 -0500 (EST)
  • References: <NDBBJGNHKLMPLILOIPPOCELMEGAA.djmp@earthlink.net> <d1gulc$t0$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

I think this is what you want. I assume you
are plotting undirected graph since you matrix is symmetric.
For undirected graph, you can replace Line with Arrow and
plot both a[[i,j]] and a[[j,i]] on an edge, placed so that
they do not clash.

The following code set font size to 20, plot node1, node2 ... on each
vertex,
plot the matrix entry value on each edge at the center, and color
the edges using entry values.

H = {{0, 2, 1}, {2, 0, 4}, {1, 4, 0}};

text = {node1, node2, node3};
x = GraphCoordinates[H];
max = Max[H];

GraphPlot[H, VertexCoordinates -> x, TextStyle -> {FontSize -> 20},
  VertexStyleFunction -> (Text[text[[#]], #] &),
  EdgeStyleFunction -> ({If[#1 > #2, {Hue[0.75*(1 - H[[#1, #2]]/max)],
              Line[{#1, #2}],
              Text[H[[#1, #2]], 0.5*(x[[#1]] + x[[#2]])]}, {}]} &)]

This make the nodes look a bit nicer

GraphPlot[H, VertexCoordinates -> x, TextStyle -> {FontSize -> 20},
  VertexStyleFunction -> ({White, Disk[#, 0.4], Black, Circle[#, 0.4],
          Text[text[[#]], #]} &),
  EdgeStyleFunction -> ({If[#1 > #2, {Hue[0.75*(1 - H[[#1, #2]]/max)],
              Line[{#1, #2}],
              Text[H[[#1, #2]], 0.5*(x[[#1]] + x[[#2]])]}, {}]} &)]



Yifan Hu
Wolfram Research


-------------

Erik Itter wrote:
> Thx,
>
> well... its quite simple. I have (e.g., of course the matrices I want
to diplay are larger):
>
>
> << DiscreteMath`GraphPlot`;
>
> H = {{0, 2, 1}, {2, 0, 4} {1, 4, 0}}
>
> GraphPlot[H, "EdgeStyleFunction" -> Automatic,
> "VertexStyleFunction" -> Automatic]
>
>
>
> ...which plots the vertices labeled using very tiny numbers and
displays the weights using different
> colors. I need the vertices numbered readable (greater fontsize) and
the weights displayed at the
> edges instead or in addition of/to the the coloring. From the help
browser I cannot deduce how to
> specify such "EdgeStyleFunction" and "VertexStyleFunction". I would
also be delighted if someone
> pointed out where the documentation about writing such kinds of
functions can be found (web or
> HelpBrowser, or Mathematica-Book [which hopefully arrives tomorrow by
mail ;) ])
>
> And if the question is simply to stupid perhaps someone could point
me to the mathematica for
> dummies list/tutorial/something...
> 
>


  • Prev by Date: Re: Replacing an expression by an expression
  • Next by Date: Re: Replacing an expression by an expression
  • Previous by thread: Fwd: Replacing an expression by an expression
  • Next by thread: Re: Much faster ConvexHull implementation