如何在Pyvis网络中隐藏节点标签?

问题描述

我有一个大约100个高度连接的节点的网络。现在,所有标签看起来都很混乱。我试图删除标签属性设置为labelNone的{​​{1}},但是随后显示了ID。是否可以隐藏标签或仅显示所选节点和边的标签

解决方法

从文档(https://pyvis.readthedocs.io/en/latest/_modules/pyvis/network.html)中,我看到add_node类的Network方法包含逻辑测试if label。如果标签为False或空字符串(None),则结果将为"",但是如果您尝试使用近似值的空字符串,则结果将为True。空格" "

如果上述操作失败,则可以尝试编辑add_node中的代码,或者(也许最好)定义自己的Network,该代码继承自原始方法并覆盖add_node方法。也许是这样的:

from pyvis.network import Network

class AbsoluteLabelNetwork(Network):
    """A version of the pyvis.network.Network class that always uses the label provided"""
    def add_node(self,n_id,label=None,shape="dot",**options):
        """See parent class for docstr,with the exception that label will always be used"""
        assert isinstance(n_id,str) or isinstance(n_id,int)
        node_label = label  # note: change from package version
        if n_id not in self.node_ids:
            n = Node(n_id,shape,label=node_label,font_color=self.font_color,**options)
            self.nodes.append(n.options)
            self.node_ids.append(n_id)

请注意,这些可能的解决方案未经测试,因此如果它们对您有所帮助,我将很感兴趣。