试图删除matplotlib图中的列

2024-09-25 00:36:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我们有这个代码,来自维康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个channelid

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

编辑:很抱歉延迟回复。通过使用“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))

Tags: plotnporderfssetbutternrowsalim