123
123

Reputation: 101

Why are bars missing in my stacked bar chart -- Python w/matplotlib

all.

I am trying to create a stacked bar chart built using time series data. My issue -- if I plot my data as time series (using lines) then everything works fine and I get a (messy) time series graph that includes correct dates. However, if I instead try to plot this as a stacked bar chart, my dates disappear and none of my bars appear.

I have tried messing with the indexing, height, and width of the bars. No luck.

Here is my code:

import pylab
import pandas as pd
import matplotlib.pyplot as plt

df1= pd.read_excel('pathway/filename.xls')
df1.set_index('TIME', inplace=True)


ax = df1.plot(kind="Bar", stacked=True)
ax.set_xlabel("Date")
ax.set_ylabel("Change in Yield")
df1.sum(axis=1).plot( ax=ax, color="k", title='Historical Decomposition -- 1 year -- One-Quarter Revision')
plt.axhline(y=0, color='r', linestyle='-')
plt.show()

If i change ax = df1.plot(kind="Bar", stacked=True)

to ax = df1.plot(kind="line", stacked=False)

I get:

if instead I use ax = df1.plot(kind="Bar", stacked=True)

I get:

Any thoughts here?

Upvotes: 0

Views: 1470

Answers (1)

teoeme139
teoeme139

Reputation: 412

Without knowing what the data looks like, I'd try something like this:

#Import data here and generate DataFrame

print(df.head(5))

               A     B     C     D
DATE
2020-01-01 -0.01  0.06  0.40  0.45
2020-01-02 -0.02  0.05  0.39  0.42
2020-01-03 -0.03  0.04  0.38  0.39
2020-01-04 -0.04  0.03  0.37  0.36
2020-01-05 -0.05  0.02  0.36  0.33

f, ax = plt.subplots()
ax.bar(df.index, df['A'])
ax.bar(df.index, df['B'])
ax.bar(df.index, df['C'], bottom=df['B'])
ax.plot(df.index, df['D'], color='black', linewidth=2)
ax.set_xlabel('Date')
ax.set_ylabel('Change in Yield')
ax.axhline(y=0, color='r')
ax.set_xticks([])
ax.legend()
plt.show()

enter image description here

Edit:: Ok, I've found a way looking at this post here: Plot Pandas DataFrame as Bar and Line on the same one chart

Try resetting the index so that it is a separate column. In my example, it is called 'DATE'. Then try:

ax = df[['DATE','D']].plot(x='DATE',color='black')
df[['DATE','A','B','C']].plot(x='DATE', kind='bar',stacked=True,ax=ax)
ax.axhline(y=0, color='r')
ax.set_xticks([])
ax.set_xlabel('Date')
ax.set_ylabel('Change in Yield')
ax.legend()
plt.show()

enter image description here

Upvotes: 1

Related Questions