我是Python的新手,目前正在努力集成必须进行tkinter的图形输出。每当我尝试运行此命令时,我也会收到错误消息。但是,每当我尝试单独运行中文邮递员问题代码时,它就可以正常运行。
我想将中国邮递员问题(而不是从Python中的默认值打开)绘制的图形放到tkinter中,但是我不确定如何将两者整合在一起
这是我目前所拥有的,我不确定什么是相关的,并插入了我到目前为止所写的全部代码:
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *
import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import sys
class mclass:
def __init__(self, window):
self.window = window
self.box = Entry(window)
self.button = Button (window, text="check", command=self.plot)
self.box.pack ()
self.button.pack()
def plot (self):
df1 = pd.read_csv("U:\\user\nodes_fixed.csv")
#print(df1.isnull().values.any())
df2 = pd.read_csv(r"U:\\user\edge_list_3_fixed.csv")
print (df2)
g=nx.Graph()
# Add edges and edge attributes
for i, elrow in df2.iterrows():
# g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
g.add_edge(elrow[0], elrow[1], **elrow[2:].to_dict())
# Edge list example
print(elrow[0]) # node1
print(elrow[1]) # node2
print(elrow[2:].to_dict()) # edge attribute dict
# Add node attributes
for i, nlrow in df1.iterrows():
# g.node[nlrow['id']] = nlrow[1:].to_dict()
nx.set_node_attributes(g, {nlrow['ID']: nlrow[1:].to_dict()})
# Node list example
print(nlrow)
# Preview first 5 edges
list(g.edges(data=True))[0:5]
# Preview first 10 nodes
list(g.nodes(data=True))[0:10]
print('# of edges: {}'.format(g.number_of_edges()))
print('# of nodes: {}'.format(g.number_of_nodes()))
# Define node positions data structure (dict) for plotting
for node in g.nodes(data=True):
print(node)
print("")
node_positions = {node[0]: (node[1]['X'], -node[1]['Y']) for node in
g.nodes(data=True)}
# Preview of node_positions
dict(list(node_positions.items())[0:5])
# Define data structure (list) of edge colors for plotting
# edge_colors = [e[2]['color'] for e in g.edges(data=True)]
edge_colors = [e[2]['color'] for e in list(g.edges(data=True))]
# Preview first 10
edge_colors[0:10]
plt.figure(figsize=(8, 6))
nx.draw(g, pos=node_positions, edge_color=edge_colors, node_size=10,
node_color='black')
plt.title('Graph Representation of repair trail', size=15)
canvas = FigureCanvasTkAgg(fig, master=self.window)
canvas.get_tk_widget().pack()
canvas.draw()
window= Tk()
start= mclass (window)
window.mainloop()
这是我得到的错误:
回溯(最近通话最近): 文件“ C:/ Users / 212749017 / AppData / Local / Programs / Python / Python37- 32 / testagain22.py“,第32行,在 对于我来说,在df2.iterrows()中行进: NameError:名称“ df2”未定义
答案 0 :(得分:0)
要使用matplotlib,您需要使用后端。 Sentdex有一个英语视频供后端使用。在tkinter中绘制图像是不同的。
也在视频中使用Sentdex
供Python3.X使用
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
代替
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
也许这些库您稍后会要求
import tkinter as tk
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from matplotlib.figure import Figure
要绘制的示例代码:
ax = self.figure.add_subplot(111)
#new_data contains the data. In this example code, nothing
new_data.plot(kind='barh', legend=False, ax=ax) #Type of plot
canvas = FigureCanvasTkAgg(self.figure, self.frame_graficos)
canvas.get_tk_widget().grid(row=1, column=0)
#Toolbar for icons that matplotlib give us
toolbarFrame = tk.Frame(self.frame_graficos)
toolbarFrame.grid(row=0, column=0) # use pack if you're using pack command
toolbar = NavigationToolbar2Tk(canvas, toolbarFrame)