#pandas #matplotlib #seaborn
#pandas #matplotlib #seaborn
Вопрос:
У меня есть небольшой набор данных, который содержит ежемесячные наблюдения за три года. Я хотел бы наложить ежемесячные наблюдения за год в виде отдельных строк на одном рисунке как в Seaborn, так и в pyplot, но у меня не получается.
Мои данные:
df3.to_json()
'{"year":{"0":2016,"1":2016,"2":2016,"3":2016,"4":2016,"5":2016,"6":2016,"7":2016,"8":2016,"9":2016,"10":2016,"11":2016,"12":2017,"13":2017,"14":2017,"15":2017,"16":2017,"17":2017,"18":2017,"19":2017,"20":2017,"21":2017,"22":2017,"23":2017,"24":2018,"25":2018,"26":2018,"27":2018,"28":2018,"29":2018,"30":2018,"31":2018,"32":2018,"33":2018,"34":2018,"35":2019,"36":2019},"month":{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7,"7":8,"8":9,"9":10,"10":11,"11":12,"12":1,"13":2,"14":3,"15":4,"16":5,"17":6,"18":7,"19":8,"20":9,"21":10,"22":11,"23":12,"24":1,"25":3,"26":4,"27":5,"28":6,"29":7,"30":8,"31":9,"32":10,"33":11,"34":12,"35":1,"36":2},"Total_Revenue":{"0":14459.68,"1":17051.84,"2":13647.02,"3":30691.44,"4":21202.59,"5":176698.9599999996,"6":447987.5399999983,"7":495101.9599999956,"8":1154867.9500000139,"9":1429084.9000000353,"10":1495052.6800000321,"11":1081255.0200000051,"12":1012744.2600000009,"13":1150345.1400000018,"14":1500781.3300000376,"15":1537148.650000029,"16":2360964.3900000644,"17":2175135.4800000801,"18":2239826.9900001031,"19":1605135.7600000571,"20":1783597.480000068,"21":1983734.2900000883,"22":1872206.5000000726,"23":1360688.5700000362,"24":1174347.0199999958,"25":1427403.4700000156,"26":1704650.3800000662,"27":2435791.9500000449,"28":2875867.6099999412,"29":2619157.7499999623,"30":1995412.9400000239,"31":2833372.5600000559,"32":4946981.0699994983,"33":4541426.7399997683,"34":3790844.0199999111,"35":3177924.9399999017,"36":2695695.8799999873},"Average_Revenue_per_Transaction":{"0":12.5627106864,"1":13.3950039277,"2":8.9901317523,"3":12.3556521739,"4":12.0469261364,"5":43.8458957816,"6":55.0759208262,"7":55.4487579796,"8":54.5160474887,"9":54.9944162241,"10":53.1460907895,"11":48.8019055786,"12":55.1633672858,"13":54.9773054865,"14":51.6958192966,"15":49.4657650845,"16":50.4371798761,"17":50.4461125284,"18":48.8831730685,"19":49.026748931,"20":47.590519238,"21":50.1348132329,"22":47.4456791688,"23":44.2990158224,"24":47.9971806924,"25":46.9942539672,"26":46.5853295802,"27":48.6973340131,"28":49.3711177682,"29":46.531369919,"30":41.9671680653,"31":25.369093351,"32":16.7093304083,"33":17.4069051507,"34":15.3087479505,"35":16.8375804811,"36":15.9871891161}}'
Seaborn:
sns.lmplot(x = 'month', y = 'Total_Revenue', data = df3.query('year == 2016'), size = 10, aspect = 2,
legend = '2016 : Total Revenue')
sns.lmplot(x = 'month', y = 'Total_Revenue', data = df3.query('year == 2017'), size = 10, aspect = 2,
legend = '2017 : Total Revenue')
sns.lmplot(x = 'month', y = 'Total_Revenue', data = df3.query('year == 2018'), size = 10, aspect = 2,
legend = '2018 : Total Revenue')
sns.lmplot(x = 'month', y = 'Total_Revenue', data = df3.query('year == 2019'), size = 10, aspect = 2,
legend = '2019 : Total Revenue')
plt.legend()
Сообщение об ошибке:
No handles with labels found to put in legend.
Графики отображаются в виде отдельных графиков, расположенных один под другим.
Pandas — pyplot
plt.plot(x = df3.query('year == 2016')['month'], y = df3.query('year == 2016')['Total_Revenue'],
label = '2016 : Total Revenue', color = 'red')
plt.plot(x = df3.query('year == 2017')['month'], y = df3.query('year == 2017')['Total_Revenue'],
label = '2017 : Total Revenue', color = 'blue')
plt.plot(x = df3.query('year == 2018')['month'], y = df3.query('year == 2018')['Total_Revenue'],
label = '2018 : Total Revenue', color = 'orange')
plt.legend()
Отображается пустой рисунок.
Сообщение об ошибке:
No handles with labels found to put in legend.
Я не понимаю значения этого сообщения.
Комментарии:
1.
plt.plot
не предоставляетx
иy
в качестве ключевого аргумента. Итак, вместоplt.plot(x=df.col1, y=df.col2)
вам нужноplt.plot(df.col1, df.col2)
.2. Спасибо. Действительно, это работает. Как я мог бы сделать то же самое с lmplot от seaborn?
3. Каждый вызов
lmplot
создает новый рисунок. Если это нежелательно, вы не можете использоватьlmplot
. Или вы используетеyear
в качествеhue
аргумента внутри одногоlmplot
вызова.