如何使用 Matplotlib 更改绘图的面颜色?

要使用 matplotlib 更改绘图的面颜色,我们可以采取以下步骤 -

  • 设置图形大小并调整子图之间和周围的填充。

  • 使用 numpy 创建 x 和 y 数据点。

  • 创建一个图形和一组子图。

  • plot()使用color=yellow 和 linewidth=7 的方法绘制 x 和 y 数据点。

  • 使用 设置坐标区的面颜色set_facecolor()

  • 要显示图形,请使用show()方法。

示例

from matplotlib import pyplot as plt
import numpy as np

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

# Create x and y data points
x = np.linspace(-10, 10, 100)
y = np.sin(x)

# Create a figure and set of subplots
fig, ax = plt.subplots()

# Plot the x and y data points with color
ax.plot(x, y, color='yellow', lw=7)

# Set the facecolor
ax.set_facecolor('red')

plt.show()
输出结果

它将产生以下输出