How can I set the background color on specific areas of a figure?

You can use axhspan and/or axvspan like this:

import matplotlib.pyplot as plt

plt.figure()
plt.xlim(0, 5)
plt.ylim(0, 5)

for i in range(0, 5):
    plt.axhspan(i, i+.2, facecolor="0.2", alpha=0.5)
    plt.axvspan(i, i+.5, facecolor="b", alpha=0.5)

plt.show()

enter image description here

Leave a Comment