Python的matplotlib绘图如何修改背景颜色的实现

yipeiwu_com6年前Python基础

在主图中背景颜色不知道怎么改,plt.plot()中没有axisbg参数。

但是子图可以对plt.subplot的参数做修改,下面是对子图的背景颜色修改代码

import matplotlib.pyplot as plt
import numpy as np
 
# Fixing random state for reproducibility
np.random.seed(19680801)
 
dt = 0.01
t = np.arange(0, 30, dt)
nse1 = np.random.randn(len(t))         # white noise 1
nse2 = np.random.randn(len(t))         # white noise 2
s1 = np.sin(2 * np.pi * 10 * t) + nse1
s2 = np.sin(2 * np.pi * 10 * t) + nse2
s3 = np.sin(2 * np.pi * 10 * t) + nse1
s4 = np.sin(2 * np.pi * 10 * t) + nse2
 
fig= plt.figure(1) # 创建图表1
axs0=plt.subplot(221,axisbg='#FFDAB9') #在图标1中创建子图
axs0.plot(t, s1) #横轴与纵轴数据
axs0.set_xlim(0, 2) #限制x轴的取值范围
axs1=plt.subplot(222,axisbg='#7FFF00')
axs1.plot(t, s2)
axs1.set_xlim(0, 2)
axs2=plt.subplot(223,axisbg='#FF7F50')
axs2.plot(t, s3)
axs2.set_xlim(0, 2)
axs3=plt.subplot(224,axisbg='#A9A9A9')
axs3.plot(t, s4)
axs3.set_xlim(0, 2)
 
plt.show()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

通过cmd进入python的实例操作

通过cmd进入python的实例操作

通过cmd启动Python需要先设置系统环境,设置步骤如下: 1、首先,在桌面找到 “计算机” 右键 找到 “属性”或者按下 win 键 再右键“计算机” 找到 “属性”也可以。如下图所...

Python 机器学习库 NumPy入门教程

NumPy是一个Python语言的软件包,它非常适合于科学计算。在我们使用Python语言进行机器学习编程的时候,这是一个非常常用的基础库。 本文是对它的一个入门教程。 介绍 NumP...

python 输出一个两行字符的变量

今天遇到老朋友,就加了个/n 呵呵。比我的简单。 简单示例如下: >>> a='aaa\nbbb' >>> print a aaa...

python 基本数据类型占用内存空间大小的实例

python中基本数据类型和其他的语言占用的内存空间大小有很大差别 import sys a = 100 b = True c = 100L d = 1.1 e ="" f = []...

Python正则表达式实现截取成对括号的方法

本文实例讲述了Python正则表达式实现截取成对括号的方法。分享给大家供大家参考,具体如下: strs = '1(2(3(4(5(67)6)7)8)9)0' reg1 = re.co...