[英]Netgraph dot layout avoiding node labels overlap: howto?
I am generating simple syntactic dependecies trees such as in the following example:我正在生成简单的语法依赖树,例如以下示例:
Best layout is "dot" but on the top row things get very crowded.最好的布局是“点”,但顶行的东西会变得非常拥挤。 Is there any parameter or other way to avoid node labels overlapping with neighbouring nodes (and ideally edges)?
是否有任何参数或其他方法可以避免节点标签与相邻节点(最好是边缘)重叠?
Maybe some matplotlib parameter to "draw on a very wide canvas"?也许一些 matplotlib 参数“在非常宽的画布上绘制”? (sorry totally matplotlib ignorant).
(抱歉完全 matplotlib 无知)。
Here is the code I am currently using:这是我目前使用的代码:
import platform
import matplotlib
import matplotlib.pyplot as plt
import stanza
import networkx as nx
from netgraph import InteractiveGraph
if platform.system() == "Darwin":
matplotlib.use("TkAgg") # MacOSX backend is bugged; use TkAgg
def display_graph(G: nx.DiGraph, title: str):
"""Use netgraph to show the graph"""
nodecolors = nx.get_node_attributes(G, "nodecol")
nodelabels = nx.get_node_attributes(G, "lemma")
nodeshapes = nx.get_node_attributes(G, "nodeshape")
edgelabels = nx.get_edge_attributes(G, "label")
# now plot the graph
plot_instance = InteractiveGraph(
G,
node_layout="dot",
node_labels=nodelabels,
node_color=nodecolors,
node_label_fontdict=dict(size=12),
node_shape=nodeshapes,
node_alpha=0.5, # node transparency
edge_labels=edgelabels,
edge_layout="curved",
arrows=True,
)
plt.suptitle(title, wrap=True)
plt.show()
return
Is there any parameter or other way to avoid node labels overlapping with neighbouring nodes (and ideally edges)?
是否有任何参数或其他方法可以避免节点标签与相邻节点(最好是边缘)重叠?
If you set the parameter node_label_offset
to some float, eg node_label_offset=0.05
, then.netgraph will find the position 0.05 away from the node center that is furthest away from all other nodes and edges.如果将参数
node_label_offset
设置为某个浮点数,例如node_label_offset=0.05
,则 .netgraph 将找到距离节点中心 0.05 的 position 0.05 距离所有其他节点和边最远的节点。 However, there is no guarantee that there aren't any label overlaps as the space may simply be too crowded.但是,不能保证没有任何 label 重叠,因为空间可能太拥挤了。
Maybe some matplotlib parameter to "draw on a very wide canvas"?
也许一些 matplotlib 参数“在非常宽的画布上绘制”? (sorry totally matplotlib ignorant).
(抱歉完全 matplotlib 无知)。
The width and height of the canvas are controlled by the shape
parameter. canvas的宽高由
shape
参数控制。 The default is shape=(1, 1)
.默认值为
shape=(1, 1)
。 So shape=(2, 2)
would double the size of the canvas. However, as fontsizes in matplotlib (and hence.netgraph) are defined in display units, you would also have to increase the size of the matplotlib figure to decrease the size of the text relative to whole figure.所以
shape=(2, 2)
会使 canvas 的大小加倍。但是,由于 matplotlib(以及因此.netgraph)中的字体大小是以显示单位定义的,因此您还必须增加 matplotlib 图形的大小以减小大小相对于整个图形的文本。
fig, ax = plt.subplots(figsize=(2*6.4, 2*4.8)) # default is 6.4, 4.8
plot_instance = InteractiveGraph(G, node_layout='dot', shape=(2, 2), ax=ax, ...)
Further suggestions:进一步的建议:
node_edge_color=nodecolors
or node_edge_width=0.
node_edge_color=nodecolors
或node_edge_width=0.
. Basically, you want to remove any source of contrast that distract from the text if you want the labels to "pop".基本上,如果您希望标签“弹出”,您需要删除任何会分散文本注意力的对比来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.