如何变换两组离散点(向量)以帮助在一个共同的尺度上绘制它们

时间:2014-11-22 16:32:39

标签: algorithm math matplotlib

我有两组离散点

G1:(x,y1),其中y1在整数范围内[1..90](比如说)

G2:(x,y2),其中y2在整数范围[1..110]

这些点的长度不同,但我想在一个共同的比例上比较他们的情节。

也就是说,我需要在单个图中以常见比例显示这些图形图(例如[1..100])。

我想知道如何做到这一点。我该如何转换这些矢量以便以通用比例绘制它?

P.S。我想说明问题。

所以,让我们考虑两个列表: len(d1)= 110,并且他包含一些值。 len(d2)= 80。 我想在一张图片中构建这些图形(使用matplotlib)。 len(x)= 110 - 范围从0到110

fig, ax = plt.subplots()
ax.plot(x, d1, 'k-')
ax.plot(x, d2, 'c--')

所以,如果我只是在一些系数上乘以列表d2的元素,我将只得到80个元素,并且它不能用于绘图。此外,我必须填写错过的值,但我不明白该怎么做。

1 个答案:

答案 0 :(得分:1)

我觉得您需要upsample / interpolate使用更少样本的矢量来获取更多样本并downsample / decimate 使用更高样本的向量来获得更少的样本(实质上匹配两个向量的采样率)。

我使用scipy.signal.resample进行上/下采样。<​​/ p>

我尝试使用两个不等样本大小的随机向量来模拟您的情况。

看看这是否有助于你:

import numpy as np
from scipy import signal  
# scipy.signal module contains a interpolator / decimator
import matplotlib.pyplot as plt

# Creating random vectors for a and b

vector_a = np.sin(2*3.14*100*np.arange(130))  
# Sine signal with 100Hz freq and 130 time samples

vector_b = np.cos(2*3.14*100*np.arange(80))
# Cosine signal with 100Hz freq and 80 time samples

# To avoid bias towards any one vector length take the
# mean of the two sample lengths as the common sample length

common_no_of_samples = (vector_a.shape[0] + vector_b.shape[0]) // 2 
# 105 Samples

# Upsample vector_a to have common_no_of_samples
vector_a = signal.resample(vector_a, common_no_of_samples)
# Downsample vector_b to have common_no_of_samples
vector_b = signal.resample(vector_b, common_no_of_samples)

fig, ax = plt.subplots()
ax.plot(np.arange(common_no_of_samples), vector_a, 'k-')
ax.plot(np.arange(common_no_of_samples), vector_b, 'c--')

# Where np.arange(common_no_of_samples) refers to the common time axis
# vector_a and vector_b are the resampled vectors.

enter image description here

如果您想要enter image description here作为积分,您可以这样做:

time_axis = np.arange(common_no_of_samples)
vector_a = np.dstack((vector_a, time_axis))

这将生成以下形式的点:

array([[[  2.23656191e-02,   0.00000000e+00],
    [ -3.96584073e-01,   1.00000000e+00],
    [ -7.01262520e-01,   2.00000000e+00],
    [ -9.31867589e-01,   3.00000000e+00],
    [ -9.95165113e-01,   4.00000000e+00],
    [ -9.24625413e-01,   5.00000000e+00],
    [ -6.96587056e-01,   6.00000000e+00],
    [ -3.74795767e-01,   7.00000000e+00],
    [  1.59956385e-02,   8.00000000e+00],
    [  3.94192306e-01,   9.00000000e+00],
    [  7.20969109e-01,   1.00000000e+01],
    [  9.28803144e-01,   1.10000000e+01],
    [  1.00160878e+00,   1.20000000e+01],
    [  9.13659002e-01,   1.30000000e+01],
    [  6.91934367e-01,   1.40000000e+01],
    [  3.57910455e-01,   1.50000000e+01],