I have a code where I am embedding a matplotlib graphic in TkInter. This works fine and the data updates, but when I try to use commands like plt.x_ticks(rotation="vertical") it doesn't do it, and any other command that starts with plt. doesn't work. Normally in MatPlotLib w/o embedding, this adjust features of the graph. Why isn't it working/ why and how is embedding changing my code?
A simpler example code where I have the same problem is shown here. This one just plots the same sin function over and over, but it doesn't rotate the axis ticks, or apply the proper axis range.
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()