matplotlib

Matplotlib中怎么自定义背景

小亿
104
2024-05-13 14:32:15
栏目: 编程语言

要自定义Matplotlib中的背景,可以使用plt.style.use函数来加载预定义的样式,或者通过设置fig.patch属性来自定义背景色。

  1. 使用预定义样式:
import matplotlib.pyplot as plt

# 加载预定义样式
plt.style.use('ggplot')

# 绘制图形
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()
  1. 自定义背景色:
import matplotlib.pyplot as plt

# 创建一个图形
fig = plt.figure()
fig.patch.set_facecolor('lightblue')

# 绘制图形
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()

通过以上两种方式,可以轻松地自定义Matplotlib图形的背景。

0
看了该问题的人还看了