python中用于绘制各种图形的区域称作_在Python中绘制有向图的方式可以分别显示所有边...

1586010002-jmsa.png

I'm using Python to simulate a process that takes place on directed graphs. I would like to produce an animation of this process.

The problem that I've run into is that most Python graph visualization libraries combine pairs of directed edges into a single edge. For example, NetworkX draws only two edges when displaying the following graph, whereas I would like to display each of the four edges separately:

import networkx as nx

import matplotlib.pyplot as plt

G = nx.MultiDiGraph()

G.add_edges_from([

(1, 2),

(2, 3),

(3, 2),

(2, 1),

])

plt.figure(figsize=(8,8))

nx.draw(G)

olCJa.png

I would like to display something like this, with each parallel edge drawn separately:

qppgo.png

The question R reciprocal edges in igraph in R seems to deal with the same issue, but the solution there is for the R igraph library, not the Python one.

Is there an easy way to produce this style of plot using an existing Python graph visualization library? It would be a bonus if it could support multigraphs.

I'm open to solutions that invoke an external program to produce the images. I'd like to generate a whole series of animation frames, so the solution must be automated.

解决方案

The Graphviz tools appear to display distinct edges.

For example, giving this:

digraph G {

A -> B;

A -> B;

A -> B;

B -> C;

B -> A;

C -> B;

}

to dot produces:

A7dnv.gif

Graphviz's input language is pretty simple so you can generate it on your own, though searching for "python graphviz" does turn up a couple of libraries including a graphviz module on PyPI.

Here's python that generates the above graph using the graphviz module:

from graphviz import Digraph

dot = Digraph()

dot.node('A', 'A')

dot.node('B', 'B')

dot.node('C', 'C')

dot.edges(['AB', 'AB', 'AB', 'BC', 'BA', 'CB'])

print(dot.source)

dot.render(file_name, view=True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值