How to plot multiple functions on the same figure
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In Matplotlib, plotting multiple functions on the same figure usually means drawing several lines on the same axes before calling show(). The basic technique is simple, but a readable chart still depends on using labels, scales, and styling carefully.
The simplest pattern
If the functions share the same x values, call plot repeatedly:
Each plt.plot(...) call adds another line to the current axes. That is all you need for quick exploratory work.
Why the object-oriented API is often better
For anything beyond a tiny script, use the explicit fig, ax = plt.subplots() style. It makes it obvious which axes receive the data and avoids confusion when figures become more complex.
The structure is clearer, especially when you later add subplots, twin axes, or custom formatting.
Make the lines distinguishable
Multiple functions become unreadable fast if they all look the same. Give each line a different style or color:
Labels matter just as much as line styles. Without a legend, the reader is forced to guess which curve is which.
When functions have very different scales
If one function is bounded and another grows quickly, the smaller one can look almost flat. In that case, either limit the plotted range or use a second axis.
Use this sparingly. Twin axes help with scale differences, but they can also make a chart harder to interpret if overused.
When not to overlay everything
Putting every function on one axes is not always the best choice. If you have many lines or very different shapes, subplots are often clearer:
The goal is not merely to fit everything onto one figure. The goal is to make the comparison easy to understand.
Common Pitfalls
One common mistake is calling plt.show() too early. Once the figure is shown and the script continues, later plot calls may affect a new figure rather than the one you intended.
Another issue is forgetting labels and legend(). When several lines use similar colors, the chart becomes ambiguous immediately.
Scale mismatch is another frequent problem. If one function has values around 1 and another has values around 1000, the smaller curve may appear invisible unless you adjust the range or use a second axis.
Finally, do not mix plt.plot and ax.plot casually in the same script unless you are certain which axes are active. The object-oriented style is safer for maintainable code.
Summary
- Plot multiple functions on one figure by calling
plotmultiple times beforeshow(). - Prefer the object-oriented API for clearer, more maintainable plotting code.
- Use labels, legends, and line styles to keep the figure readable.
- Adjust axis limits or use twin axes when scales differ sharply.
- Choose subplots instead of overlays when too many functions make one chart cluttered.
Related reading
- How to plot polygons from categorical grid points in matplotlib? phase-diagram generation
- How to Plot PR-Curve Over 10 folds of Cross Validation in Scikit-Learn
- How to plot ROC curves for every cross-validations using Caret
- How to plot the graph in python like varImpPlot method plots in R ,for plotting the important variables in Random forest?
- how to plot the tensorflow neural network object
- How to POST JSON data with Python Requests?
- How to prepare a dataset for Keras?
- How to prepend a string to a column value in MySQL?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.