networkx draw_networkx_edges capstyle

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

Does anyone know if it is possible to have fine-grained control over line properties when drawing networkx edges via (for example) draw_networkx_edges? I would like to control the line solid_capstyle and solid_joinstyle, which are (matplotlib) Line2D properties.

>>> import networkx as nx >>> import matplotlib.pyplot as plt >>> G = nx.dodecahedral_graph() >>> edges = nx.draw_networkx_edges(G, pos=nx.spring_layout(G), width=7) >>> plt.show() 

In the example above, there are 'gaps' between the edges which I'd like to hide by controlling the capstyle. I thought about adding the nodes at just the right size to fill in the gaps, but the edges in my final plot are coloured, so adding nodes won't cut it. I can't figure out from the documentation or looking at edges.properties() how to do what I want to do... any suggestions?

Carson

回答1:

It looks like you can't set the capstyle on matplotlib line collections.

But you can make your own collection of edges using Line2D objects which allows you to control the capstyle:

import networkx as nx import matplotlib.pyplot as plt from matplotlib.lines import Line2D G = nx.dodecahedral_graph() pos = nx.spring_layout(G) ax = plt.gca() for u,v in G.edges():     x = [pos[u][0],pos[v][0]]     y = [pos[u][1],pos[v][1]]     l = Line2D(x,y,linewidth=8,solid_capstyle='round')     ax.add_line(l) ax.autoscale() plt.show() 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!