utf8编码的绘图问题

时间:2018-07-17 19:30:43

标签: python utf-8

我试图绘制幅值与频率的二维图,并将其作为x和y位置的函数,然后引入第三个轴以显示y依赖性。但是,我拥有的简单2D图与数据不匹配,并且我不得不假设这是由于数据是utf8编码的事实。

下面是我的代码:

import numpy as np
import glob, os
import codecs
import re
import matplotlib.pyplot as plt
#-------------------------------------------------------------------------                   
os.chdir('C:/Users/elarrick/Desktop/201807161056')
dataFolders = glob.glob('./*.ltda')

dataLines = []
freq = []
OpenLoopMag = []
OpenLoopPhase = []
for item in dataFolders:
    name, ext = os.path.splitext(item)
    if ext == '.ltda':
        print item
        dataLines = []
        f = codecs.open(item, encoding='utf-8')
        for line in f:
            if '<p>' in line:
                dataLines.append(line)      #All lines with <p> are an entry in dataLines
        #print "\n\n", dataLines
        #break
        for item in dataLines:
            item = re.sub("<p>", "", item)
            item = re.sub("True</p>", "", item)
            item = item.replace(",", "")
            splitItem = item.split()
            #print splitItem
            freq.append(splitItem[0])
            OpenLoopMag.append(splitItem[1])
            OpenLoopPhase.append(splitItem[2])
        print "Frequencies: ", freq
        print "\n\n\n\n\n\nOpenLoopMag: ", OpenLoopMag
#   This is where I will make the plots for each x,y position
        name = name.strip(".\\")
        name = name.replace("-","NEG")
        #plt.semilogx(freq, OpenLoopMag)
        plt.plot(freq, OpenLoopMag)
        plt.xlabel("Frequency, (Hz)")
        plt.ylabel("Magnitude")
        plt.title("{0}".format(name))
        #plt.xlim([20,2000])
        #plt.ylim([-43.2,10.9])
        #ticks = [20,40,70,100,200,400,700,1000,2000]
        #plt.xticks(ticks,ticks)
        plt.savefig("plot_{0}.png".format(name))

#________ Clear the values for the next data folder_______#            
        freq = []
        OpenLoopMag = []
        OpenLoopPhase = []
        break
    else:
        print "Something went wrong - check ColorMap.py"
        sys.exit()

我对freqOpenLoopMag的值(为便于举例,仅几个)是:

Frequencies:  [u'20', u'20.2321728649916', u'20.4670409419452', u'20.7046355186148', u'20.944988245963', u'21.188131142377', u'21.4340965979342', ..., u'1977.04914182566', u'2000']


OpenLoopMag:  [u'10.9797774776683', u'9.40509858861171', u'8.38037523081066', u'7.84390252022723', u'7.36930058248386', u'6.97548426230449', u'6.59941431608661', ..., u'-42.4773549086419', u'-37.3733298906582']

因此,y轴的比例应为〜10。但是,当我绘制它们时,结果看起来如图所示(注意:轴看起来很糟糕是因为结果是所有8位数字,我无法确定比例):

Semilog Plot

我不知道为什么我的freqOpenLoopMag的数据不匹配,但是下图中的上图是所需的输出:

Actual Data

有人可以告诉我我在做什么错吗?

0 个答案:

没有答案