matplotlib - ValueError:x和y必须具有相同的第一个维度

时间:2016-10-26 17:58:10

标签: python matplotlib error-handling

我一直致力于为课程编写一段代码,要求我们读取数据并绘制日志功率图。我的日志功率似乎都很好,但是一旦我尝试确定t值(即每个数据点的时间,使用25kHz的给定频率)并使用我的图中的值,我得到Valueerror:x和y必须具有相同的第一维度。

我认为某个地方我丢失了或在其中一个集合中获得了数据点,但我不知道在哪里。

import matplotlib.pyplot as plt #importing relevent libraries
import math
import numpy as np

filename = raw_input("Enter filename : ") #requesting file to input
filein = open(filename, "r") #opening requested file

v = [] #empty lists for data 
c = [] #v for voltage and c for current

for line in filein.readlines():
    if line.startswith("#"): #ignoring comments within data
        continue
    data = line.split(",") #seperating data 
    v.append(float(data[0])) #assigning data to lists
    c.append(float(data[1]))

vc = [] 
for i in range(0,len(v)):
    vc.append(v[i]*c[i]) #multiplying the two lists together

log = (math.log(math.e)) #addressing the log power
power = [p * (math.log(math.e)) for p in vc] #the log power of v(t)*i(t)

t = [x / 100000.0 for x in range (4, 10 *len(v), 4)] #creating time range and intervals from frequency

filein.close() #closing data file

plt.plot(t, power) #plotting the graph
plt.title("Log Power of Electrical Circuit")
plt.xlabel("t")
plt.ylabel("p(t)")
plt.show()

欢迎任何想法,谢谢

0 个答案:

没有答案