liveBook · Manning

Paired histogram

As explained in Statistics Every Programmer Needs

A paired histogram is a visualization that plots two histograms on the same axes to compare one variable across two groups.

A paired histogram overlays two histograms on shared axes, splitting one numeric variable by class so distribution shape, center, spread, overlap, and separation can be compared directly. section 5.2.1

Use paired histograms to:

  • Inspect whether predictor distributions resemble or differ across two classes. section 5.2.1
  • Identify overlap or separation that may indicate discriminatory power for binary classification. section 5.2.1
  • Guide variable selection before fitting logistic regression, without replacing formal statistical evaluation. section 5.2.1

Raisin feature comparison

Plot Area, MajorAxisLength, MinorAxisLength, Eccentricity, ConvexArea, Extent, and Perimeter for Kecimen and Besni raisins in a 3×3 Matplotlib grid. Use transparency, class labels, titles, axis labels, and legends so overlapping distributions remain interpretable. section 5.2.1

>>> import matplotlib.pyplot as plt    #1
>>> fig, ((ax1, ax2, ax3), (ax4, ax5, ax6), 
>>>       (ax7, ax8, ax9)) = plt.subplots(nrows = 3, 
>>>                              ncols = 3, 
>>>                              figsize = (8, 10))    #2

>>> axes = [ax1, ax2, ax3, ax4, ax5, 
>>>         ax6, ax7, ax8, ax9]    #3
>>> columns = ['Area', 'MajorAxisLength', 'MinorAxisLength', 
>>>            'Eccentricity', 'ConvexArea', 
>>>            'Extent', 'Perimeter']     #4

>>> for i, column in enumerate(columns):    #5
>>>     for class_label, 
>>>     data in raisins.groupby('Class'): \    #6
>>>         data[column] \
>>>         .hist(alpha = 0.5, 
>>>		      label = class_label, 
>>>               ax = axes[i])    #7
>>>     axes[i].set_title(f'{column}')     #8
>>>     axes[i].set_xlabel(column)    #9
>>>     axes[i].set_ylabel('Frequency')     #10
>>>     axes[i].legend(labels = ['Kecimen', 
>>>                              'Besni']) #11

>>> for ax in [ax8, ax9]:     #12
>>>     ax.axis('off')    #13

>>> plt.tight_layout()     #14
>>> plt.show()    #15

Browse the Python implementation that iterates through numeric columns and grouped classes to render the paired-histogram grid. section 5.2.1

How Statistics Every Programmer Needs covers it

How deep it goes
Explains construction and interpretation, but stops short of formal significance testing.
Examples
Compares seven raisin morphology variables between the Kecimen and Besni classes.
What it assumes
Assumes numeric variables and class labels, while demonstrating grouped plotting with Python data.
What you can do after
Evaluate predictor overlap and separation to assess variables for a logistic regression model.
How it connects
Connects histograms with predictor selection, logistic regression, and binary classification.

Also explained in

Compare how 2 books explain paired histogram →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.