Python:如何绘制以颜色编码值作为背景的信号?

标签 python matplotlib signals overlay colormap

我是 Python 和 matplotlib 的初学者,广泛的搜索没有产生任何有用的东西,所以这里是:

我已从设备获取了数据(每秒 256 个样本)。 下面的代码将重新创建(并绘制)数据集。

# Import modules
import numpy as np
import matplotlib.pyplot as plt
# Set variable
Fs = 256
# Create list with random measurements
np.random.seed(1)
data = [np.random.uniform(-20000, 20000) for i in range(10*Fs)]
#plt.plot(data, color = "black", linewidth = 0.3); plt.show()

对于数据,我每秒计算了几个值,即 256 个数据点。每一秒都由一组这些数字来表征。 下面的代码将为其中 3 个数字创建一个数据集,称为 sig_vals。

# Create lists with random sig_values with length of 1/Fs of measurements
sig_vals1 = [np.random.uniform(0, 1) for i in range(int(len(data)/Fs))]
sig_vals2 = [np.random.uniform(0, 1) for i in range(int(len(data)/Fs))]
sig_vals3 = [np.random.uniform(0, 1) for i in range(int(len(data)/Fs))]

目标是有一个显示的叠加图

  1. 有关数据间隔的类似热图的颜色编码信息,
  2. 与原始数据重叠。

https://ibb.co/iZcvWk

如何获得 sig_vals 颜色编码值的漂亮图作为 data 的背景?

最佳答案

这样的事情可能就是您所追求的。它假设两个数据集分布在相同的 10 秒范围内。进行颜色编码的信号连接在 3 x 10 矩阵中,可以使用 pcolormesh 绘制该矩阵。

import numpy as np
import matplotlib.pyplot as plt
# Set variable
Fs = 256
# Create list with random measurements
np.random.seed(1)
data = np.random.uniform(-20000, 20000, size=10*Fs)
t = np.linspace(0,10,len(data))

sig_vals1 = np.random.uniform(0, 1, size=int(len(data)/Fs)) 
sig_vals2 = np.random.uniform(0, 1, size=int(len(data)/Fs))
sig_vals3 = np.random.uniform(0, 1, size=int(len(data)/Fs))
sig = np.c_[sig_vals1,sig_vals2,sig_vals3].T

T, s = np.meshgrid(np.linspace(0,10,int(len(data)/Fs)+1),np.arange(4))


plt.pcolormesh(T,s,sig)
plt.yticks(np.arange(.5,3,1), ["Signal{}".format(i+1) for i in range(3)])

ax2 = plt.gca().twinx()
ax2.plot(t, data, color = "w", linewidth = 0.3, alpha=0.6)
ax2.margins(0)

plt.show()

enter image description here

关于Python:如何绘制以颜色编码值作为背景的信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45823723/

相关文章:

c - `c` 中的信号。 while循环的使用

C++ - Clutter 1.0 - 从线程调用函数导致段错误

python - 如何从nd数组python中删除空格

python - 如何设置饼图 matplotlib 的透明度和背景颜色

python - 非数字数据的散点图

python - 如何使用 Matplotlib 从轮廓到图像蒙版

python - 在 spyder IDE 中以时间相关值显示 gekko 变量值

Python Windows 错误 : [Error 3] The system cannot find the file specified when trying to rename

python plot 和 powerlaw 适合

c++ - 为什么 timer_create 在 solaris 10 中为 SIGEV_THREAD 抛出错误?