0
$\begingroup$

I would like to put a radial gradient effect on the nodes of a network. As far as I can tell Mathematica does not support radial gradient fill effects for graphics primitives. One way to get the radial gradient effect is with concentric circles, and a few other alternatives that I've found around the net. I made my own version using a triangular polygon that is rotated and it tends to give smoother gradients at the expense of ragged edges, but those are transparent for these applications so it works nicely.

Unfortunately, I can't get the graphics object to scale at all, and it's supposed to scale by the radius property. Also, the only way I could figure out how to get the nodes to be the shapes is to set the VertexShapeFunction for each node separately using Properties. In the sample below I've only done this for the second node, but I could build a table of these if that's the only way to set it.

TheGraph=RandomGraph[{5, 8}];
TheNodeColors={Red,Green,Blue,Purple,Orange};
TheNodeSizes={1,2,3,4,5};

GradientFillNode[theColor_:grey,theRadius_:1,NumberOfSlices_:40]:=Graphics[Table[Polygon[{{0,0},{theRadius*Cos[2Pi t/NumberOfSlices],theRadius*Sin[2Pi t/NumberOfSlices]},{theRadius*Cos[2Pi (t+1)/NumberOfSlices],theRadius*Sin[2Pi (t+1)/NumberOfSlices]}},VertexColors->{theColor,Opacity[0,theColor],Opacity[0,theColor]}],{t,NumberOfSlices}]];

NodeImages=Table[GradientFillNode[TheNodeColors[[i]],TheNodeSizes[[i]],40],{i,Length[TheNodeColors]}]

ColoredGraph=Graph[TheGraph,VertexShapeFunction->(Inset[NodeImages[[1]],#]&),ImageSize->500,Properties->{2->{VertexShapeFunction->(Inset[NodeImages[[2]],#]&)}}]

As you can see if you run it, this does change the color of the second node, but the sizes are all the same.

(1) How do I get the nodes to size according to my setting?

(2) Is there a better way to use the VertexShapeFunction to have it color and size the nodes properly?

(3) Is there an entirely different and better way to achieve nodes with a radial color gradient?

$\endgroup$
1
$\begingroup$

A very rough attempt:

gradDisk[cent_?VectorQ, rad_?NumericQ, col_?ColorQ, n_Integer: 30] :=
    {Texture[RadialGradientImage[{Append[Darker[col], 1], Append[col, 1/2], 
                                  Append[Lighter[col], 0]}]], 
     Polygon[N[CirclePoints[cent, rad, n]], 
             VertexTextureCoordinates -> N[CirclePoints[{1, 1}/2, 1/2, n]]]}

BlockRandom[SeedRandom["somegraph"]; (* for reproducibility *)
            gr = RandomGraph[{5, 8}]; 
            sizes = RandomSample[Range[5]/5]; 
            cols = RandomSample[{Red, Green, Blue, Purple, Orange}];]

Graph[gr, EdgeStyle -> Directive[Opacity[1], Thick, Gray], 
      VertexShapeFunction -> 
      Thread[Range[5] -> Map[Function[c, gradDisk[#1, Mean[#3]/2, c] &], cols]], 
      VertexSize -> Thread[Range[5] -> sizes], VertexStyle -> EdgeForm[]]

graph with radial gradient nodes

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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