How can I avoid this while True
Loop. I have to run def func(i)
infinite times. The reason for avoiding this while True
loop is that whenever I create two classes, one class having this piece of code and another class having a button to access this piece of code when button is pressed, but the problem I get is that I cannot integrate this matplotlib
within the tkinter window. The button is shown separately and the matplotlib is running separately. But when I remove while loop then it somehow solves the problem but then there is no infinite loop.
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import numpy as np
import tkinter as tk
from tkinter import *
import matplotlib.animation as animation
j=0
fig = plt.figure()
ax1 = fig.add_axes([0.85, 0.093, 0.04, 0.8])
cax = fig.add_subplot(1, 1, 1)
H = np.array([[1, 2, 3, 1], [4, 5, 6, 10], [3, 7, 8, 4], [10, 5, 3, 1]])
Z = np.array([[3, 290, 600], [1011, 230, 830], [152, 750, 5]])
while True:
def func(i):
global j
if j == 0:
j += 1
rows, cols = H.shape
im = plt.imshow(H, interpolation='nearest',
extent=[0, cols, 0, rows],
cmap='bwr', vmin=0, vmax=10)
fig.colorbar(im, cax=ax1, orientation='vertical')
elif j == 1:
j -= 1
rows, cols = H.shape
im = plt.imshow(Z, interpolation='nearest', cmap='Spectral', vmin=0, vmax=1023,extent=[0, cols, 0, rows])
v = np.linspace(0, 1023, 15, endpoint=True)
fig.colorbar(im, cax=ax1, orientation='vertical', ticks=v)
ani = animation.FuncAnimation(fig, func, interval=1000)
plt.show()
plt.close()