【问题标题】:Text missing from graph using networkx and matplotlib使用networkx和matplotlib从图形中丢失文本
【发布时间】:2015-08-25 19:18:12
【问题描述】:

我已经在 Mac Os 10.10.3 for python 3.3 上使用 pip 安装了 networkx 和 matplotlib。

当我运行以下代码时:

import networkx as nx
import matplotlib.pyplot as plt

def simple_graph():

    #create an empty graph
    G = nx.Graph()

    #add three edges
    G.add_edge('A','B');
    G.add_edge('B','C');
    G.add_edge('C','A');

    #draw the graph
    nx.draw(G)

    #show
    plt.show()

simple_graph()

我得到了预期的图表,但所有文本都丢失了。有什么建议吗?

【问题讨论】:

  • 你想要节点标签吗?你可能更喜欢打电话给nx.draw_networkx(G)

标签: python python-3.x matplotlib networkx


【解决方案1】:

传递参数with_labels=True:

import networkx as nx
import matplotlib.pyplot as plt

def simple_graph():

    #create an empty graph
    G = nx.Graph()

    #add three edges
    G.add_edge('A','B');
    G.add_edge('B','C');
    G.add_edge('C','A');

    #draw the graph
    nx.draw(G,with_labels=True)

    #show
    plt.show()

simple_graph()

产生情节:

如果你打电话给draw_networkx:

基本上nx.draw 调用nx.draw_networkx,但没有设置某些参数。

【讨论】:

    【解决方案2】:

    主要问题正如 EdChum 所回答的那样。

    但是,您似乎正在使用 ipython。在这种情况下,即使包含 with_labels=True 也可能会失败。如果您绘制并保存它,它将具有标签,但不会使用plt.show() 命令显示标签。这是一个错误,(根据 cmets)已在即将发布的版本中修复。

    如果我将您的代码编辑为 with_labels=True:

    import networkx as nx
    import matplotlib.pyplot as plt
    
    def simple_graph():
    
        #create an empty graph
        G = nx.Graph()
    
        #add three edges
        G.add_edge('A','B');
        G.add_edge('B','C');
        G.add_edge('C','A');
    
        #draw the graph
        nx.draw(G, with_labels=True)
    
        #show
        plt.show()
    
    simple_graph()
    

    并在 ipython 中运行它,我得到了绘图,但没有出现标签,并且错误消息:

    >Traceback (most recent call last):
    >  File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", >line 59, in draw_wrapper
        draw(artist, renderer, *args, **kwargs)
      File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1079, in draw
        func(*args)
      File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
        draw(artist, renderer, *args, **kwargs)
      File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2092, in draw
        a.draw(renderer)
      File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
        draw(artist, renderer, *args, **kwargs)
      File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 538, in draw
        bbox, info, descent = self._get_layout(renderer)
      File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 311, in _get_layout
        ismath=False)
      File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 166, in get_text_width_height_descent
        six.text_type(s), family, size, weight, style)
    TypeError: bad argument type for built-in operation
    

    这是一个已知的错误。一些细节可以在这里找到:pylab/networkx; no node labels displayed after update

    【讨论】:

    • 这是在当前master上修复的,将在1.5版本中发布
    • @tcaswell 谢谢 - 请在发布后跟进。
    • 我会把它留给你。请留意发布公告。
    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多