【问题标题】:Tuples Vs List Vs Numpy Arrays for Plotting a Boxplot in Python用于在 Python 中绘制箱线图的元组与列表与 Numpy 数组
【发布时间】:2011-11-07 15:49:02
【问题描述】:

我正在尝试为几个 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()

对于箱线图,第一个参数是“an array or a sequence of vectors”,所以我认为elapsed_array 会起作用...?但是data_array,一个“列表”可以工作...但是 elapsed_list` 一个“列表”不能...?有没有更好的方法来做到这一点...?

我对 python 还很陌生,我想了解元组、列表和 numpy-array 之间的差异如何阻止此箱线图工作。

错误信息示例:

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'

【问题讨论】:

    标签: python arrays list numpy matplotlib


    【解决方案1】:

    elapsed 包含字符串。 Matplotlib 需要整数或浮点数来绘制一些东西。尝试将elapsed 的每个值转换为整数。你可以这样做

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

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

    elapsed_list = array(elapsed_list, dtype=float)
    

    【讨论】:

    • 正要说同样的话 - 基本上使用类似“array(elapsed_list, dtype=float)”之类的东西来创建数组
    • 做到了!那么我如何判断 elapsed 是否包含字符串或其他内容......?是'xx',它是一个字符串...?它们看起来都像数字(-:
    • @KM,如果你查看elapsed 的元素之一的type,它会告诉你它是否是一个字符串:print type(elapsed[0]) 打印&lt;type 'str'&gt;;如果添加行print type(data_array[0]),您将看到&lt;type 'int'&gt;。是的,'xx' 表示它是一个字符串,但 type 方法是明确的。这能回答你的问题吗?
    【解决方案2】:

    我不熟悉 numpy 或 matplotlib,但仅从描述和工作原理来看,它似乎正在寻找一个嵌套的序列序列。这就是为什么 data_array 工作的原因,因为它是一个包含列表的元组,而您的所有其他输入只有一层深。

    至于区别,列表是可变的对象序列,元组是不可变的对象序列,数组是可变的字节、整数、字符序列(基本上是 1、2、4 或 8 字节值) .

    这里是关于 5.6. Sequence Types 的 Python 文档的链接,您可以从那里跳转到有关 Python 中的列表、元组、数组或任何其他序列类型的更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2011-06-23
      • 2021-03-28
      • 1970-01-01
      相关资源
      最近更新 更多