您可以使用 matplotlib 库中的 figure 函数来更改绘图的背景颜色。以下是一个示例代码:

import matplotlib.pyplot as plt

# 创建画布并设置背景颜色
fig = plt.figure(facecolor='lightgrey')

# 绘制图形
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)

# 显示图形
plt.show()

在上面的代码中,我们使用 facecolor 参数来设置画布的背景颜色为灰色。您可以将其更改为任何其他颜色或 RGB 值。

Python Matplotlib: 如何更改绘图背景颜色

原文地址: https://www.cveoy.top/t/topic/mIoL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录