0

I seem to be having an issue subplotting a line and grouped bar graph using pandas.DataFrame.plot() with a datetime index.

Everything works as expected when I do not use a datetime index:

df = pd.DataFrame({'A': [2, 4, 6, 8], 'B': [1, 3, 5, 7]}) 
df.plot(kind='bar')
df.sum(axis=1).plot(kind='line')

enter image description here

However, when I change the index to datetime, it does not plot the line graph on top of the bar graph but the y-axis is adjusted as if the line graph data was present.

index = pd.to_datetime(['2022-01-01', '2022-01-02',
                        '2022-01-03', '2022-01-04']).date

df2 = pd.DataFrame({'A': [2, 4, 6, 8], 'B': [1, 3, 5, 7]}, index=index)
df2.plot(kind='bar')
df2.sum(axis=1).plot(kind='line')

enter image description here

Is there a simple way using pandas to plot a line graph on top of a grouped bar graph with a datimetime index. It works if I convert the index to strings: index = ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04']

I could of course use plotly or another plotting library but curious if there is a workaround for df.plot() with a datetime index.

Using pandas 1.3.3 and python 3.8.10

It_is_Chris
  • 11,310
  • 2
  • 21
  • 36

0 Answers0