我知道这是一个常见问题,并且已经阅读了与此错误相关的所有链接。我意识到我必须在pyplot等导入之前放置matplotlib.use(),但仍然无法正常工作。我正在使用jupyter notebook和python 2.7。这些都是我已经导入的:
import Tkinter as tk
from Tkinter import*
import tkMessageBox
import tkFileDialog
import pandas as pd
import matplotlib
matplotlib.use('tkagg')
from PIL import Image, ImageTk
from tkinter import ttk
import pylab as plt
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
from matplotlib.figure import Figure
import seaborn as sns
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from collections import Counter
try:
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk as PlotNav
except:
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg as PlotNav
,此函数使用其中的tkagg
提供了导航工具栏:
def monthly_sells(array,hh):
monse = tk.Toplevel(hh)
array['monthj'] = pd.to_numeric(array['monthj'])
x=array['monthj'].values.tolist()
x.sort(key=int)
f = Figure(figsize=(4,3), dpi=80)
f.add_axes([0.2, 0.15,0.78,0.7])
canvas = FigureCanvasTkAgg(f, master=monse)
canvas.get_tk_widget().grid(row=1, column=3, rowspan=6)
tkagg.PlotNav(canvas, monse)
p = f.gca()
bins=[1,2,3,4,5,6,7,8,9,10,11,12,13]
p.hist(x,bins,rwidth=0.8, label='sales amount in different months of year' )
p.set_xlabel('month', fontsize = 10)
p.set_ylabel('number of orders', fontsize = 10)
当我单击使用monthly_sells函数作为命令的按钮时,会发生此错误。
未定义全局名称'tkagg'