How to change plot background color?
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 plotting libraries, "background color" usually means one of two surfaces: the figure background outside the axes and the plotting-area background inside the axes. If you only change one of them, the result often looks wrong or incomplete.
In Matplotlib, the key methods are fig.patch.set_facecolor() for the outer figure and ax.set_facecolor() for the inner plotting area. Once you know that split, changing the visual theme becomes straightforward.
Change the Axes Background in Matplotlib
If you want to recolor only the data area, set the axes face color.
This changes the rectangle behind the plotted line, but not the outer canvas of the figure. That is why plots can still show white margins even after you change the axes color.
Change the Figure Background Too
To control the full visual frame, set both the figure and axes colors:
This is the practical pattern for dark themes. The background color alone is not enough. Text, ticks, and spines also need contrasting colors or the plot becomes unreadable.
Set Defaults for Many Plots
If you need the same background repeatedly, set global defaults instead of restyling each figure manually.
This is useful in notebooks, reporting scripts, and dashboards where a consistent theme matters more than one-off customization.
The same idea also works well with subplots. Once the defaults are set, every axes object starts from the same background and only needs local overrides when one panel should stand out from the rest.
You can also start from a built-in style:
Then override individual colors as needed.
Save the Background Color Correctly
A common surprise is that the saved image does not match the interactive display. When saving, pass the figure face color explicitly if you want the exported file to keep the chosen background.
If you want no background at all, use transparency instead:
That is especially useful when a web page or slide deck will provide the final background behind the chart.
Common Pitfalls
The biggest mistake is changing only the figure color or only the axes color and expecting the whole plot to match. They are separate layers.
Another common issue is forgetting to restyle text and ticks after switching to a dark background. A dark plot with black labels is technically correct and visually useless.
It is also easy to lose the chosen background during export by saving without a matching facecolor argument.
Finally, style presets can overwrite manual changes if you apply them after your custom color settings. Apply the style first, then customize.
Summary
- In Matplotlib, the axes and figure backgrounds are separate.
- Use
ax.set_facecolor()for the plotting area. - Use
fig.patch.set_facecolor()for the outer canvas. - Update text, ticks, and spines so the plot stays readable.
- Pass the face color explicitly when saving if you want the file to match the displayed chart.
Related reading
- How to change size of plot in xgboost.plot_importance?
- How to change the datetime format in Pandas
- How to change the figure size of a seaborn axes or figure level plot
- How to change the font size on a matplotlib plot
- How to change the font size on a matplotlib plot
- How to change the order of DataFrame columns?
- How to change the order of DataFrame columns?
- How to change the threshold on decision tree classifier model?
.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.