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创建文件备份的脚本

制作文件备份 打开原文件 old_f_name = input(“请输入备份的文件路径:”) old_f = open(old_f_name, “r”) 打开新文件...

python实现排序算法解析

python实现排序算法解析

本文实例为大家分享了python实现排序算法的具体代码,供大家参考,具体内容如下 一、冒泡排序 def bububle_sort(alist): """冒泡排序(稳定|n^2m)...

Jupyter中直接显示Matplotlib的图形方法

Jupyter中直接显示Matplotlib的图形方法

一.使用以下cmd命令生成ipython_config.py 文件 ipython profile create 二.在ipython_config.py中添加以下代码 c....

Python中最常用的操作列表的几种方法归纳

这里介绍几个常用的列表操作 添加元素 添加元素使用列表的内置方法append number = [1, 2, 3, 4] number.append(5) # number = [1...

python取数作为临时极大值(极小值)的方法

编程中有时候需要一个初始极大值(或极小值)作为temp,当然可以自定义设置为10000(whatever),不过python中有一个值可以代替之: 在python2.7中可以用这个(不...