我是Python和Xlwings的新手。我正在尝试创建一个UDF以在Excel中返回“箱形图”。 Excel中的表具有两列“ Sales_Region”和“ Total Sales”。
在运行以下代码时,出现错误“ Object required”。我认为该错误基本上是由于装饰器的使用不正确造成的。寻找帮助!
import seaborn as sns, numpy as np
import matplotlib.pyplot as plt
import xlwings as xw
import pandas as pd
from pandas import DataFrame
@xw.func
@xw.arg('df', pd.DataFrame, index = False, header=True)
@xw.arg('x_name', category = str)
@xw.arg('y_name', category = str)
def BOX_Plot(df,x_name,y_name):
dt = pd.DataFrame(df)
names = x_name
bill = y_name
sns.set_style("whitegrid")
ax = sns.boxplot(x=names, y=bill, data=dt)
medians = dt.groupby([names])[bill].median().values
median_labels = [str(np.round(s, 2)) for s in medians]
pos = range(len(medians))
for tick,label in zip(pos,ax.get_xticklabels()):
ax.text(pos[tick], medians[tick], median_labels[tick],
horizontalalignment='center', size='x-small', color='w', weight='semibold')
plt.show()
return "Done!"
答案 0 :(得分:0)
代码很完美,我忘了在VBA工具中添加Xlwings参考。