How to set the background color of a column in a matplotlib table?

To set the background color of a column in a matplotlib table, you need to define colors for each cell and pass them to the table's cellColours parameter. This allows you to customize the appearance of specific columns or individual cells.

Basic Example

Here's how to create a table with different background colors for each column ?

import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Define table structure
columns = ('Name', 'Age', 'Marks', 'Salary')
cell_text = [["John", "23", "98", "234"], 
             ["James", "24", "90", "239"]]

# Define colors for each cell (row by row)
colors = [["lightblue", "lightgreen", "lightyellow", "lightcoral"], 
          ["lightblue", "lightgreen", "lightyellow", "lightcoral"]]

# Create figure and table
fig, ax = plt.subplots()
table = ax.table(cellText=cell_text, 
                cellColours=colors, 
                colLabels=columns, 
                loc='center')

# Hide axes
ax.axis('off')

plt.show()

Setting Uniform Column Colors

To set the same background color for an entire column, repeat the color for each row ?

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [8, 4]
plt.rcParams["figure.autolayout"] = True

# Table data
columns = ('Product', 'Price', 'Stock', 'Rating')
data = [["Laptop", "$999", "15", "4.5"], 
        ["Mouse", "$25", "50", "4.2"],
        ["Keyboard", "$75", "30", "4.7"]]

# Create uniform column colors
num_rows = len(data)
column_colors = ['lightblue', 'lightgreen', 'lightyellow', 'lightpink']
colors = [column_colors for _ in range(num_rows)]

fig, ax = plt.subplots()
table = ax.table(cellText=data, 
                cellColours=colors, 
                colLabels=columns, 
                loc='center')

# Style the table
table.auto_set_font_size(False)
table.set_fontsize(10)
table.scale(1.2, 1.5)

ax.axis('off')
plt.show()

Customizing Specific Columns

You can highlight specific columns by setting targeted colors ?

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [8, 4]

# Sample data
columns = ('Employee', 'Department', 'Salary', 'Bonus')
data = [["Alice", "Engineering", "$80000", "$5000"], 
        ["Bob", "Marketing", "$65000", "$3000"],
        ["Carol", "Engineering", "$75000", "$4500"]]

# Create color matrix - highlight salary column
colors = []
for row in data:
    row_colors = ['white', 'white', 'lightgreen', 'white']  # Highlight salary column
    colors.append(row_colors)

fig, ax = plt.subplots()
table = ax.table(cellText=data, 
                cellColours=colors, 
                colLabels=columns, 
                loc='center')

table.auto_set_font_size(False)
table.set_fontsize(9)
table.scale(1.2, 2)

ax.axis('off')
ax.set_title('Employee Data - Salary Column Highlighted', pad=20)

plt.show()

Key Parameters

  • cellText − The data to display in table cells
  • cellColours − Background colors for each cell (2D list)
  • colLabels − Column header labels
  • loc − Table position ('center', 'upper left', etc.)

Conclusion

Use the cellColours parameter to set background colors for table columns. Create a 2D color list matching your data structure, where each sublist represents colors for one row across all columns.

Updated on: 2026-03-26T00:20:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements