Issue
I am trying to increase my Y font from my graphic but it is not working.
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()
Solution
After you have added your data to the axes, but before you embed the figure into the Tkinter canvas, add a line to set the y-axis label font size like this:
b1.set_yticklabels(b1.get_yticklabels(), fontsize=21)
In case this is not working, you can check two things:
print(dir(b1))
to check which methods are available for it. In case you have accidentally overwritten or modified b1 somewhere else in your code than the part that you showed, it might not be recognized anymore that it is an axis, in which case you will have more problems regarding the methods that should be available.print(matplotlib.__version__)
to see if you are using an older version of matplotlib, in which some methods are not available.
A quick fix would be to use this line instead, which should work in older versions of matplotlib as well:
b1.tick_params(axis='y', labelsize=21)
Answered By - Miles
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.