Set Canvas Color in Python: Matplotlib Guide

In Python, you can use the Matplotlib library to create plots and set the canvas color. Here is an example of how to set the canvas color:

import matplotlib.pyplot as plt

# 创建一个画布
plt.figure()

# 设置画布颜色为灰色
plt.gca().set_facecolor('lightgrey')

# 绘制一条直线
plt.plot([0, 1], [0, 1])

# 显示图形
plt.show()

In the code above, a canvas is first created, then the background color of the canvas is set using the plt.gca().set_facecolor() method, and finally the plot is drawn and displayed. The color of the canvas can be changed by modifying the ‘lightgrey’ parameter.

bannerAds