Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - Bar and Line Chart - Datetime axis

I am trying to plot a bar chart and a line overlay, where my data has datetime as an index. This is the code:

import pandas as pd
import numpy as np
dates = pd.date_range('2019-01-01', '2019-01-31', freq='B')
df = pd.DataFrame(index=dates, 
              columns=['a', 'b', 'c'],
              data = np.random.randn(len(dates), 3))

fig, ax = plt.subplots()
df.plot.bar(ax=ax)
df.sum(axis=1).plot(ax=ax)

Unfortunately, it only ends up showing the last chart requested. enter image description here

I'm using

python 3.6.8
pandas 0.24.0
matplotlib 3.0.2

Regards

like image 298
Tanguy Bretagne Avatar asked Sep 03 '25 02:09

Tanguy Bretagne


1 Answers

Following the comment by @ImportanceOfBeingErnest I think the best way to answer this is to use the use_index=False kwarg. For the data format etc, this is another problem and depends on what one wants to achieve.

import pandas as pd
import numpy as np
dates = pd.date_range('2019-01-01', '2019-01-31', freq='B')
df = pd.DataFrame(index=dates,
                  columns=['a', 'b', 'c'],
                  data = np.random.randn(len(dates), 3))

fig, ax = plt.subplots()
df.plot.bar(ax=ax)
df.sum(axis=1).plot(ax=ax, use_index=False)

enter image description here

like image 174
Tanguy Bretagne Avatar answered Sep 04 '25 15:09

Tanguy Bretagne