Mathematica中的颜色编码图

时间:2017-06-16 20:14:40

标签: graph colors wolfram-mathematica

这是我的示例代码

Morb = 3;
NPar = 5;
Sols = Solve [
   Append[Array[n[#] >= 0 &, Morb], Array[n, Morb, 1, Plus] == NPar], 
   Integers];
CIElements = Array[n, Morb] /. Sols;

OpOB[ij_, Ind1_] := (
  If[Part[ Ind1, Part[ij, 2]] != 0,
   Ind2 = Ind1;


   Part[Ind2, Part[ij, 1]] = Part[Ind1, Part[ij, 1]] + 1; 
   Part[Ind2, Part[ij, 2]] = Part[Ind1, Part[ij, 2]] - 1;
   , Ind2 = 0 ];
  Return[Ind2]
  )
GenerateEdge[ij_, Ind1_] := Ind1 \[DirectedEdge] OpOB[ij, Ind1]


OpSol = Solve[{i < j, i > 0, i <= Morb, j > 0, j <= Morb}, {i, j}, 
   Integers];
OpLabels = {i, j} /. OpSol;
MapList = {};
Do[  
 If[Length[OpOB[ii, jj]] != 0,
  AppendTo[ MapList, GenerateEdge[ ii, jj] ],
  Unevaluated[Sequence[]]],
 {ii, OpLabels}, {jj, CIElements}]
Graph[MapList]

我生成一个名为MapList的边列表,它可以很好地绘制图形。但是,我想根据OpLabels的哪个元素生成边缘来对图形的边缘进行颜色编码。我可以轻松地修改我的Do[ ]子句以包含一些标签,以便稍后将其解释为颜色。但是,我遇到过其他解决方案,例如

https://mathematica.stackexchange.com/questions/17658/how-can-i-display-a-multigraph-with-different-colored-edges

明确列出不同颜色的数量。这里的颜色数量取决于Morb的值,所以我可以提前指定。有没有什么方法可以简单地用数字标记每个边缘,然后根据某些预定义的调色板按颜色选择颜色?

1 个答案:

答案 0 :(得分:2)

我认为你的意思是每个ij都是不同的颜色..

GenerateEdge[ij_, Ind1_] := 
 Style[ Ind1 \[DirectedEdge] OpOB[ij, Ind1] , color[ij] ]

函数color的定义如下:

ncolors = 0
Clear[color]
color[x_] := color[x] = ColorData[3, "ColorList"][[++ncolors]] ;   

这会导致每个唯一参数产生一个新颜色..

其余代码相同..

enter image description here

相关问题