我正在尝试在图形中创建图例,但当我使用 legend() 时它不起作用。我不知道还能有什么。我看到了很多关于它的信息,我也在做同样的事情,但它一直不起作用。你能帮我一下吗?
在我的代码下面:
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()
进行一些更改并指出错误后,这是另一个代码,这次它可以工作并显示图例:
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()
证明:
以上是如何将图例Matplotlib TKinter的详细内容。更多信息请关注PHP中文网其他相关文章!