如何在 Matplotlib 中更改轴背景色?
要更改轴背景色,我们可以使用set_facecolor()方法。
步骤
设置图形大小并调整子图之间的和周围的填充。
使用gca()方法获取当前轴。
设置轴的背景色。
使用 numpy 创建 x 和 y 数据点。
使用plot()方法绘制 x 和 y 数据点。
要显示该图形,请使用show()方法。
示例
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True ax = plt.gca() ax.set_facecolor("orange") x = np.linspace(-2, 2, 10) y = np.exp(-x) plt.plot(x, y, color='red') plt.show()
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
广告