data analysis
histogram plotting
data visualization
data collection
statistical graphics

Getting data for histogram plot

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A histogram needs one numeric variable and a clear idea of what each observation represents. Most histogram problems are really data-preparation problems: the values are missing, mixed with text, measured in the wrong unit, or grouped so badly that the final plot becomes misleading.

The best workflow is to collect one clean numeric series first, then worry about bins and styling. If the input data is sound, plotting becomes the easy part.

Start with One Numeric Column

A histogram answers "how many observations fall into each numeric range?" That means the raw input should be a single list or column of numbers such as response times, ages, scores, or file sizes.

Here is a simple Python example that loads a CSV file and extracts one numeric column for plotting:

python
1import pandas as pd
2
3df = pd.read_csv("measurements.csv")
4values = pd.to_numeric(df["response_ms"], errors="coerce").dropna()
5
6print(values.head())
7print(values.describe())

Two details matter here:

  • 'pd.to_numeric(..., errors="coerce") turns invalid values into missing data'
  • 'dropna() removes those missing entries before plotting'

That cleaning step is often the difference between a useful histogram and a broken one.

Understand What Each Row Means

Before plotting, verify that each value is one comparable observation. A histogram of daily revenue, request latency, and user ages all work, but only if the rows represent the same kind of thing on the same scale.

For example, mixing milliseconds and seconds in the same column will create a meaningless distribution. Likewise, mixing raw measurements with already-aggregated totals can distort the shape badly.

It helps to do a quick sanity check:

python
print(values.min(), values.max())
print(values.sample(10, random_state=0).tolist())

This catches obvious unit problems before the graph makes them look like a statistical insight.

Choose Bins Deliberately

Once the numeric data is clean, you can choose bin edges. Too few bins hide structure. Too many bins make random noise look important.

You do not need to guess blindly. NumPy can suggest bin edges from the data:

python
1import numpy as np
2
3bin_edges = np.histogram_bin_edges(values, bins="auto")
4print(bin_edges)

Then plot the histogram with those edges:

python
1import matplotlib.pyplot as plt
2
3plt.hist(values, bins=bin_edges, edgecolor="black")
4plt.xlabel("Response time (ms)")
5plt.ylabel("Count")
6plt.title("Distribution of response times")
7plt.show()

This is a good default because the binning strategy is derived from the actual sample instead of an arbitrary round number.

Get Data from Common Sources

The way you gather histogram data depends on where the numbers live:

  • CSV files or spreadsheets often become a pandas column
  • SQL sources often become one query result column
  • application logs often need parsing before numeric extraction
  • APIs often need JSON normalization before plotting

A SQL example:

python
1import sqlite3
2import pandas as pd
3
4connection = sqlite3.connect("app.db")
5query = "select duration_ms from requests where duration_ms is not null"
6values = pd.read_sql_query(query, connection)["duration_ms"]

No matter the source, the goal is the same: produce one clean numeric sequence with a well-understood meaning.

Watch for Outliers and Filtering

Some datasets contain extreme outliers that compress the rest of the histogram into a narrow cluster. That does not always mean you should remove them, but you should know whether the plot is answering the question you care about.

Sometimes the right approach is to plot the full data once, then plot a filtered version for operational analysis:

python
filtered = values[values <= 5000]
plt.hist(filtered, bins=30, edgecolor="black")
plt.show()

Filtering should be explicit and documented. Quietly discarding data produces pretty charts but weak analysis.

Common Pitfalls

The biggest mistake is plotting non-numeric or mixed-unit data as if it were one clean measurement series.

Another common issue is choosing bins arbitrarily and then over-interpreting the picture. Bin size can change the apparent shape a lot.

It is also easy to use pre-aggregated counts instead of raw observations. A histogram expects raw sample values, not already-binned totals.

Finally, always inspect missing values and outliers before plotting. A histogram is only as trustworthy as the data preparation behind it.

Summary

  • A histogram needs one clean numeric variable.
  • Convert and clean the data before plotting.
  • Verify that every row represents the same kind of measurement.
  • Choose bins deliberately, ideally from the data itself.
  • Watch for outliers, missing values, and mixed units before drawing conclusions.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.