How to get different colored lines for different plots in a single 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
When several series share one figure, color is usually the first thing that helps the reader separate them. Matplotlib already cycles colors automatically, but explicit color control is better when you need consistency, accessibility, or a chart that will be reproduced in reports and notebooks.
Let Matplotlib Use Its Default Cycle
For quick exploratory work, calling plot repeatedly on the same axes is enough. Matplotlib assigns a different color to each line from its default cycle.
This is fine for ad hoc plots, but the colors may shift if you change plotting order or insert another series later.
Assign Explicit Colors for Stable Output
If the same series should always have the same color, set the color yourself.
Hex colors are often the easiest format when you need to match a design system or reuse chart colors across several files.
Generate Many Distinct Colors Programmatically
Manual color assignment becomes tedious when you have many lines. In that case, sample from a qualitative colormap.
For categorical lines, colormaps such as tab10, tab20, or Set2 are usually a better fit than continuous ramps like viridis.
Keep Colors Consistent Across Figures
If the same labels appear across several charts, build a palette dictionary and reuse it everywhere.
This prevents a common reporting problem where the same series is blue in one chart and orange in the next.
Combine Color With Other Visual Cues
Color alone is not always enough. Some readers are color-impaired, some charts are printed in grayscale, and some palettes simply do not separate nearby hues well. Add line styles or markers when clarity matters.
That makes the figure easier to interpret even when color information is weak.
Seaborn and Plotly Follow the Same Pattern
The exact API differs, but the idea is the same in other libraries. In Seaborn, map categories to a palette with hue. In Plotly, set per-trace line colors explicitly. Deterministic color mapping by series identity is the underlying concept regardless of the library.
Common Pitfalls
The most common mistake is relying on the default color order when plot order can change. That makes charts inconsistent across edits.
Another issue is using too many visually similar colors on one axes. Once the hues become hard to distinguish, add line styles, markers, or reduce the number of simultaneously displayed series.
Developers also often pick continuous colormaps for category data. That makes the colors imply an ordering that may not exist.
Finally, if the same series appears in multiple figures, keep the mapping fixed. Reassigning colors from chart to chart increases reader error.
Summary
- Matplotlib already gives different colors to successive
plotcalls on the same axes. - Use explicit colors when you need stable, repeatable chart output.
- Sample from a qualitative colormap when plotting many categories.
- Combine color with markers or line style for accessibility and clarity.
- Reuse one palette mapping across related figures so the same series always looks the same.
Related reading
- How to get different Variable Importance for each class in a binary h2o GBM in R?
- How to get feature Importance in naive bayes?
- How to get feature names selected by feature elimination in sklearn pipeline?
- How to get Grafana to include sum of values in tooltip or legend for stacked linechart
- How to get distinct values from an array of objects in JavaScript?
- How to get first element in a list of tuples?
- How to get largest number of consecutive integers in a substantially large array (spread across multiple machines)
- how to get pandas get_dummies to emit N-1 variables to avoid collinearity?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.