Python中绘制箱线图时元组、列表和Numpy数组的比较

1 投票
2 回答
2854 浏览
提问于 2025-04-17 00:55

我正在尝试为几个csv文件中的一列数据绘制箱线图(当然不包括表头),但在处理元组、列表和数组时遇到了一些困惑。以下是我目前的进展:

    #!/usr/bin/env python

    import csv
    from numpy import *
    import pylab as p
    import matplotlib

    #open one file, until boxplot-ing works
    f = csv.reader (open('2-node.csv'))
    #get all the columns in the file
timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,bytes,Latency = zip(*f)

    #Make list out of elapsed to pop the 1st element -- the header
    elapsed_list = list(elapsed)
    elapsed_list.pop(0)

    #Turn list back to a tuple
    elapsed = tuple(elapsed_list)

    #Turn list to an numpy array 
    elapsed_array = array(elapsed_list)

    #Elapsed Column statically entered into an array
    data_array = ([4631, 3641, 1902, 1937, 1745, 8937] )

    print data_array #prints in this format: ([xx,xx,xx,xx]), .__class__ is list ... ?
    print elapsed    #prints in this format: ('xx','xx','xx','xx'), .__class__ is tuple
    print elapsed_list # #print in this format: ['xx', 'xx', 'xx', 'xx', 'xx'], .__class__ is list
    print elapsed_array #prints in this format: ['xx' 'xx' 'xx' 'xx' 'xx'] -- notice no commas, .__class__ is numpy.ndarray

    p.boxplot (data_array) #works
    p.boxplot (elapsed) # does not work, error below
    p.boxplit (elapsed_list) #does not work
    p.boxplot (elapsed_array) #does not work
    p.show()

对于箱线图,第一个参数是一个“数组或向量序列”,所以我认为elapsed_array应该可以用……?但是data_array是一个“列表”,却可以用……而elapsed_list也是一个“列表”,却不行……?有没有更好的方法呢……?

我对python还比较陌生,想了解一下元组、列表和numpy数组之间的区别,为什么会导致这个箱线图无法正常工作。

示例错误信息是:

Traceback (most recent call last):
  File "../pullcol.py", line 32, in <module>
    p.boxplot (elapsed_list)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 1962, in boxplot
    ret = ax.boxplot(x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 5383, in boxplot
    q1, med, q3 = mlab.prctile(d,[25,50,75])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mlab.py", line 946, in prctile
    return _interpolate(values[ai],values[bi],frac)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mlab.py", line 920, in _interpolate
    return a + (b - a)*fraction
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

2 个回答

1

我对numpy和matplotlib不太熟悉,但从描述和运行的情况来看,它似乎在寻找一个嵌套的序列。也就是说,data_array之所以能用,是因为它是一个包含列表的元组,而你其他的输入只有一层深。

至于它们之间的区别,列表是一种可以改变的对象序列,元组是一种不可改变的对象序列,而数组是一种可以改变的字节、整数、字符的序列(基本上是1、2、4或8字节的值)。

这里有一个链接,指向Python文档关于 5.6. 序列类型 的部分,从那里你可以跳转到关于列表、元组、数组或Python中其他序列类型的更详细信息。

4

elapsed 里面存的是字符串。Matplotlib 需要整数或浮点数才能画图。所以你可以试着把 elapsed 里的每个值转换成整数。你可以这样做:

elapsed = tuple([int(i) for i in elapsed])

或者像 FredL 在下面评论的那样:

elapsed_list = array(elapsed_list, dtype=float)

撰写回答