试图摆脱matplotlib图中的列

时间:2017-10-11 14:38:48

标签: python numpy matplotlib

我们有来自Vicon Nexus的动作捕捉软件附带的代码。使用Python,我们生成从EMG模拟源捕获的图形。 包含的幅度与频率列无关。我们可以注释掉填充第二列的代码部分,但它仍然在第2列留下空的子图。

def plot(axrow, ax, ay, alim, bx, by, blim, emgLabel):
    axrow[0].plot(ax, ay, color='blue', label=emgLabel)
    axrow[0].set_xlim(alim)
    axrow[0].set_xlabel('Time / s')
    axrow[0].set_ylabel('Volt. / V', fontsize=9)
    axrow[0].legend()
    axrow[0].locator_params(axis='y', nbins=3)
    #axrow[1].plot(bx, by, color='red', label=emgLabel)
    #axrow[1].set_xlim(blim)
    #axrow[1].set_xlabel('Frequency / Hz')
    #axrow[1].set_ylabel('Ampl./ a.u.', fontsize=9)
    #axrow[1].legend()
    #axrow[1].locator_params(axis='y', nbins=3)  

Second Column Empty

当我们将子图的参数从2更改为1时,只显示一列但是图完全为空

nrows = len(channelId)
fig, axes = plt.subplots(nrows, 1)

One Column, No Graphs

有8个channelIDs

任何帮助将不胜感激。谢谢

编辑:抱歉延迟回复。我们终于通过使用“squeeze = false”来找出解决方案。

为了清晰起见,这是代码的完整性

from __future__ import division, print_function

import ViconNexus
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, lfilter


# define butterworth bandpass filter
def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = butter(order, [low, high], btype='band')
    return b, a


def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y

   #plot(row, timeLineCut, x, alim, freq, np.abs(y), blim, emgLabel)
def plot(axrow, ax, ay, alim, bx, by, blim, emgLabel):
    axrow[0].plot(ax, ay, color='blue', label=emgLabel)
    axrow[0].set_xlim(alim)
    axrow[0].set_xlabel('Time / s')
    axrow[0].set_ylabel('Volt. / V', fontsize=9)
    axrow[0].legend()
    axrow[0].locator_params(axis='y', nbins=3)
    #axrow[1].plot(bx, by, color='red', label=emgLabel)
    #axrow[1].set_xlim(blim)
    #axrow[1].set_xlabel('Frequency / Hz')
    #axrow[1].set_ylabel('Ampl./ a.u.', fontsize=9)
    #axrow[1].legend()
    #axrow[1].locator_params(axis='y', nbins=3)  


vicon = ViconNexus.ViconNexus()

# Extract information from active trial
subjectName = vicon.GetSubjectNames()[0]
sessionLoc = vicon.GetTrialName()[0]
trialName = vicon.GetTrialName()[1]


analogId = 3
emgOutId = 1

channelNames = vicon.GetDeviceOutputDetails(3, 1)[4]
channelId = vicon.GetDeviceOutputDetails(3, 1)[5]


nrows = 4
fig, axes = plt.subplots(nrows, 1, squeeze=False)


# over all analog channels
for ii, row in zip(range(nrows), axes):

    emgId = channelId[ii]
    emgData = vicon.GetDeviceChannel(analogId, emgOutId, emgId)[0]
    emgDataRate = vicon.GetDeviceChannel(analogId, emgOutId, emgId)[2]
    emgDataArray = np.asarray(emgData)
    timeLine = np.arange(emgDataArray.size)

    # write routine to select Left / right from trial_name

    if channelNames[ii][-1] == 'R':
        testEvent = vicon.GetEvents(subjectName, 'Right', 'Foot Strike')
        testEventOff = vicon.GetEvents(subjectName, 'Right', 'Foot Off')
    else:
        testEvent = vicon.GetEvents(subjectName, 'Left', 'Foot Strike')
        testEventOff = vicon.GetEvents(subjectName, 'Left', 'Foot Off')

    trajDataRate = vicon.GetFrameRate()

    if len(testEventOff[0]) == 1:
        startFrameTraj = testEvent[0][0]
        footOffFrame = testEventOff[0][0]
        stopFrameTraj = testEvent[0][1]
    else:
        startFrameTraj = testEvent[0][0]
        footOffFrame = testEventOff[0][1]
        stopFrameTraj = testEvent[0][1]


    startFrameAnal = int(startFrameTraj * (emgDataRate/trajDataRate))
    footOffAnal = int(footOffFrame * (emgDataRate/trajDataRate))
    stopFrameAnal = int(stopFrameTraj * (emgDataRate/trajDataRate))

    emgDataCut = emgDataArray[startFrameAnal:stopFrameAnal]
    T = 1.0 / 4000.0  # time per frame
    tEnd = T * (emgDataCut.size - 1)
    timeLineCut = np.linspace(0.0, tEnd, num=emgDataCut.size)
    #timeLineCut = np.arange(emgDataCut.size)

    # do some filtering
    fs = emgDataRate  # sample rate
    lowCut = 40.0  # Hz
    highCut = 800.0  # Hz
    order = 6  # or 2, 4 ...?
    x = butter_bandpass_filter(emgDataCut, lowCut, highCut, fs, order)

    # another style for fft
    Y = emgDataCut
    y = np.fft.fft(Y)
    freq = np.fft.fftfreq(len(Y), T)

    alim = [0, tEnd+1]
    blim = [0, 600]
    emgLabel = channelNames[ii]
    plot(row, timeLineCut, x, alim, freq, np.abs(y), blim, emgLabel)

fig.suptitle('EMG and spectrum - %s - %s' % (subjectName, trialName))
fig.set_size_inches(6, 10)  # for landscape
fig.savefig('%s%s_%s_EMG.pdf' % (sessionLoc, subjectName, trialName))

1 个答案:

答案 0 :(得分:0)

解决方案是你使用" squeeze = False"在subplots参数中,将colmuns的数量切换为1

$reporter->report($data)