gpt4 book ai didi

python - 用于在 Python 中绘制箱线图的元组、列表和 Numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-04 09:17:29 25 4
gpt4 key购买 nike

我正在尝试为多个 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 数组之间的差异会阻止此箱线图工作。

示例错误消息是:

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'

最佳答案

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

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

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

elapsed_list = array(elapsed_list, dtype=float)

关于python - 用于在 Python 中绘制箱线图的元组、列表和 Numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7258144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com