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

yipeiwu_com5年前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设计】。

相关文章

简单了解python关系(比较)运算符

简单了解python关系(比较)运算符

a.对象的值进行比较 数字间的比较 运算符连着使用: 数字与True、False的比较 True 表示 1 , False 表示 0 数字与字符串的比较(不能比较) 字符串间的...

python Web flask 视图内容和模板实现代码

这篇文章主要介绍了python Web flask 视图内容和模板实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 基本使用...

Python读取csv文件分隔符设置方法

Windows下的分隔符默认的是逗号,而MAC的分隔符是分号。拿到一份用分号分割的CSV文件,在Win下是无法正确读取的,因为CSV模块默认调用的是Excel的规则。 所以我们在读取文件...

在CentOS上配置Nginx+Gunicorn+Python+Flask环境的教程

Python基础环境搭建 CENTOS 6.X 系列默认安装的 Python 2.6 ,目前开发中主要是使用 Python 2.7 ,这两个版本之间还是有不少差异的,程序在 Python...

用 Python 爬了爬自己的微信朋友(实例讲解)

用 Python 爬了爬自己的微信朋友(实例讲解)

最近几天干啥都不来劲,昨晚偶然了解到 Python 里的 itchat 包,它已经完成了 wechat 的个人账号 API 接口,使爬取个人微信信息更加方便。鉴于自己很早之前就想知道诸如...