Discussion:
[pyqtgraph] NetworkX in pyqtgraph
JME
2018-08-16 21:18:28 UTC
Permalink
I have been attempting to plot a network, generated from networkX, in a
pyqtgraph widget. NetworkX generates graphs for Matplotlib (and by that, I
mean...it plots...not well, and not interactive), and I was hoping to be
able to use pyqtgraph to display these graphs. Since they are not
coordinate based figures, however, I believe that there are some problems
passing the network to the plotting functions. I can use networkX to
generate [and save] things like .graphML , GEXF, GML,YAML,SparseGraph6,
Pajek, and so on, but is there a way to then plot or open these datatypes
in a pyqtgraph widget to be able to "zoom around" and look at them?
I thought about using the matplotlib widget within the pyqtgraph plot, but
I don't know what this entails, or if it has any added value to just using
a matplotlib canvas anyways.

To make the matplotlib widget, I use the following:

from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
import matplotlib

# Ensure using PyQt5 backend
matplotlib.use('QT5Agg')

# Matplotlib canvas class to create figure
class MplCanvas(Canvas):
def __init__(self):
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.ax.cla()#test
Canvas.__init__(self, self.fig)
Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
Canvas.updateGeometry(self)

# Matplotlib widget
class MplWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent) # Inherit from QWidget
self.canvas = MplCanvas() # Create canvas object
self.vbl = QtWidgets.QVBoxLayout() # Set box for plotting
self.vbl.addWidget(self.canvas)
self.setLayout(self.vbl)




To generate the network in the Matplotlib widget, I do the following:

G = nx.Graph()#create a networkx graph
Comparison_List = pd.read_csv("SampleTracker.csv",index_col=[0])
#just some data filtering for the next few steps
Comparison_List2 =
Comparison_List[Comparison_List["Status"]=='Finalized'].Sample_ID.tolist()
Comparison_List3 = Comparison_List[Comparison_List["Status"]=='Data
Processing Needed'].Sample_ID.tolist()
fulledgelist=[]
for i in obj_Network.index.tolist():
edgelist = eval(obj_Network.loc[i]["Trait_1"])
if len(edgelist) < len(Comparison_List2):
edgelist_condensed = edgelist
for y in edgelist_condensed:
Value = (i,y)
fulledgelist.append(Value)
goldlist = [item for item in fulledgelist]
G.add_nodes_from(obj_Network.index.tolist(),color='yellow') #Add
nodes
G.add_nodes_from(Comparison_List2,color='green')
G.add_nodes_from(Comparison_List3,color='orange')
G.add_edges_from(goldlist,weight=0.8) #connect the nodes
nx.draw_spring(G,
with_labels=True,ax=self.widget.canvas.ax)#Altered to display on ax axis
self.widget.canvas.draw() #this works as an MPL.pyplot object (with
correct back-ends enabled)

I believe the problem is the final part, where the "canvas" is a mpl
object. Whereas, the pyqtgraph function obviously does not know how to
handle the request to "draw".

Any thoughts or ideas? Thanks for your time.
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/dce3f68f-af9f-4761-b3c9-1e3d6f8e8fed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Patrick
2018-08-17 02:07:12 UTC
Permalink
Hi,

This won't help with the actual laying out of the graph (as in, I assume
networkX can decide the "best" x,y coordinates of the nodes) but pyqtgraph
has the GraphItem, which can render a graph network. I'm not sure how you'd
go about obtaining the node positions etc from networkX and passing them to
the pyqtgraph, but it might be possible to make a bridge between them
somehow. Or yeah, get NetworkX to dump out data in some easy format and
read it back in and recreate the graph in pyqtgraph.
Have you seen the "Labeled Graph" demo in the examples?
(https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/CustomGraphItem.py)
Not sure if that will help or not -- apologies I don't have any experience
with NetworkX or plotting graphs in pyqtgraph...

With the matplotlib widget, as far as I'm aware it's just a simple wrapper
around a matplotlib figure as a QWidget. I don't think it will give you any
additional benefits of just plotting in matplotlib.

Patrick
Post by JME
I have been attempting to plot a network, generated from networkX, in a
pyqtgraph widget. NetworkX generates graphs for Matplotlib (and by that, I
mean...it plots...not well, and not interactive), and I was hoping to be
able to use pyqtgraph to display these graphs. Since they are not
coordinate based figures, however, I believe that there are some problems
passing the network to the plotting functions. I can use networkX to
generate [and save] things like .graphML , GEXF, GML,YAML,SparseGraph6,
Pajek, and so on, but is there a way to then plot or open these datatypes
in a pyqtgraph widget to be able to "zoom around" and look at them?
I thought about using the matplotlib widget within the pyqtgraph plot, but
I don't know what this entails, or if it has any added value to just using
a matplotlib canvas anyways.
from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
import matplotlib
# Ensure using PyQt5 backend
matplotlib.use('QT5Agg')
# Matplotlib canvas class to create figure
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.ax.cla()#test
Canvas.__init__(self, self.fig)
Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
Canvas.updateGeometry(self)
# Matplotlib widget
QtWidgets.QWidget.__init__(self, parent) # Inherit from QWidget
self.canvas = MplCanvas() # Create canvas object
self.vbl = QtWidgets.QVBoxLayout() # Set box for plotting
self.vbl.addWidget(self.canvas)
self.setLayout(self.vbl)
G = nx.Graph()#create a networkx graph
Comparison_List = pd.read_csv("SampleTracker.csv",index_col=[0])
#just some data filtering for the next few steps
Comparison_List2 =
Comparison_List[Comparison_List["Status"]=='Finalized'].Sample_ID.tolist()
Comparison_List3 =
Comparison_List[Comparison_List["Status"]=='Data Processing
Needed'].Sample_ID.tolist()
fulledgelist=[]
edgelist = eval(obj_Network.loc[i]["Trait_1"])
edgelist_condensed = edgelist
Value = (i,y)
fulledgelist.append(Value)
goldlist = [item for item in fulledgelist]
G.add_nodes_from(obj_Network.index.tolist(),color='yellow') #Add
nodes
G.add_nodes_from(Comparison_List2,color='green')
G.add_nodes_from(Comparison_List3,color='orange')
G.add_edges_from(goldlist,weight=0.8) #connect the nodes
nx.draw_spring(G, with_labels=True,ax=self.widget.canvas.ax)#Altered
to display on ax axis
self.widget.canvas.draw() #this works as an MPL.pyplot object
(with correct back-ends enabled)
I believe the problem is the final part, where the "canvas" is a mpl
object. Whereas, the pyqtgraph function obviously does not know how to
handle the request to "draw".
Any thoughts or ideas? Thanks for your time.
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/898c5ef0-a9d2-47b7-94f1-126020061532%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...