Using Tkinter and matplotlib to draw a pie chart, tkintermatplotlib
When learning python, we will always use some common modules. Next I will explain in detail how to use two different methods to draw a pie chart.
First, use the canvas in [Tkinter] to draw a pie chart:
From tkinter import Tk, Canvasdef DrawPie (): # create windows = Tk () # Add the title windows. title ("Pie Chart") # Set canvas style Canvas = canvas (windows, height = 500, width = 500) # package the canvas into a window Canvas. pack () # Use create_arc of the canvas to draw a pie, (400,400) and (100,100) as the rectangle around the pie, # start = angle start, extent = degree of rotation, fill = filled color canvas. create_arc (400,400,100,100, start = 0, extent = 36, fill = "red") canvas. create_arc (400,400,100,100, start = 36, extent = 72, fill = "green") canvas. create_arc (400,400,100,100, start = 108, extent = 108, fill = "yellow") canvas. create_arc (400,400,100,100, start = 216, extent = 144, fill = "blue") # add content for each slice, with the center (250,250) canvas. create_text (430,200, text = "36 °", font = (" 文 ", 20) canvas. create_text (330,100, text = "72 °", font = (" 文 ", 20) canvas. create_text (90,200, text = "108 °", font = (" 文 ", 20) canvas. create_text (390,370, text = "144 °", font = (" 文 ", 20) # enable message loop windows. mainloop () if _ name _ = '_ main _': # Call method DrawPie ()
The above method is to use the Tkinter canvas to draw a pie chart. Next let's look at the pyplot in the third-party module matplotlib:
From matplotlib import pyplot # Chinese support for pyplot. rcParams ['font. sans-serif'] = ['simhei'] # used to display the Chinese tag pyplot normally. rcParams ['axes. unicode_minus '] = False # used to normally display the negative def showPieChart (): # Call the pie method in the pyplot module to draw a pie chart. The first parameter of the pie method is the proportion of each part, other parameters are some modified labels for the pie chart. labels is the description, startangle is the starting angle of the painting, and counterclock is the direction of the painting (the default value is) pyplot. pie ([108,144, 108], labels = ["36 °", "72 °", "144 °", "°"], startangle = 90, counterclock = False) # display the graph pyplot. show () if _ name _ = '_ main _': # Call the function showPieChart ()
In fact, the two methods are similar, but the application modules are different. The first method can only draw a graph, but cannot add the content of the pie chart. The second method encapsulates the pie chart style, various styles can be added.
The above example of using Tkinter and matplotlib to draw a pie chart is all the content that I share with you. I hope you can give us a reference and support for more.