Mathematica 中的颜色编码图
color coding graph in 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[ ]
子句以包含一些稍后将被解释为颜色的标签。但是,我遇到的其他解决方案,例如
明确列出不同颜色的数量。这里的颜色数量取决于 Morb
的值,所以我可以提前指定。有什么方法可以简单地用数字标记每条边,然后根据一些预定义的调色板按数字选择颜色?
我假设你的意思是这里的每个 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]] ;
这会导致每个独特的参数产生一种新颜色..
与您的其余代码相同..
这是我的示例代码
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[ ]
子句以包含一些稍后将被解释为颜色的标签。但是,我遇到的其他解决方案,例如
明确列出不同颜色的数量。这里的颜色数量取决于 Morb
的值,所以我可以提前指定。有什么方法可以简单地用数字标记每条边,然后根据一些预定义的调色板按数字选择颜色?
我假设你的意思是这里的每个 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]] ;
这会导致每个独特的参数产生一种新颜色..
与您的其余代码相同..