我有以下数据框:
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)
这就是我当前用于条形图的内容。
我将日期转换为datetime后尝试了此操作
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()
答案 0 :(得分:0)
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()
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()