Issue
I am trying to creating a legend in my graphic but It is not working when I use legend().I don't know what can be more. I saw many information about it and I am doing the same, but It keep not working. Could you help me please.
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')
fig2, avail = plt.subplots(figsize=(10,7), dpi=60, facecolor= '#e9eef0',)
avail.tick_params(axis='y', labelsize=0)#Formatar a fonte yLabel
avail.set_ylabel('FOLLOWING THE PRODUCTION', fontsize=20)
avail.set_xlabel('TIME (minute)', fontsize=20)
avail.legend(['ON','PLANNED','STOP'])
stop = [30]
on = [40]
planned = [70]
x = 1
w = 0.2
avail.barh(x + w, on ,w, color='#22e3e3', edgecolor = 'black', linewidth = 2)
avail.barh(x, planned , w ,color='#05337d', edgecolor = 'black', linewidth = 2)
avail.barh(x - w, stop, w, color = '#c95924', edgecolor = 'black', linewidth = 2)
#criando figura para plotar dentro do TKinter
canvas_avail = FigureCanvasTkAgg(fig2, master=root)
canvas_avail.get_tk_widget().place(x= 280, y= 150)
root.mainloop()
Solution
After making some changes and after you pointed out the error here's another code and this time its working and also showing the legends:
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root = tk.Tk()
root.geometry('600x450')
fig2, avail = plt.subplots(figsize=(10,7), dpi=60, facecolor='#e9eef0',)
avail.tick_params(axis='y', labelsize=0)
avail.set_ylabel('FOLLOWING THE PRODUCTION', fontsize=20)
avail.set_xlabel('TIME (minute)', fontsize=20)
stop = [30]
on = [40]
planned = [70]
x = 1
w = 0.2
avail.barh(x + w, on, w, color='#22e3e3', edgecolor='black', linewidth=2)
avail.barh(x, planned, w, color='#05337d', edgecolor='black', linewidth=2)
avail.barh(x - w, stop, w, color='#c95924', edgecolor='black', linewidth=2)
# Legend added after creating bars
avail.legend(['ON', 'PLANNED', 'STOP'])
#criando figura para plotar dentro do TKinter
canvas_avail = FigureCanvasTkAgg(fig2, master=root)
canvas_avail.get_tk_widget().place(x=10, y=0)
root.mainloop()
Proof:
Answered By - AshhadDevLab
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.