开发者

Pylab.bar plotting difficulty

开发者 https://www.devze.com 2023-03-26 12:53 出处:网络
I have been putting together this plotting program. It strips out numbers from a set of files, does some math, and then should plot out开发者_JS百科 a bar chart to show how the numbers change.

I have been putting together this plotting program. It strips out numbers from a set of files, does some math, and then should plot out开发者_JS百科 a bar chart to show how the numbers change.

From what I can tell, the pylab.bar() part of the program is unable to properly take the calculated data and use it. The program keeps asking to have the height set to some number or a scalar. I think the problem is when I start to convert things to strings, but I am not sure.

import glob
import numpy
from numpy import *
import pylab
from pylab import *


lable = "c 1n0 an1 an2 an3 an4".split()
fnam = "Cmos*.csv"
opfnam = glob.glob(fnam)
for s in opfnam:
    words = s.strip().split("[]")
    a = open(words[0], "r").readlines()
    b = str(a).split(',')
    simp = str(b).translate(None, """'()["n]""")
    t = simp.split(',')
    c = t[2]
    an0 = t[3]
    an1 = t[4]
    an2 = t[5]
    an3 = t[6]
    an4 = t[7]
    tie = t[0]+t[1]
    data = c,an0, an1, an2, an3, an4
    print data
    y = (-1,0,1,2,3,4)
    bar(y, data, width = .75 )
    show()"


I think you are right, it has to do with your data still being in string format. Here is a striped down version of your program:

import pylab
from pylab import *

simp = "11,22,1,.5,.75,1,1.2,.9"
t = [float(val) for val in simp.split(',')]
c = t[2]
an0 = t[3]
an1 = t[4]
an2 = t[5]
an3 = t[6]
an4 = t[7]
tie = t[0]+t[1]
data = c,an0, an1, an2, an3, an4
print data
y = (-1,0,1,2,3,4)
bar(y, data, width = .75 )
show()

I set simp to a comma-separated list of random values; I'm guessing that is what simp is after your parsing of each line of the file. Notice I've used a list comprehension to convert each value of the split list into a float() value before I assigned the list to t. If you want to make t a list of integers, use int() in the place of float() of course.

0

精彩评论

暂无评论...
验证码 换一张
取 消