Попытка преобразовать функцию графика морского дна - PullRequest
0 голосов
/ 01 августа 2020

Я пытаюсь создать линейный график в seaborn, и приведенный ниже код отлично работает.

plt.figure(figsize=(50, 50))
fig1 = sns.lineplot(data=zrrgroupbysummary, x='filedate', y='Count', hue='location', 
style='location', linewidth=3.0)

plt.xlabel('Time')
plt.ylabel('Resource Count')
plt.title(f'Trend - By SBU\n {dt.date.strftime(fromdate, "%m/%d/%Y")} to {dt.date.strftime(todate, 
"%m/%d/%Y")}')
fig1.yaxis.set_minor_locator(MultipleLocator(5))
fig1.xaxis.set_minor_locator(WeekdayLocator(byweekday=MO))
fig1.grid(True, which='minor', lw=1, ls=':', color='darkgrey')
fig1.grid(True, which='major', lw=1, color='darkgrey')

Однако, когда я превращаю указанное выше в функцию и пытаюсь построить график, я получаю следующую ошибку

titlee = f'Trend - By SBU\n {dt.date.strftime(fromdate, "%m/%d/%Y")} to 
{dt.date.strftime(todate, 
"%m/%d/%Y")}'
Summary_Reporting('rrgroupbysummary',"'filedate'","'Count'","'location'", 
"'location'", titlee, 
'fig1')

Функция:

def Summary_Reporting(data, xaxis, yaxis, hue, style, titlee, fig):
    print(f'{fig} = sns.lineplot(data = {data}, x = {xaxis}, y={yaxis}, hue = 
    {hue}, style={style})')
    plt.figure()
    fig = sns.lineplot(data=data, x=xaxis, y=yaxis, hue=hue, style=style, 
    linewidth=3.0)
    plt.xlabel('Time')
    plt.ylabel('Resource Count')
    plt.title(titlee)
    fig.yaxis.set_minor_locator(MultipleLocator(5))
    fig.xaxis.set_minor_locator(WeekdayLocator(byweekday=MO))
    fig.grid(True, which='minor', lw=1, ls=':', color='darkgrey')
    fig.grid(True, which='major', lw=1, color='darkgrey')

Я получаю следующее сообщение об ошибке:

fig1 = sns.lineplot(data = rrgroupbysummary, x = 'filedate', y='Count', 
hue = 'location', style='location')
Traceback (most recent call last):
File "E:/PYTHON/PyCharm/Python/ZRR_modified.py", line 142, in <module>
Summary_Reporting('rrgroupbysummary',"'filedate'","'Count'","'location'", 
"'location'", titlee, 'fig1')
File "E:/PYTHON/PyCharm/Python/ZRR_modified.py", line 35, in 
Summary_Reporting
fig = sns.lineplot(data=data, x=xaxis, y=yaxis, hue=hue, style=style, 
linewidth=3.0)
File "C:\Users\sunil\AppData\Local\Programs\Python\Python38-32\lib\site- 
packages\seaborn\relational.py", line 1120, in lineplot
p = _LinePlotter(
File "C:\Users\sunil\AppData\Local\Programs\Python\Python38-32\lib\site- 
packages\seaborn\relational.py", line 694, in __init__
plot_data = self.establish_variables(
File "C:\Users\sunil\AppData\Local\Programs\Python\Python38-32\lib\site- 
packages\seaborn\relational.py", line 126, in establish_variables
x = data.get(x, x)
AttributeError: 'str' object has no attribute 'get'
Process finished with exit code 1

Если вы видите, что оператор печати в порядке, но все еще не уверен, почему ошибка

1 Ответ

0 голосов
/ 01 августа 2020

Ваша проблема в том, что вы передаете в функцию имя фрейма данных, а не сам фрейм данных.

Вы должны писать (обратите внимание на отсутствие кавычек "'"):

Summary_Reporting(rrgroupbysummary,"filedate","Count","location", "location", titlee, fig1)

вместо:

Summary_Reporting('rrgroupbysummary',"'filedate'","'Count'","'location'", "'location'", titlee, 'fig1')
...