First graph is erased when using clear() but second graph is still usable. Graphs plotted using Matplotlib and Tkinter for GUI
So here is my code.
from tkinter import *
from numpy import pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FC
import matplotlib.pyplot as plt
global r
global t
r = 90
t = 90
main_window = Tk()
main_window.title("GUI test")
plt.rc('grid', color='#316931', linewidth=1, linestyle='-')
plt.rc('xtick', labelsize=15)
plt.rc('ytick', labelsize=15)
# initializig figure 1
fig1 = plt.figure(figsize=(2, 1),facecolor='#4a5859')
fig1.tight_layout()
ax1 = fig1.add_axes([0.1, 0.1, 0.7, 0.7], polar=True)
ax1.set_thetamin(0)
ax1.set_thetamax(180)
ax1.set_rticks([])
arr1 = plt.arrow(r/180.*pi, 0, 0, 1, width = 0.015, edgecolor = 'black', lw = 2, zorder = 5)
canvas1 = FC(fig1, master = main_window)
canvas1.get_tk_widget().grid(row = 0, column = 1)
# initializing figure 2
fig2 = plt.figure(figsize=(2, 1),facecolor='#4a5859')
fig2.tight_layout()
ax2 = fig2.add_axes([0.1, 0.1, 0.7, 0.7], polar=True)
ax2.set_thetamin(0)
ax2.set_thetamax(180)
ax2.set_rticks([])
arr2 = plt.arrow(t/180.*pi, 0, 0, 1, width = 0.015, edgecolor = 'black', lw = 2, zorder = 5)
canvas2 = FC(fig2, master = main_window)
canvas2.get_tk_widget().grid(row = 0, column = 2)
# function to control first graph.
def polarGraphUD(z):
ax1.clear()
ax1.set_thetamin(0)
ax1.set_thetamax(180)
ax1.set_rticks([])
arr1 = plt.arrow(z/180.*pi, 0, 0, 1, width = 0.015,
edgecolor = 'black', lw = 2, zorder = 5)
canvas1.draw()
# function to control second graph.
def polarGraphT(z):
ax2.clear()
ax2.set_thetamin(0)
ax2.set_thetamax(180)
ax2.set_rticks([])
ax2.set_facecolor('#f4b860')
arr2 = plt.arrow(z/180.*pi, 0, 0, 1, width = 0.015,
edgecolor = 'black', lw = 2, zorder = 5)
canvas2.draw()
# function called by GUI to control value plotted in first graph.
def incR(direction):
global r
if direction:
r+=1
polarGraphUD(r)
else:
r-=1
polarGraphUD(r)
# function called by GUI to control value plotted in second graph.
def incT(direction):
global t
if direction:
t+=1
polarGraphT(t)
else:
t-=1
polarGraphT(t)
# GUI buttons
rotateL_btn = Button(main_window, text = 'left', bg = 'dark sea green', fg = 'white', repeatdelay = 80, repeatinterval = 80, command=lambda: incR(True))
rotateL_btn.grid(row = 2, column = 1, padx = 5, pady = 5)
rotateR_btn = Button(main_window, text = 'right', bg = 'dark sea green', fg = 'white', repeatdelay = 80, repeatinterval = 80, command=lambda: incR(False))
rotateR_btn.grid(row = 3, column = 1, padx = 5, pady = 5)
tiltU_btn = Button(main_window, text = 'up', bg = 'dark sea green', fg = 'white', repeatdelay = 80, repeatinterval = 80, command=lambda: incT(True))
tiltU_btn.grid(row = 4, column = 1, padx = 5, pady = 5)
tiltD_btn = Button(main_window, text = 'down', bg = 'dark sea green', fg = 'white', repeatdelay = 80, repeatinterval = 80, command=lambda: incT(False))
tiltD_btn.grid(row = 5, column = 1, padx = 5, pady = 5)
mainloop()
My issue is that every time the first graph is adjusted it's cleared, but the second one is able to be adjusted and updated. I'm very new to tkinter, matplotlib, and FigureCanvas so I'm probably doing something really dumb, but I can't seem to find other people with the same issue. Any help would be greatly appreciated!
source https://stackoverflow.com/questions/74174102/first-graph-is-erased-when-using-clear-but-second-graph-is-still-usable-graph
Comments
Post a Comment