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 [email protected].
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.