我正在编写tkinter GUI,我必须"显示" tkinter GUI窗口上的两个matplot / networkx图。问题是只有一个图表显示而另一个图表没有。 .grid()方法计算图形的空间,但该空间为空(见图)。你只能看到由ImageCanvasTkAgg(fig,self.master)输出的一个图像,但不能看到FigureCanvasTkAgg(figRed,self.master)输出的图像。 这是我的代码(删除不相关的东西,因为有很多代码) image):
class mdsCube(tk.Frame):
G = nx.cubical_graph()
pos = {0: np.array([ 0.82877618, 0.53211873]),
1: np.array([ 0.8059564, 0. ]),
2: np.array([ 0.51148475, 0.37349706]),
3: np.array([ 0.54462887, 0.89200482]),
4: np.array([ 0.31695909, 0.62593525]),
5:np.array([ 0.02260257, 1. ]),
6: np.array([ 0. , 0.46707769]),
7: np.array([ 0.27222528, 0.10714391])}
WelcomeMessage = "This MDS Algorithm implementation reduces dimensions of a cube to two." \
" Enter new point coordinates if you want to change cube dimensions."
def __init__(self, master):
tk.Frame.__init__(self,master)
self.master.title("MDS Cube Algorithm")
self.master.minsize(1000,470)
#self.master.resizable(False,False)
Header = tk.Label(self.master,text=self.WelcomeMessage,
font=("Times BOLD ITALIC", 16) )
Header.grid(column=0,row=0,padx=10,pady=10,columnspan=5)
self.click_ok()
def getDistanceMatrix(graph):
pathLengths = bl.dict(nx.all_pairs_dijkstra_path_length(graph))
df = pd.DataFrame(pathLengths)
distanceMatrix = df.as_matrix()
return distanceMatrix
def graph_node_position_mds(G):
#if edges of the graph do not have weights, assign 1 as a weight
if not hasattr(G.edges(), 'weight'):
for i in G.edges():
a = i[0]
b = i[1]
G.add_edge(a,b,weight=1)
DM = getDistanceMatrix(G)
MDS_ = MDS(n_components=2,dissimilarity='precomputed').fit_transform(DM)
MDS_dict = {}
for index, val in enumerate(MDS_):
MDS_dict[index] = val
return MDS_dict
def click_ok(self):
fig=plt.figure()
fig.patch.set_facecolor('white')
nx.draw_networkx_nodes(self.G, self.pos,
node_color='b',
node_size=500,
alpha=1)
nx.draw_networkx_edges(self.G, self.pos, width=1.0, alpha=0.5)
Canvas = FigureCanvasTkAgg(fig, self.master)
Canvas.get_tk_widget().grid(column=3,row=2,rowspan=8)
plt.plot()
figRed = plt.figure()
figRed.patch.set_facecolor('white')
dictPos = graph_node_position_mds(G)
nx.draw_networkx_nodes(self.G, dictPos,
node_color='r',
node_size=500,
alpha=1)
nx.draw_networkx_edges(self.G, dictPos, width=1.0, alpha=0.5)
CanvasRed = FigureCanvasTkAgg(figRed, self.master)
CanvasRed.get_tk_widget().grid(column=4, row=2,rowspan=8)
plt.plot()
if __name__ == '__main__':
root = tk.Tk()
window = mdsCube(root)
window.mainloop()