Examples of how to create a histogram from a dataframe using pandas in python
Table of contents
Create a dataframe with pandas
To start, lets create a simple dataframe with pandas:
import pandas as pdimport matplotlib.pyplot as pltdata = {'c':['a','a','a','b','b','b','a','a','b'],'v1':[1,1,2,3,4,4,4,5,5],'v2':[6,6,4,4,4,5,5,7,8]}df = pd.DataFrame(data)print(df)
returns
c v1 v20 a 1 61 a 1 62 a 2 43 b 3 44 b 4 45 b 4 56 a 4 57 a 5 78 b 5 8
Create histograms
To create a histogram from a given column:
hist = df['v1'].hist()plt.savefig("pandas_hist_01.png", bbox_inches='tight', dpi=100)

To create a histogram from a given column and create groups using another column:
hist = df['v1'].hist(by=df['c'])plt.savefig("pandas_hist_02.png", bbox_inches='tight', dpi=100)

To create two histograms from columns v1 and v2
df.hist(column=['v1','v2'])plt.savefig("pandas_hist_03.png", bbox_inches='tight', dpi=100)

Same result
hist = df[['v1','v2']].hist()plt.savefig("pandas_hist_04.png", bbox_inches='tight', dpi=100)

To create two histograms that share the same x and y axes:
hist = df[['v1','v2']].hist(sharey=True, sharex=True)plt.savefig("pandas_hist_05.png", bbox_inches='tight', dpi=100)

hist = df.hist(column=['v1'], by=df['c'])plt.savefig("pandas_hist_06.png", bbox_inches='tight', dpi=100)

