Matplotlib中怎么自定义背景

1334
2024/4/29 14:32:15
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

要自定义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图形的背景。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: 如何在Matplotlib中绘制圆环图并在其中显示百分比或值