0
votes

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?

1
Can you share a few lines of data?teoeme139
Bars are there, but they are in the range 0 to N-1, while the lines are in the range around 2000. You can use matplotlib bars (plt.bar), which are numeric instead.ImportanceOfBeingErnest
@ImportanceOfBeingErnest -- Am i able to do this when the values of my time series are sometimes negative and sometimes positive? Based on what I am reading, to stack bars using plt.bar() I have to tell Python to treat the top of one bar as the bottom of another. This seems like it will cause issues.123
I don't currently see any issue with that, but if you encounter one you can always ask about it. (Remember to follow minimal reproducible example)ImportanceOfBeingErnest
@ImportanceOfBeingErnest -- It creates massive issues if I do it this way because of the fact that the times series all contain both negative and positive values. Thanks, though. I will just keep trying to figure it out.123

1 Answers

1
votes

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