我正在使用 netgraph 來可視化我的 networkx 圖。我現在的問題是,公式的可視化自然不像我期望的那樣漂亮。這是我的示例圖:
import networkx as nx
from netgraph import Graph # pip install netgraph
import matplotlib.pyplot as plt
node_labels = {1: 'p→q', 2: '?q', 3: '? (?p)', 4: '?p', 5: '?p∧ ? (?p)', 6: 'p', 7: 'q', 8: 'q∧ ?q', 9: '?p'}
color_map = {1: 'red', 2: 'red', 3: 'red', 4: 'red', 5: 'lightblue', 6: 'lightblue', 7: 'lightblue', 8: 'lightblue', 9: 'blue'}
edge_labels = {(3, 5): '∧I', (4, 5): '∧I', (4, 6): '?E', (5, 6): '?E', (1, 7): '→E', (6, 7): '→E', (2, 8): '∧I', (7, 8): '∧I', (8, 9): '?E', (3, 9): '?E'}
graph = nx.from_edgelist(edge_labels, nx.DiGraph())
Graph(graph, node_labels=node_labels, edge_labels=edge_labels,
node_color=color_map, node_edge_color=color_map, arrows=True)
plt.show()
輸出(直接/放大后):
:
我現在的問題:
- 他們是一種調整節點字體大小的方法嗎?
- 是否可以以水平對齊的方式旋轉邊緣標簽?
uj5u.com熱心網友回復:
默認情況下,如果節點標簽繪制在節點頂部(即沒有偏移),netgraph 會縮放節點標簽字體大小,以使所有標簽都適合它們各自的節點藝術家。但是,可以通過明確指定字體大小來覆寫此行為,例如使用node_label_fontdict(size=20).
可以通過設定標志來關閉邊緣標簽的旋轉edge_label_rotate = False。

#!/usr/bin/env python
# coding: utf-8
import matplotlib.pyplot as plt
import networkx as nx
from netgraph import Graph # pip install netgraph
node_labels = {1: 'p→q', 2: '?q', 3: '? (?p)', 4: '?p', 5: '?p∧ ? (?p)', 6: 'p', 7: 'q', 8: 'q∧ ?q', 9: '?p'}
color_map = {1: 'red', 2: 'red', 3: 'red', 4: 'red', 5: 'lightblue', 6: 'lightblue', 7: 'lightblue', 8: 'lightblue', 9: 'blue'}
edge_labels = {(3, 5): '∧I', (4, 5): '∧I', (4, 6): '?E', (5, 6): '?E', (1, 7): '→E', (6, 7): '→E', (2, 8): '∧I', (7, 8): '∧I', (8, 9): '?E', (3, 9): '?E'}
graph = nx.from_edgelist(edge_labels, nx.DiGraph())
Graph(graph, node_layout='dot',
node_labels=node_labels, node_label_fontdict=dict(size=21),
edge_labels=edge_labels, edge_label_fontdict=dict(size=14), edge_label_rotate=False,
node_color=color_map, node_edge_color=color_map, arrows=True
)
plt.show()
uj5u.com熱心網友回復:
Plotly 有一個
編輯:
用 Plotly 添加邊緣方向有點棘手(
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461105.html
標籤:Python matplotlib 网络x 网络图
