0

I'm creating a stacked line chart using the code below. Right now I'm plotting data from a pandas data frame. Using the code as below, I'm feeding in a list of 3 headers for variables in that df that interest me, and I'm happy with what I've got so far. However, I'd like to feed in more than 3 variables (about 80). So I'm looking for a reliable way to loop this to avoid typing in each of 80 lines, but I haven't figured it out yet.

Any simple ideas to accomplish this?

Thanks!

import matplotlib.pyplot as plt
df['Date'] = pd.to_datetime(df['Date'])
df.index = df['Date'] 

y1 = df[headers_list[0]].resample('m').sum()
y2 = df[headers_list[1]].resample('m').sum()
y3 = df[headers_list[2]].resample('m').sum()
x = list(range(len(y1)))   
y = np.vstack([y1, y2, y3])
labels = [headers_list[0], headers_list[1], headers_list[2]]
fig, ax = plt.subplots(figsize=(20,6))
ax.stackplot(x, y1, y2, y3, labels=labels)
ax.legend(loc=2)
plt.show()
Kees
  • 451
  • 1
  • 8
  • 17
  • 1
    If I understand what you are asking, there is a dataframe method that does this for you: http://pandas.pydata.org/pandas-docs/version/0.23/visualization.html#area-plot. I think the example code you provided could be a little more clear. If you haven't read http://sscce.org give it a read through. – Tyler Nickerson Jul 12 '18 at 17:06
  • For pandas, [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) is probably a useful read. – ImportanceOfBeingErnest Jul 12 '18 at 21:14

0 Answers0