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}];

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}]];



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?


2 Answers 2


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


You can also use the built-in ChartElementFunction "GradientBubble" as a VertexShapeFunction as follows:

vShapeF[cs_: "Rainbow", gd_: "Radial"] := ChartElementDataFunction["GradientBubble", 
  "ColorScheme" -> cs, "GradientDirection" -> gd][
   Transpose[Function[t, # + t #3/2] /@ {-1, 1}], {1}] &;

Using gr, sizes and cols from J.M.'s answer:

Graph[gr, EdgeStyle -> Directive[Opacity[1], Thick, Gray], 
 VertexShapeFunction -> {v_ :>  vShapeF[Lighter[cols[[v]], #] & /@ Subdivide[4]], 
   1 -> vShapeF["CoffeeTones"]}, 
 VertexSize -> {v_ :> sizes[[v]]}]

enter image description here


VertexShapeFunction -> {v_ :> 
   vShapeF[Opacity[1 - #, cols[[v]]] & /@ Rest[Subdivide[5]]]}

to get

enter image description here

Use the optional second argument to change the direction of the gradient:

Graph[gr, EdgeStyle -> Directive[Opacity[1], Thick, Gray], 
 VertexShapeFunction -> {v_ :> 
    vShapeF[Lighter[cols[[v]], #] & /@ Subdivide[4], "DescendingRadial"], 
   1 -> vShapeF["CoffeeTones", "DescendingRadial"]}, 
 VertexSize -> {v_ :> sizes[[v]]}]

enter image description here


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.