繁体   English   中英

"使用 matplot 图在同一信号上绘制多个开始和结束段"

[英]Plotting multiple start and end segments on the same signal using matplot figure

我想在同一绘图信号上打印候选元组表示的所有数据信号段。 我已经尝试过以下方法,但只能设法绘制不同的数字。 但是,我想在同一个图上绘制不同的段,即在相同的数据信号上绘制段(60、822)、(829、159)和(832、346),但在不同的段点上,如示例所示下图。

预期输出的示例如下: 在此处输入图像描述<\/a>

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def plot(ind1, ind2, filename='test.png'):
    fig = plt.figure(figsize=(20,6))
    ax = fig.add_subplot(211)
    d1 = data[ind1:ind1+n]
    d2 = data[ind2:ind2+n]
    ax.plot(range(ind1,ind1+n),d1, color='g', marker='', linestyle='-', lw=2., mew=2.)
    ax.plot(range(ind2,ind2+n),d2, color='b', marker='', linestyle='-', lw=2., mew=2.)
    ax.plot(range(len(data)), data, color='k', marker='', linestyle='-', alpha=.5, mew=1.)
    ax.set_xlabel('t')
    ax.set_ylabel('T')
    plt.suptitle('Title')
    fig.savefig(filename, format='png')
    plt.close()

def build_cm():
    
    candidates = [(60, 822), (829, 159), (832, 346)]
    for i in range(len(candidates)):
      ind1, ind2 = candidates[i]
      plot(ind1, ind2, 'comp_%s_%s.png'%(ind1, ind2))

if __name__ == '__main__':
    n = 40
    data = pd.read_csv('data.csv')
    build_cm()

我不知道样本输出中的连续数据来自哪里,因为跟踪是从不同部分收集的。 但是,我会使用屏蔽数组来解决这个问题:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def plot(data, indexes, n = 40, filename='test.png'):
    fig, ax = plt.subplots(figsize=(20,6))
    
    idx = np.asarray(indexes)
    mask =  np.tile(np.concatenate([n * [True], n * [False]]), idx.shape[0])
    vals = np.asarray([data[i:i+n] for i in idx.flat])
    vals_idx = np.arange(vals.size)
    
    ax.plot(np.ma.masked_where(mask, vals_idx), np.ma.masked_where(mask, vals.flat), color='g', marker='', linestyle='-', lw=2., mew=2.)
    ax.plot(np.ma.masked_where(~mask, vals_idx), np.ma.masked_where(~mask, vals.flat), color='b', marker='', linestyle='-', lw=2., mew=2.)

    ax.set_xlabel('t')
    ax.set_ylabel('T')
    plt.suptitle('Title')    
    #fig.savefig(filename, format='png')
    #plt.close()
    plt.show()

if __name__ == '__main__':
    n = 40
    data = pd.read_csv("test.csv")
    candidates = [(60, 822), (829, 159), (832, 346)]
    output_name = "test.png"
    plot(data, candidates, n, output_name)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM