The following code will create only one window at a time, the second window will only show when the first one is closed by the user.
How to show them at the same time with different titles?
nx.draw_networkx(..a..)
nx.draw_networkx(..b..)
It works the same as making other plots with Matplotlib.
Use the figure() command to switch to a new figure.
import networkx as nx
import matplotlib.pyplot as plt
G=nx.cycle_graph(4)
H=nx.path_graph(4)
plt.figure(1)
nx.draw(G)
plt.figure(2)
nx.draw(H)
plt.show()
You can use matplotlib and a grid to show multiple graphs:
#!/usr/bin/env python
"""
Draw a graph with matplotlib.
You must have matplotlib for this to work.
"""
__author__ = """Aric Hagberg (hagberg#lanl.gov)"""
# Copyright (C) 2004-2008
# Aric Hagberg <hagberg#lanl.gov>
# Dan Schult <dschult#colgate.edu>
# Pieter Swart <swart#lanl.gov>
# All rights reserved.
# BSD license.
try:
import matplotlib.pyplot as plt
except:
raise
import networkx as nx
G=nx.grid_2d_graph(4,4) #4x4 grid
pos=nx.spring_layout(G,iterations=100)
plt.subplot(221)
nx.draw(G,pos,font_size=8)
plt.subplot(222)
nx.draw(G,pos,node_color='k',node_size=0,with_labels=False)
plt.subplot(223)
nx.draw(G,pos,node_color='g',node_size=250,with_labels=False,width=6)
plt.subplot(224)
H=G.to_directed()
nx.draw(H,pos,node_color='b',node_size=20,with_labels=False)
plt.savefig("four_grids.png")
plt.show()
Code above will generates this figure:
Reference: https://networkx.org/documentation/networkx-1.9/examples/drawing/four_grids.html
Related
I am trying to implement parametrized plotting using Jupyter notebook and ipywidgets to work in VS Code. Below is a snippet I am trying with:
import ipywidgets as widgets
import numpy as np
import matplotlib.pyplot as plt
#widgets.interact(freq=(1.0, 10.0))
def plot(freq=1.0):
t = np.linspace(-1., +1., 1000)
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(t, np.sin(2 * np.pi * freq * t), lw=2, color='blue')
This worked perfectly with Jupyter notebook in a browser, but in VS Code no plot appears. Simple ipywidgets controls like
#widgets.interact
def f(x=5):
print(x)
work perfectly good in my VS Code. Is this a general disadvantage of the VS Code Jupyter extension or am I doing things wrongly?
OS: Windows 10, 64 bit;
VS Code Jupyter extension: Miscosoft v. 57,661,722;
IPython: v. 8.9.0;
ipywidgets: v.8.0.4.
Hello what is a good way to visualize a pyg HeteroData object ?
(defined similarly: https://pytorch-geometric.readthedocs.io/en/latest/notes/heterogeneous.html#creating-heterogeneous-gnns )
I tried with networkx but I think it is restricted to homogeneous graph ( it is possible to convert it but it is much less informative).
g = torch_geometric.utils.to_networkx(data.to_homogeneous(), to_undirected=False )
Did anyone try to do it with other python lib (matplotlib) or js (sigma.js/d3.js)?
Any docs link you can share?
I have done the following:
import networkx as nx
from matplotlib import pyplot as plt
from torch_geometric.nn import to_hetero
g = torch_geometric.utils.to_networkx(data.to_homogeneous())
# Networkx seems to create extra nodes from our heterogeneous graph, so I remove them
isolated_nodes = [node for node in g.nodes() if g.out_degree(node) == 0]
[g.remove_node(i_n) for i_n in isolated_nodes]
# Plot the graph
nx.draw(g, with_labels=True)
plt.show()
However, it's true that it was "flattened" to a homogeneous, while it'd be more interesting to, for example, use different colors for different types of nodes.
The following code works, when I run it via Jupyter through Anaconda, yet when I do the same through VSC although gives me no error messages the output is missing the graph and only shows the top "name" segment, see picture. I would really like to it to work on VSC, any ideas why it isnt working? I have tried everything, reinstalling, updating, switching interpreters/kernels etc and nothing works.
import lumicks.pylake as lk
import matplotlib.pyplot as plt
import glob
import numpy as np
%matplotlib notebook
import lumicks.pylake as lk
#lk.pytest()
pip install scipy
fdcurves = {}
for filename in
glob.glob('C:/Users/paulv/OneDrive/Desktop/20220321_BNT_groupA/20220321-140249 FD Curve B7 1nM DNA Fwd1.h5'):#Control curves
file = lk.File(filename)
key, curve = file.fdcurves.popitem()
fdcurves[key] = curve
fdcurves
selector = lk.FdDistanceRangeSelector(fdcurves)
plt.show()
Thank you in advance !!
Simply put, magic function %precision does not respect floating point precision for output of a simple variable.
#Configure matplotlib to run on the browser
%matplotlib notebook
%precision 3
from ipywidgets import widgets
from IPython.display import display
import pandas as pd
import numpy as np
import matplotlib as mpl
mpl.get_backend() #Import pyplot scripting layer as plt
import matplotlib.pyplot as plt
import ipywidgets as widgets
np.random.seed(12345)
np.set_printoptions(precision=3)
df = pd.DataFrame([np.random.normal(32000,200000,3650),
np.random.normal(43000,100000,3650),
np.random.normal(43500,140000,3650),
np.random.normal(48000,70000,3650)],
index=[1992,1993,1994,1995]).T
df_stats = df.describe()
a=8/11
The output is simply 0
Please assist.
It looks like this post is a modified replica of Python division.
I used the following to assist,
from __future__ import division
Have an excellent day and thanks for the help!
I have a huge graph in networkx and I would like to get all the subgraphs of depth 2 from each node. Is there a nice way to do that using buildin function in networkx?
As I said in the comment, networkx.ego_graph fits the bill. You just need to make sure that you set the radius to 2 (default is 1):
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
# create some test graph
graph = nx.erdos_renyi_graph(1000, 0.005)
# create an ego-graph for some node
node = 0
ego_graph = nx.ego_graph(graph, node, radius=2)
# plot to check
nx.draw(ego_graph); plt.show()