Menu

Histograms and Box Plots

Histograms and Box Plots

Before comparing or predicting, you need to know how your values are spread. Histograms and box plots show the shape and range of your data. This topic explains how to create and read both.

a. Understanding Data Distribution

A distribution describes how the values in a column are spread out. Are most values near the middle, or are they pushed to one side? Are there a few extreme values? These questions are answered by looking at the distribution.

Common shapes include the bell shape, where most values are near the average, and the skewed shape, where values bunch up on one side with a long tail on the other. Income data is often skewed.

Knowing the shape helps you choose the right statistics. For skewed data, the median is usually a better summary than the mean.

b. Creating Histograms and Choosing Bins

A histogram splits the range of values into equal groups called bins, and counts how many values fall in each one. The height of each bar shows the count.

fig, ax = plt.subplots()
ax.hist(df["age"], bins=15, edgecolor="black")
ax.set_title("Age Distribution")
ax.set_xlabel("Age")
ax.set_ylabel("Count")
plt.show()

The number of bins changes how the chart looks. Too few bins hide detail, and too many make the chart noisy. Try a few values and pick the one that shows the shape clearly.

Add edgecolor so the bars are separated and easy to see.

c. Reading Box Plots

A box plot summarizes a distribution in a compact way. The box covers the middle 50 percent of the data, from the first quartile to the third quartile. A line inside the box marks the median.

The lines extending from the box are called whiskers. They show the range of typical values. Points beyond the whiskers are marked as possible outliers.

ax.boxplot(df["salary"])

You can place several box plots side by side to compare groups, such as salaries across departments. This makes it easy to see differences in the median and the spread.

d. Spotting Outliers in Charts

Outliers are values that are far from the rest. In a box plot, they appear as dots beyond the whiskers. In a histogram, they show up as small bars far from the main group.

Outliers may be errors, such as a typing mistake, or real but rare events. Do not delete them automatically. Investigate first to understand why they exist.

If an outlier is an error, fix or remove it. If it is real, keep it and consider whether it affects your summary numbers. Charts are one of the best tools to find them quickly.


Data Visualization Tutorial

WR

Wesline Rinisha



Learn via Video Course

Data Visualization Using Python Logo

Data Visualization Using Python

19 hrs



Get in Touch For Details