Issue
I am trying to edit my axis's graphic, but It is not working when I use plt.yticks(\[\])
. The program is showing me the values yet. Could you help me to edit, please? Maybe the problem is because I am using canvas, I tried to put yticks(\[\])
inside him but even so not working.
Below my code:
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import animation
import random
# Create the Tkinter window
root = tk.Tk()
root.geometry('400x100')
fig1, b1 = plt.subplots(figsize=(10, 0.5), dpi = 80,)
b1 = fig1.add_subplot(111)
fig2, b2 = plt.subplots(figsize= (10, 0.5), dpi = 80)
b2 = fig2.add_subplot(111)
def grafico_1 (i):
global b1
global nume
global valu
global fig1
global y_fix
global x
x = []
y_fix = []
ws = ['WIRE']
for valu in range (1, 6):
nume = random.randint(1, 10)
x.append(nume)
#print(x)
#Primeira camada do grafico
if int(x[0]) < 3:
y_fix.append(int(x[0]), )
#print(f'Lista de y_n ,e {y_n}')
b1.barh(ws, int(x[0]), color = 'green')
if int(x[0]) >= 3 and int(x[0]) < 6:
y_fix.append(x[0])
b1.barh(ws, int(x[0]), color = 'yellow' )
if int(x[0]) >= 6:
y_fix.append(x[0])
b1.barh(ws, int(x[0]), color= 'red' )
#Segunda camada:
for c in x[1:]:
if int(c) < 3 :
b1.barh(ws, int(c), color= 'green', left=y_fix)
y_fix.append(int(c))
jump = sum(y_fix)
y_fix = []
y_fix.append(jump)
if int(c) > 3 and int(c) < 5:
b1.barh(ws, int(c), color='yellow', left= y_fix)
y_fix.append(int(c))
jump = sum(y_fix)
y_fix = []
y_fix.append(jump)
if int(c) >= 5:
b1.barh(ws, int(c), color='red', left= y_fix)
y_fix.append(int(c))
jump = sum(y_fix)
y_fix = []
y_fix.append(jump)
def grafico_2 (i2):
global b2
global nume2
global valu2
global fig2
global y_fix2
global x2
x2 = []
y_fix2 = []
ws2 = ['line 2']
for valu2 in range (1, 6):
nume2 = random.randint(1, 10)
x2.append(nume2)
#print(f'Grafico 2 {x2}')
#Primeira camada do grafico
if int(x2[0]) < 3:
y_fix2.append(int(x2[0]))
#print(f'Lista de y_n ,e {y_n}')
b2.barh(ws2, int(x2[0]), color = 'green')
if int(x2[0]) >= 3 and int(x2[0]) < 6:
y_fix2.append(x2[0])
b2.barh(ws2, int(x2[0]), color = 'yellow' )
if int(x2[0]) >= 6:
y_fix2.append(x2[0])
b2.barh(ws2, int(x2[0]), color= 'red' )
#Segunda camada:
for c2 in x2[1:]:
if int(c2) < 3 :
b2.barh(ws2, int(c2), color= 'green', left=y_fix2)
y_fix2.append(int(c2))
jump2 = sum(y_fix2)
y_fix2 = []
y_fix2.append(jump2)
if int(c2) > 3 and int(c2) < 5:
b2.barh(ws2, int(c2), color='yellow', left= y_fix2)
y_fix2.append(int(c2))
jump2 = sum(y_fix2)
y_fix2 = []
y_fix2.append(jump2)
if int(c2) >= 5:
b2.barh(ws2, int(c2), color='red', left= y_fix2)
y_fix2.append(int(c2))
jump2 = sum(y_fix2)
y_fix2 = []
y_fix2.append(jump2)
#plt.show()
ani1 = animation.FuncAnimation(fig1, grafico_1, interval = 3000, frames=100)
ani2 = animation.FuncAnimation(fig2, grafico_2, interval = 3000, frames=100)
# Embed the figures into Tkinter using FigureCanvasTkAgg
canvas1 = FigureCanvasTkAgg(fig1, master=root)
canvas1.get_tk_widget().place(x= 300, y= 460)
canvas2 = FigureCanvasTkAgg(fig2, master=root)
canvas2.get_tk_widget().place(x= 300, y = 530)
root.mainloop()
Create graphic without any numbers in the y axis
Solution
Here's how you can address the issue:
Set the y-ticks for each subplot directly: You need to call the
set_yticks()
method on the specific axes objects you want to modify (b1
andb2
in your case).Ensure consistent axes usage: It looks like you're redefining
b1
andb2
after creating them. When you useb1 = fig1.add_subplot(111)
, it's not necessary to createfig1, b1 = plt.subplots()
beforehand; you can directly create the subplot.
Here an updated code example:
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import animation
import random
# Create the Tkinter window
root = tk.Tk()
root.geometry('400x100')
fig1 = plt.Figure(figsize=(10, 0.5), dpi=80)
b1 = fig1.add_subplot(111)
fig2 = plt.Figure(figsize=(10, 0.5), dpi=80)
b2 = fig2.add_subplot(111)
# ... [rest of your code remains the same until the grafico_1 and grafico_2 functions]
def grafico_1(i):
global b1
# ... [rest of your code for grafico_1]
# Clear previous axes and set y-ticks to an empty list
b1.clear()
b1.set_yticks([])
# ... [rest of your plotting code for grafico_1]
def grafico_2(i2):
global b2
# ... [rest of your code for grafico_2]
# Clear previous axes and set y-ticks to an empty list
b2.clear()
b2.set_yticks([])
# ... [rest of your plotting code for grafico_2]
# ... [rest of your code for animations and canvas]
root.mainloop()
Answered By - Jake Zappin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.