Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating stacked histogram with pandas dataframes data python

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

like image 604
ccsv Avatar asked Jul 06 '14 09:07

ccsv


People also ask

How do I make a histogram in pandas Dataframe?

In order to plot a histogram using pandas, chain the . hist() function to the dataframe. This will return the histogram for each numeric column in the pandas dataframe.

How do you make a dataset histogram in Python?

To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and count the values which fall into each of the intervals. Bins are clearly identified as consecutive, non-overlapping intervals of variables. The matplotlib. pyplot.


1 Answers

The method in that post should work:

plt.hist([df1['text'],df2['printed']],
          bins=100, range=(1,100), stacked=True, color = ['r','g'])
like image 156
Happy001 Avatar answered Sep 21 '22 14:09

Happy001