簡體   English   中英

如何在matplotlib / networkx中包裝標記標簽

[英]How to wrap marker labels in matplotlib/networkx

我正在嘗試使用networkx和matplotlib創建網絡圖。 我有以下代碼:

nx.draw_networkx_nodes(G, graph_pos, node_size=node_size, 
                       alpha=node_alpha, node_color=node_color)
nx.draw_networkx_edges(G, graph_pos, width=edge_tickness,
                       alpha=edge_alpha, edge_color=edge_color)
nx.draw_networkx_labels(G, graph_pos, labels=labels, font_size=node_text_size,
                        font_family=text_font)
plt.show()

結果如下圖: 網絡圖

如您所見,一些標記標簽很長,並且被切掉了繪圖的邊緣。 有沒有辦法包裝這些標簽的文本以適合繪圖區域?

編輯:

數據是來自我使用以下命令導入的.csv文件的45x45矩陣:

network = pd.read_csv('Network.csv')
G = nx.from_numpy_matrix(network.values)
graph_pos = nx.nx_agraph.graphviz_layout(G, prog='neato')

我想出了一個解決辦法,盡管不確定這是否是最好的方法:

for l in labels:
if len(labels[l]) > 55:
    indices = [i for i, x in enumerate(list(labels[l])) if x == ' ']
    indices = np.asarray(indices)
    try:
        idx = indices[indices > 55][0]
        newlabel = list(labels[l])
        newlabel.insert(idx, ' \n')
        newlabel = ''.join(newlabel)
        labels[l] = newlabel
    except:
        continue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM