4

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

7
  • Do you use it in a notebook? Commented Feb 1, 2019 at 11:48
  • For me, it shows only the first graph (bar chart) on matplotlib 2.2.2 Commented Feb 1, 2019 at 12:03
  • 1
    According to a comment here it has something to do with the index. If I set use_index=False in both plot commands, both the bars and the line are visible, but then the xticks are just numbers from 0 to 22. Commented Feb 1, 2019 at 12:07
  • 4
    Pandas bar plots are categorical in nature, they put bars as successive integer positions. Hence to draw a line plot in the same axis, the line plot would need to be categorical as well. This can be achieved with use_index=False as commented above. So the solution is to replace the last line with df.sum(axis=1).plot(ax=ax, use_index=False). Commented Feb 1, 2019 at 13:26
  • 1
    I believe the most panda-esque way to deal with the problem is to use @ImportanceOfBeingErnest tip of use_index=False Commented Feb 1, 2019 at 14:17

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

2 Comments

I swear for a moment I thought the figure is a laptop ;)
If you are happy with this solution, consider accepting it (even though it's your own answer), so that other people know that the question needs no further attention.
-2

You can do it like that:

fig = plt.figure()
ax = df.plot.bar()
ax2 = ax.twinx()
ax2.plot(df.sum(axis=1).values)
plt.show()

df.plot.bar returns an axis you can use for the next plot:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html#pandas-dataframe-plot

Also discussed:

How to align the bar and line in matplotlib two y-axes chart?

Plot Pandas DataFrame as Bar and Line on the same one chart

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.