我有一个代码,我在TkInter中嵌入了matplotlib图形。这工作正常并且数据更新,但是当我尝试使用像plt.x_ticks(rotation =“vertical”)这样的命令时,它不执行它,以及以plt开头的任何其他命令。不起作用。通常在没有嵌入的MatPlotLib中,这会调整图形的功能。为什么不工作/为什么以及如何嵌入更改我的代码?
这里显示了一个更简单的示例代码,我遇到了同样的问题。这个只是反复绘制相同的sin函数,但它不会旋转轴刻度,或应用适当的轴范围。
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
import numpy as np
from pylab import *
from datetime import datetime
from time import localtime, strftime
import time
import serial
import re
import sys
import exceptions
if sys.version_info[0]<3:
import Tkinter as Tk
else:
import tkinter as Tk
#############################################
#############################################
# All of the following is Specific to Tk #
# and getting the window with buttons open #
#############################################
#############################################
def mainWindow():
global i
global fig
global canvas
root.wm_title("Practice with TK")
fig=Figure(figsize=(7,5),dpi=100)
canvas=FigureCanvasTkAgg(fig,master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP,fill=Tk.BOTH,expand=1)
toolbar=NavigationToolbar2TkAgg(canvas,root)
toolbar.update()
button=Tk.Button(master=root, text="Plot", command=startPlotting)
button.pack(side=Tk.RIGHT)
button=Tk.Button(master=root, text="StopPlot", command=stopPlotting)
button.pack(side=Tk.RIGHT)
button=Tk.Button(master=root, text="QUIT", command=quit)
button.pack(side=Tk.LEFT)
Tk.mainloop()
def plotting():
global i
global j
l=i
if l==1:
j+=1
global fig
ax1=fig.add_subplot(111)
t=arange(0.0,3.0,.01)
s=np.sin(2*np.pi*t)
ax1.plot(t,s)
print(str(j)+":You have plotted")
plt.axis([0,4,-1.5,1.5])
plt.xticks(rotation='vertical')
canvas.show()
root.after(2000,plotting)
else:
pass
def startPlotting():
global i
global j
j=0
i=1
print i
plotting()
def stopPlotting():
global i
i=0
print i
def quit():
root.quit()
root.destroy()
global i
i=0
global root
root=Tk.Tk()
mainWindow()