Skip to main content

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

Popular posts from this blog

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

The number of nodes and edges I calculated manually and by a method provided by networkx library dont match

I am trying to analyse a graph using networkx library in Python. I have implemented a function that returns the nodes and edges of that graph. After that I counted the number of nodes and edges by implementing the len() function for nodes and edges, which gave me 17 for both nodes and edges. However, when I used the method graph.number_of_nodes() of networkx library it gave me 53. My code: import pandas as pd import networkx as nx import matplotlib.pyplot as plt header_list = ["a","b","w"] E =pd.read_csv("aves-barn-swallow-contact-network.edges",sep=" ",header= None,names=header_list) # print(E.head()) # prints first few rows of the csv file G = nx.from_pandas_edgelist(E,"a","b",["w"]) nx.draw(G) plt.show() class GraphAnalyse: #finding nodes and edges def nodeEdge(self,Graph): nodes = Graph.nodes() edges = Graph.edges() ...