histogram: setting y-axis label for pandas

Question:

I have dataframe:

d = {'group': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'D', 'D', 'D', 'D', 'D'], 
     'value': [0.2, 0.4, 0.6, 0.8, 0.2, 0.4, 0.6, 0.8, 0.2, 0.4, 0.6, 0.8, 1.0],
     'count': [4, 3, 7, 3, 12, 14, 5, 10, 3, 8, 7, 15, 4]}
df = pd.DataFrame(data=d)
df

I want to plot multiple histograms in one figure. That is, a histogram for group A, a histogram for group B and group D in one figure. So, labels is a group column, y-axis is count, x-axis is value.

I do this, but there are incorrect values on the y-axis and it builds several figures.

axarr = df.hist(column='value', by = 'group', bins = 20)

for ax in axarr.flatten():
    ax.set_xlabel("value")
    ax.set_ylabel("count")
Asked By: Rory

||

Answers:

Assuming that you are looking for a grouped bar plot (pending the clarification in the comments):

Plot:

enter image description here

Code:

import pandas as pd

d = {'group': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'D', 'D', 'D', 'D', 'D'], 
     'value': [0.2, 0.4, 0.6, 0.8, 0.2, 0.4, 0.6, 0.8, 0.2, 0.4, 0.6, 0.8, 1.0],
     'count': [4, 3, 7, 3, 12, 14, 5, 10, 3, 8, 7, 15, 4]}
df = pd.DataFrame(data=d)

df_pivot = pd.pivot_table(
    df,
    values="count",
    index="value",
    columns="group",
)

ax = df_pivot.plot(kind="bar")
fig = ax.get_figure()                
fig.set_size_inches(7, 6)
ax.set_xlabel("value")
ax.set_ylabel("count")

Brief plot option:

pd.pivot(df, index="value", columns="group").plot(kind="bar", y='count')

Brief explanation:

Pivot table for preparation:

  • x-axis ‘value’ as index
  • different bars ‘group’ as columns
group    A     B     D
value                 
0.2    4.0  12.0   3.0
0.4    3.0  14.0   8.0
0.6    7.0   5.0   7.0
0.8    3.0  10.0  15.0
1.0    NaN   NaN   4.0

Pandas .plot() can handle that groupded bar plot directly after the df_pivot preparation.
Its default backend is matplotlib, so usual commands apply (like fig.savefig).


Add-on apperance:

You can make it look more like a hist plot concerning the x-axis by aligning the spacing in between the groups:

enter image description here

Just add a , width=0.95 (or =1.0) within the .plot( ) statements.

Answered By: MagnusO_O