问题描述
我正在matplotlib中尝试networkx和可视化,我感到困惑,因为我不清楚他们之间如何相互作用?有简单的例子
I am trying networkx and visualization in matplotlib an I'm confused becouse I do not clearly understand how do they interact with each other?There simple example
import matplotlib.pyplot
import networkx as nx
G=nx.path_graph(8)
nx.draw(G)
matplotlib.pyplot.show()
我在哪里告诉pyplot我想绘制图形G?我猜nx.draw使用的是matplotlib.pyplot之类的东西.因此,如果我想绘制2个图形:
Where do I tell pyplot, that I want to draw graph G?I guess that nx.draw use something like matplotlib.pyplot.{plot, etc ...}So, if I want to draw 2 graphs:
import matplotlib.pyplot
import networkx as nx
G=nx.path_graph(8)
E=nx.path_graph(30)
nx.draw(G)
matplotlib.pyplot.figure()
nx.draw(E)
matplotlib.pyplot.show()
然后...小实验
import networkx as nx
G=nx.path_graph(8)
E=nx.path_graph(30)
nx.draw(G)
import matplotlib.pyplot
matplotlib.pyplot.figure()
nx.draw(E)
import matplotlib.pyplot as plt
plt.show()
请不要因为这个愚蠢的代码而杀了我,我只是想了解-networkx如何绘制matplotlib,甚至还没有导入!
Please don't kill me about this stupid code, I am just trying to understand - how do networkx draw something matplotlib, while it even doesn't import yet!
P.S:对不起,我的英语.
P.S: Sorry for my English.
推荐答案
如果要分别绘制图形或创建单个 Axes
对象并将其传递给,只需创建两个不同的轴nx.draw
.例如:
Just create two different axes if you want to draw the graphs separately or create a single Axes
object an pass it to nx.draw
. For example:
G = nx.path_graph(8)
E = nx.path_graph(30)
# one plot, both graphs
fig, ax = subplots()
nx.draw(G, ax=ax)
nx.draw(E, ax=ax)
获得:
如果要两个不同的图形对象,则分别创建它们,如下所示:
If you want two different figure objects then create them separately, like so:
G = nx.path_graph(8)
E = nx.path_graph(30)
# two separate graphs
fig1 = figure()
ax1 = fig1.add_subplot(111)
nx.draw(G, ax=ax1)
fig2 = figure()
ax2 = fig2.add_subplot(111)
nx.draw(G, ax=ax2)
收益:
最后,如果需要,您可以创建一个子图,像这样:
Finally, you could create a subplot if you wanted, like this:
G = nx.path_graph(8)
E = nx.path_graph(30)
pos=nx.spring_layout(E,iterations=100)
subplot(121)
nx.draw(E, pos)
subplot(122)
nx.draw(G, pos)
导致:
对于任何有价值的东西,当您使用 matplotlib
的API 时, nx.draw
的 ax
参数看起来毫无用处想要在 pylab
之外创建子图,因为 nx.draw
调用了 gca
,这使其依赖于 pylab
界面.并没有真正弄清楚为什么会这样,只是以为我会指出来.
For whatever it's worth it looks like the ax
argument to nx.draw
is useless with matplotlib
's API when you want to create subplots outside of pylab
, because nx.draw
has some calls to gca
which makes it dependent on the pylab
interface. Didn't really dig into why that is, just thought I would point it out.
nx.draw
的源代码非常简单:
try:
import matplotlib.pylab as pylab
except ImportError:
raise ImportError("Matplotlib required for draw()")
except RuntimeError:
print("Matplotlib unable to open display")
raise
cf=pylab.gcf()
cf.set_facecolor('w')
if ax is None:
if cf._axstack() is None:
ax=cf.add_axes((0,0,1,1))
else:
ax=cf.gca()
# allow callers to override the hold state by passing hold=True|False
b = pylab.ishold()
h = kwds.pop('hold', None)
if h is not None:
pylab.hold(h)
try:
draw_networkx(G,pos=pos,ax=ax,**kwds)
ax.set_axis_off()
pylab.draw_if_interactive()
except:
pylab.hold(b)
raise
pylab.hold(b)
return
- 使用
gcf
从环境中捕获人物. - 然后将
Axes
对象添加到图中(如果不存在),否则使用gca
从环境中获取它. - 使剧情的脸部颜色为白色
- 打开
保持
- 使用内部函数进行绘制
- 关闭轴
- 最后,如果我们处于交互模式,请绘制它并重新引发所有被捕获的异常
- A figure is captured from the environment using
gcf
. - Then an
Axes
object is added to the figure if one doesn't exist, otherwise get it from the environment usinggca
. - Make the plot face color white
- turn
hold
on - draw it with an internal function
- turn off the axes
- lastly if we're in interactive mode, draw it and reraise any exceptions that were caught
这篇关于networkx和matplotlib之间的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!