1

我有一个代码,我在 TkInter 中嵌入了 matplotlib 图形。这工作正常并且数据更新,但是当我尝试使用 plt.x_ticks(rotation="vertical") 之类的命令时,它不会这样做,并且任何其他以 plt.x_ticks(rotation="vertical") 开头的命令都不会这样做。不起作用。通常在没有嵌入的 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()
4

0 回答 0