Seaborn Bar Bar Overlay с линейным графиком - PullRequest
0 голосов
/ 31 марта 2020

У меня есть следующий фрейм данных:

    date        Total1          Total2
0   3/24/2020   133731.9147     133731.9147
1   3/25/2020   141071.6383     274803.5529
2   3/26/2020   -64629.74024    210173.8127
3   3/27/2020   647.5360108     210821.3487

df.info ():

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
date      4 non-null object
Total1     4 non-null float64
Total2    4 non-null float64
dtypes: float64(2), object(1)
memory usage: 168.0+ bytes

total2 - совокупный итог total1. Я хотел бы сделать гистограмму total1 и наложить ее на линейный график total2.

ax = sns.barplot(x="date",y="NetPL",data=gby)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)

Это то, что я сейчас использую для гистограммы.

Я пробовал это после преобразования моей даты в дату и время

plt.style.use('ggplot')
ax =sns.barplot(x="date", y="Total1", data=df)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)

# add lineplot
sns.lineplot(x='date', y='Total2', data=df, marker='o')
plt.show()

enter image description here

1 Ответ

0 голосов
/ 31 марта 2020

date не преобразуется в формат даты и времени

plt.style.use('ggplot')
ax = sns.barplot(x="date", y="Total1", data=df)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)

# add lineplot
sns.lineplot(x='date', y='Total2', data=df, marker='o')
plt.show()

enter image description here

с date в качестве индекса даты и времени

df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)


plt.style.use('ggplot')
ax = sns.barplot(x=df.index, y="Total1", data=df)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
ax.xaxis.set_major_formatter(plt.FixedFormatter(df.index.to_series().dt.strftime("%Y-%m-%d")))

# use the ax labels as the x axis for the lineplot
locs, labels = plt.xticks()
label_t = [x.get_text() for x in labels]

sns.lineplot(x=label_t, y='Total2', data=df, marker='o')

plt.ylabel('Totals')
plt.show()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...