17

I am trying to create a stacked histogram with data from 2 or more uneven pandas dataframes? So far I can get them to graph on top of each other but not stack.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('dert.csv', encoding = "ISO-8859-1", index_col=0)

df1['text'] = df['text'].dropna(subset=['five'])
df2['printed'] = df['text2']
ax = df1['text'].hist( bins=100, range=(1,100), stacked=True, color = 'r')
ax = df2['printed'].hist(bins=100, range=(1,100), stacked=True, color = 'g')

plt.setp(ax.get_xticklabels(),  rotation=45)
plt.show()

How do I get them to stack?

I found a solution but it does not use pandas dataframes Matplotlib, creating stacked histogram from three unequal length arrays

Community
  • 1
  • 1
ccsv
  • 8,188
  • 12
  • 53
  • 97
  • Have you tried using the same method as that link you posted? Using regular matplotlib, just passing the DataFrame columns you want to plot, should work. At most, you can create a list of each column's values. – RJT Jul 06 '14 at 09:44

1 Answers1

27

The method in that post should work:

plt.hist([df1['text'],df2['printed']],
          bins=100, range=(1,100), stacked=True, color = ['r','g'])
smci
  • 32,567
  • 20
  • 113
  • 146
Happy001
  • 6,103
  • 2
  • 23
  • 16
  • Be sure to [remove nan's](https://stackoverflow.com/questions/20656663/matplotlib-pandas-error-using-histogram) before using this. – Josiah Yoder Jul 13 '18 at 18:11