Multiple lines on same plot with incremental logging - wandb
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
Getting multiple lines onto the same Weights and Biases chart is less about plotting code and more about logging discipline. The key requirements are a shared step axis and stable metric names across the run. When either of those changes midstream, W&B stops seeing one evolving chart and starts seeing separate histories.
The Simplest Incremental Logging Pattern
For most training jobs, log multiple metrics at the same step and let W&B build the chart from history:
This is incremental logging because each call appends one more point to the same history. Both metrics share the same step value, so overlaying them makes sense.
In the W&B UI, metrics with related names such as loss/train and loss/val are easy to overlay or group.
Define a Shared Step Metric Explicitly
If you want to control the x-axis yourself, define it once:
This is especially useful when W&B's internal step is not the axis you care about. A common example is plotting training and validation lines against epoch rather than against raw log-call count.
Custom Multi-Line Charts With line_series
If you want a single purpose-built chart object rather than relying on UI grouping, use wandb.plot.line_series. The trade-off is that you maintain the history arrays yourself.
This approach gives you a single chart with named lines, but it is less lightweight because every update resends the accumulated arrays.
Which Approach Should You Use?
Use ordinary wandb.log with separate metric keys when:
- training runs are long
- you want efficient incremental logging
- you are happy to use the dashboard's normal metric charts
Use wandb.plot.line_series when:
- you want one explicit custom chart object
- you need more control over labels and presentation
- the logged history is not huge
For most training loops, plain incremental metric logging is the better default because it scales better and keeps run history simple.
Example With Training and Validation Every Epoch
Here is a practical pattern that works well in Keras or PyTorch training code:
This gives you stable metric names, a stable x-axis, and a history that can be compared across runs.
Common Pitfalls
- Logging related lines at different step values. Fix: make the metrics share one explicit step axis.
- Changing metric names partway through a run. Fix: decide on naming once and keep it stable from the first log call.
- Rebuilding a
line_serieschart from only the newest point. Fix: preserve accumulated history when using custom chart objects. - Logging too frequently. Fix: choose a cadence that is informative without bloating history.
- Mixing custom chart logging and plain metric logging without a plan. Fix: decide whether the dashboard should group raw metrics or display one custom chart object.
Summary
- To get multiple lines on one plot, make the metrics share the same x-axis and naming convention.
- Plain
wandb.logwith stable keys is usually the simplest incremental solution. - Use
wandb.define_metricwhen you want explicit control over the step metric. - Use
wandb.plot.line_serieswhen you need a single custom multi-line chart object. - Be consistent about steps and metric names, or the dashboard will fragment the plot into separate histories.
Related reading
- Multiple Output Neural Network
- Multiple output regression or classifier with one or more parameters with Python
- Multiple outputs in Keras
- Multiple outputs in keras Sequential models
- Multiple metrics to specific inputs
- Multiple pipelines that merge within a sklearn Pipeline?
- Multiple sessions and graphs in Tensorflow in the same process
- multiple tasks using python asyncio
.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.