Home > Backend Development > Python Tutorial > How to convert legend to Matplotlib TKinter

How to convert legend to Matplotlib TKinter

WBOY
Release: 2024-02-22 14:10:03
forward
613 people have browsed it

如何将图例Matplotlib TKinter

Question content

I'm trying to create a legend in a graph but it doesn't work when I use legend(). I don't know what else. I've seen a lot of information about it and I'm doing the same thing but it hasn't worked. Can you help me?

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()
Copy after login


Correct answer


After making some changes and pointing out the error, here is another code and this time it works and displays the legend:

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()
Copy after login

prove:

The above is the detailed content of How to convert legend to Matplotlib TKinter. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template