看起來挺簡單的問題,還真就搞不定了。
根據資料畫圖表,分別畫柱狀圖和餅圖,互相切換后,柱狀圖的坐標軸就不顯示了,求助!



代碼如下:
#-*- codeing = utf-8 -*-
from tkinter import messagebox
from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk #NavigationToolbar2TkAgg
name_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
num_list = [33, 44, 53, 6,11, 7, 7, 10, 3, 1]
class drawTest:
def __init__(self):
self.root = Tk()
self.root.title('畫圖測驗')
self.root.geometry("800x500")
self.btn_bigdata01=Button(self.root,text = 'a1',command=self.bigdata_draw01).place(x=20, y=20, width=80)
self.btn_bigdata02=Button(self.root,text = 'a2',command=self.bigdata_draw02).place(x=20, y=70, width=80)
self.frame = Frame(self.root,width=300,height=200,bg='blue')
self.frame.place(x=150, y=50)
fig,self.ax = plt.subplots(figsize=(6,4))
self.canvs = FigureCanvasTkAgg(fig, self.frame)
self.canvs.get_tk_widget().pack()
def bigdata_draw01(self):
self.ax.clear()
x = name_list
y = num_list
self.ax.bar(x, y)
self.ax.axis('auto')
self.ax.spines['left'].set_visible(True)
self.ax.spines['bottom'].set_visible(True)
self.ax.spines['right'].set_visible(False)
self.ax.spines['top'].set_visible(False)
#self.ax.spines['left'].set_color('red')
self.ax.xaxis.set_ticks_position('bottom')
self.ax.spines['left'].set_linewidth(1)
self.ax.spines['bottom'].set_linewidth(1)
#self.ax.spines['left'].set_linestyle('--')
self.canvs.draw()
def bigdata_draw02(self):
self.ax.clear()
bing_data=https://bbs.csdn.net/topics/num_list
bing_label=name_list
explode=(0,0,0,0,0,0,0,0,0,0.1)
self.ax.pie(bing_data,explode=explode,labels=bing_label)
self.ax.axis('equal')
self.canvs.draw()
def main():
a = drawTest()
mainloop()
if __name__ == '__main__':
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/34971.html
下一篇:Python中attributeerror錯誤如何解決“attributeerror:'str' object has no attribute 'x_l“
