Color choices in a Matplotlib figure separate related data series, keep markers readable, and make a chart match a report palette. Setting colors on the drawn objects themselves keeps the figure predictable when default color cycles or style sheets change elsewhere in a script.
Matplotlib accepts named colors such as tab:blue, hex strings such as #ffbf00, RGB or RGBA tuples with values from 0 to 1, grayscale strings, and cycle references such as C2. Line plots, markers, bars, and filled regions expose different color parameters, so the color should be set on the artist being drawn.
Direct plot colors differ from colormaps, which map numeric values onto a color scale, and from style sheets, which change defaults across many figures. Use direct colors when one line, marker, bar group, or filled band needs a fixed appearance in one plot.
Related: How to set a colormap in Matplotlib
Related: How to set a Matplotlib style theme
Related: How to create a line chart in Matplotlib
Steps to set Matplotlib plot colors:
- Create a Python script that assigns colors to a line, markers, bars, and a filled range.
- plot_color_set.py
import matplotlib.pyplot as plt from matplotlib.colors import to_hex months = ["Jan", "Feb", "Mar", "Apr", "May"] planned = [4, 5, 6, 7, 8] actual = [3, 6, 5, 8, 9] palette = { "line": "tab:blue", "marker_face": "#ffbf00", "marker_edge": (0.0, 0.2, 0.4), "bars": "C2", "fill": (0.2, 0.6, 0.9, 0.25), } fig, ax = plt.subplots(layout="constrained") (actual_line,) = ax.plot( months, actual, color=palette["line"], marker="o", markerfacecolor=palette["marker_face"], markeredgecolor=palette["marker_edge"], linewidth=2.5, label="Actual", ) bars = ax.bar( months, planned, color=palette["bars"], edgecolor="#1f2937", alpha=0.35, label="Plan", ) band = ax.fill_between( months, [value - 1 for value in actual], [value + 1 for value in actual], color=palette["fill"], label="Range", ) ax.set_title("Revenue plan and actuals") ax.set_ylabel("Revenue ($k)") ax.legend() fig.savefig("plot-color-set.png", dpi=160) print(f"line color: {to_hex(actual_line.get_color())}") print(f"marker face: {to_hex(actual_line.get_markerfacecolor())}") print(f"marker edge: {to_hex(actual_line.get_markeredgecolor())}") print(f"bar face: {to_hex(bars.patches[0].get_facecolor())}") print(f"fill face: {to_hex(band.get_facecolor()[0], keep_alpha=True)}") print("saved: plot-color-set.png")
color sets line and many patch colors. markerfacecolor and markeredgecolor control marker fill and outline separately. edgecolor sets the bar outline, and the RGBA tuple on fill carries its own transparency.
- Run the script and verify the resolved colors and saved figure.
$ python plot_color_set.py line color: #1f77b4 marker face: #ffbf00 marker edge: #003366 bar face: #2ca02c fill face: #3399e640 saved: plot-color-set.png
Use cycle references such as C2 when the color should follow the active property cycle. Use named or hex colors when the same element should keep the same appearance across styles.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.