程式從excel檔案讀取資料,設定好輸出路徑,點擊最下面一個按鈕,即可生成圖形。
源資料(類似于下面的xlsx檔案):

代碼如下:
import tkinter as tk
import tkinter.filedialog as fd
import plotly as py
import plotly.graph_objs as go
import openpyxl
import pandas as pd
import os
class App(tk.Tk):
def __init__(self):
super().__init__()
btn_file = tk.Button(self, text="Step1:Import data from", command=self.choose_file)
btn_dir = tk.Button(self, text="Step2:Set output path",
command=self.choose_directory)
btn_start = tk.Button(self, text="Step3:Start to plot", command=self.start)
btn_file.pack(padx=60, pady=10)
btn_dir.pack(padx=60, pady=10)
btn_start.pack(padx=60, pady=10)
def choose_file(self):
filetypes = (("Excel files", "*.xlsx"),
)
filename = fd.askopenfilename(title="Open file",
initialdir="/", filetypes=filetypes)
if filename:
print(filename)
def choose_directory(self):
directory = fd.askdirectory(title="Open directory",
initialdir="/")
if directory:
print(directory)
def start(self,filename,directory):
self.filename = filename
self.directory = directory
self.SP(filename, directory)
def SP(self, input_file, output_dir):
self.input_file = input_file
self.output_dir = output_dir
wb = openpyxl.load_workbook(self.input_file)
sheet = wb['Sheet1']
row_max = sheet.max_row
col_max = sheet.max_column
first_row_list = []
first_col_list = []
for col_n in range(2, col_max+1):
first_row_list.append(sheet.cell(row=1, column=col_n).value)
for row_n in range(2,row_max+1):
first_col_list.append(sheet.cell(row=row_n, column=1).value)
data_all = pd.read_excel(self.input_file)
data_selected = data_all.loc[:,first_row_list]
df = pd.DataFrame(data_selected)
df.index = first_col_list
colors = ['rgb(150,204,90)','rgb(255, 130, 71)','rgb(255, 193, 37)','rgb(180,240,190)','rgb(255, 10, 1)',
'rgb(25, 19, 3)','rgb(100, 100, 100)','rgb(45,24,200)','rgb(33, 58, 108)','rgb(35, 208, 232)']
data = [go.Scatter(
x=df.columns,
y=[country]*len(df.columns),
mode='markers+text',
marker=dict(
color=colors[num],
size= df.loc[country],
showscale = False,
),
text=list(map(str, df.loc[country])),
textposition='middle center',
)
for num, country in enumerate(reversed(df.index))
]
layout = go.Layout(plot_bgcolor='rgb(10, 10, 10)',
paper_bgcolor='rgb(20, 55, 100)',
font={
'size': 15,
'family': 'sans-serif',
'color': 'rgb(255, 255, 255)'
},
width=1000,
height=800,
xaxis=dict(
title='Output of grapes per year in different countries',
nticks = col_max+1,
type = 'category',
),
showlegend=False,
margin=dict(l=100, r=100, t=100, b=100),
hovermode = False,
)
fig = go.Figure(data=https://bbs.csdn.net/topics/data, layout=layout)
py.offline.plot(fig, filename=os.path.join(self.output_dir,'bubble_diagram.html'))
if __name__ == "__main__":
app = App()
app.mainloop()
運行后,前面兩步正常,點擊最后一個按鈕后,系統報錯:
TypeError: start() missing 2 required positional arguments: 'filename' and 'directory'
請問如何修改?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/24534.html
