'How to line plot timeseries data on a bar plot

I have the following data frame:

data = {'date': ['3/24/2020', '3/25/2020', '3/26/2020', '3/27/2020'],
        'Total1': [133731.9147, 141071.6383, -64629.74024, 647.5360108],
        'Total2': [133731.9147, 274803.5529, 210173.8127, 210821.3487]}

df = pd.DataFrame(data)

    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() is:

<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 is a cumulative total of total1. I would like to make a bar chart of total1 and overlay that with a line chart of total2.

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

That is what I am currently using for the bar chart.

I tried this after converting my date to 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()

enter image description here



Solution 1:[1]

  • Tested in python 3.10, pandas 1.4.1, matplotlib 3.5.1, seaborn 0.11.2

'date' as a str

  • This works because the tick locations of a bar plot are 0 indexed, and since the 'date' column values are strings, the line plot tick locations are also 0 indexed.
    • Check the locations and labels with p1.get_xticklabels()
p1 = sns.barplot(x="date", y="Total1", data=df)
p1.set_xticklabels(p1.get_xticklabels(), rotation=45)

# add lineplot to the same axes
p2 = sns.lineplot(data=df, x='date', y='Total2', marker='o', ax=p1)
p1.set(ylabel='Total', xlabel='Date')
plt.show()

enter image description here

'date' as a datetime dtype

  • p2 = sns.lineplot(data=df, x='date', y='Total2', marker='o') results in the following xtick locations:
    • p2.get_xticks() ? array([18345. , 18345.5, 18346. , 18346.5, 18347. , 18347.5, 18348. ]), which do not correspond to the 0 indexed xtick locations produced by a bar plot
  • Plot the line plot against the bar plot xtick locations, p1.get_xticks(), or use df.index(), as long as the index is a 0 indexed RangeIndex.
# convert date to a datetime dtype and extract only the date component
df['date'] = pd.to_datetime(df['date']).dt.date

p1 = sns.barplot(data=df, x='date', y='Total1')
p1.set_xticklabels(p1.get_xticklabels(), rotation=45)

# get the xtick locations
xticks = p1.get_xticks()

# plot the line to the xtick locs (or df.index)
p2 = sns.lineplot(data=df, x=xticks, y='Total2', marker='o', ax=p1)

p1.set(ylabel='Total', xlabel='Date')
plt.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1