在Plotly中绘制3D网络图。

huangapple go评论79阅读模式
英文:

Plotting 3d network graphs in plotly

问题

我正在尝试使用plotly.js绘制3D网络

    <html>
      <head>
        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
      </head>
      <body>
        <div id="myDiv"></div>
        <script>
          var nodes = [
            { x: 0, y: 0, z: 0 },
            { x: 1, y: 1, z: 1 },
            { x: 2, y: 0, z: 2 },
            { x: 3, y: 1, z: 3 },
            { x: 4, y: 0, z: 4 }
          ];
    
          var edges = [
            { source: 0, target: 1 },
            { source: 1, target: 2 },
            { source: 2, target: 3 },
            { source: 3, target: 4 }
          ];
    
          var x = [];
          var y = [];
          var z = [];
    
          for (var i = 0; i < nodes.length; i++) {
            x.push(nodes[i].x);
            y.push(nodes[i].y);
            z.push(nodes[i].z);
          }
    
          var xS = [];
          var yS = [];
          var zS = [];
          var xT = [];
          var yT = [];
          var zT = [];
    
          for (var i = 0; i < edges.length; i++) {
            xS.push(nodes[edges[i].source].x);
            yS.push(nodes[edges[i].source].y);
            zS.push(nodes[edges[i].source].z);
            xT.push(nodes[edges[i].target].x);
            yT.push(nodes[edges[i].target].y);
            zT.push(nodes[edges[i].target].z);
          }
    
          var traceNodes = {
            x: x, y: y, z: z,
            mode: 'markers',
            marker: { size: 12, color: 'red' },
            type: 'scatter3d'
          };
    
          var traceEdges = {
            x: [xS, xT],
            y: [yS, yT],
            z: [zS, zT],
            mode: 'lines',
            line: { color: 'red', width: 2},
            opacity: 0.8,
            type: 'scatter3d'
          };
          
          var layout = {
            margin: { l: 0, r: 0, b: 0, t: 0 }
          };
    
          Plotly.newPlot('myDiv', [traceNodes, traceEdges], layout);
        </script>
      </body>
    </html>
英文:

I am trying to plot a 3d network using plotly.js

&lt;html&gt;
&lt;head&gt;
&lt;script src=&quot;https://cdn.plot.ly/plotly-latest.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;myDiv&quot;&gt;&lt;/div&gt;
&lt;script&gt;
var nodes = [
{ x: 0, y: 0, z: 0 },
{ x: 1, y: 1, z: 1 },
{ x: 2, y: 0, z: 2 },
{ x: 3, y: 1, z: 3 },
{ x: 4, y: 0, z: 4 }
];
var edges = [
{ source: 0, target: 1 },
{ source: 1, target: 2 },
{ source: 2, target: 3 },
{ source: 3, target: 4 }
];
var x = [];
var y = [];
var z = [];
for (var i = 0; i &lt; nodes.length; i++) {
x.push(nodes[i].x);
y.push(nodes[i].y);
z.push(nodes[i].z);
}
var xS = [];
var yS = [];
var zS = [];
var xT = [];
var yT = [];
var zT = [];
for (var i = 0; i &lt; edges.length; i++) {
xS.push(nodes[edges[i].source].x);
yS.push(nodes[edges[i].source].y);
zS.push(nodes[edges[i].source].z);
xT.push(nodes[edges[i].target].x);
yT.push(nodes[edges[i].target].y);
zT.push(nodes[edges[i].target].z);
}
var traceNodes = {
x: x, y: y, z: z,
mode: &#39;markers&#39;,
marker: { size: 12, color: &#39;red&#39; },
type: &#39;scatter3d&#39;
};
var traceEdges = {
x: [xS, xT],
y: [yS, yT],
z: [zS, zT],
mode: &#39;lines&#39;,
line: { color: &#39;red&#39;, width: 2},
opacity: 0.8,
type: &#39;scatter3d&#39;
};
var layout = {
margin: { l: 0, r: 0, b: 0, t: 0 }
};
Plotly.newPlot(&#39;myDiv&#39;, [traceNodes, traceEdges], layout);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

The nodes of the network are visible but the edges are not visible. Suggestions on
how to fix this issue will be really helpful.

答案1

得分: 2

问题在于traceEdges的构建方式。在每个轴上,应该有一个由null值分隔的(源,目标)坐标列表,即:

x: [source_0.x, target_0.x, null, source_1.x, target_1.x, null, ...]

我们使用null值来正确地将每条边隔离开,防止它们全部连接在一起(我们想要在source_0target_0之间画一条线,但不想在target_0source_1之间画线)。

所以,我们不再使用[xS, xT][yS, yT][zS, zT],而是定义edge_xedge_yedge_z,如下所示:

const edge_x  = [];
const edge_y  = [];
const edge_z  = [];
for (var i = 0; i < edges.length; i++) {
const a = nodes[edges[i].source];
const b = nodes[edges[i].target];
edge_x.push(a.x, b.x, null);
edge_y.push(a.y, b.y, null);
edge_z.push(a.z, b.z, null);
}
const traceEdges = {
x: edge_x,
y: edge_y,
z: edge_z,
type: 'scatter3d',
mode: 'lines',
line: { color: 'red', width: 2},
opacity: 0.8
};
// (其余的代码不变)
英文:

The problem is how traceEdges is built. On each axis, there should be a list of (source, target) coordinates separated by a null value, ie. :

x: [source_0.x, target_0.x, null, source_1.x, target_1.x, null, ...]

We use null values to properly isolate each edge from the others and prevent chaining them all together (we want to draw a line between source_0 and target_0, but not between target_0 and source_1).

So instead of having [xS, xT], [yS, yT], and [zS, zT] we will just have edge_x, edge_y, and edge_z, defined as follows :

const edge_x  = [];
const edge_y  = [];
const edge_z  = [];
for (var i = 0; i &lt; edges.length; i++) {
const a = nodes[edges[i].source];
const b = nodes[edges[i].target];
edge_x.push(a.x, b.x, null);
edge_y.push(a.y, b.y, null);
edge_z.push(a.z, b.z, null);
}
const traceEdges = {
x: edge_x,
y: edge_y,
z: edge_z,
type: &#39;scatter3d&#39;,
mode: &#39;lines&#39;,
line: { color: &#39;red&#39;, width: 2},
opacity: 0.8
};
// (rest of the code doesn&#39;t change)

Here is the output :

在Plotly中绘制3D网络图。

huangapple
  • 本文由 发表于 2023年2月8日 16:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定