Data Visualization for ML with Matplotlib
Visualization as debugging, not decoration
It's easy to think of charts as something you make at the end, for a slide deck. In machine learning practice, that's backwards — plots are one of the fastest ways to catch problems *while* you're building, before you waste hours training a broken model. A histogram catches a feature that's badly skewed before it wrecks a model's assumptions. A loss curve catches overfitting hours before a final accuracy number would reveal it. A confusion matrix catches a class the model is systematically confusing, which a single "accuracy: 94%" number would hide completely.
Matplotlib is the foundational Python plotting library — lower-level and more verbose than some newer alternatives, but it's what most other plotting libraries (including pandas' own .plot()) are built on top of, and understanding it directly gives you control when the defaults don't show what you need.
Plotting distributions before you trust your data
Before training anything, plotting the distribution of each feature is one of the highest-value five minutes you can spend. A histogram instantly reveals skew, unexpected multi-modal clusters, suspicious spikes at a single value (often a placeholder like 0 or -1 for missing data), and outliers that summary statistics like mean and standard deviation can hide or distort.
Loss curves: the single most important training plot
During training, plotting training loss and validation loss against each epoch is the fastest diagnostic available. If both losses are dropping together, training is healthy. If training loss keeps dropping while validation loss flattens or rises, that's overfitting — the model is memorizing training data rather than learning generalizable patterns, and continuing to train will only make the gap worse. If both losses stay high and flat, the model likely isn't learning at all, pointing to a learning rate, data, or architecture problem rather than simply "needs more epochs."
None of this is visible from a single final accuracy number. The shape of the curve over time is the actual diagnostic signal.
Note
Common ML plots and what each one catches
| Plot type | What it reveals | When to use it |
|---|---|---|
| Histogram / distribution plot | Skew, outliers, placeholder values, missing-data patterns | Before training, during data exploration |
| Loss curve (train vs. validation) | Overfitting, underfitting, learning rate problems | During and after every training run |
| Confusion matrix | Which specific classes get confused with which | After evaluation, for classification tasks |
| Scatter plot of two features | Correlation, clusters, separability by class | During feature exploration |
What's next
Visualization helps you catch problems, but it doesn't tell you *why* a trained model makes the decisions it does. Continue to Model Interpretability: Feature Importance Methods to see how to open up a trained model and ask which inputs actually drove its predictions.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.