gpt4 book ai didi

python - 从 csv 中读取值并使用 python 中的 matplotlib 绘制条形图

转载 作者:行者123 更新时间:2023-12-01 06:11:53 26 4
gpt4 key购买 nike

我正在尝试读取名为“StabilityResults.csv”的分隔 csv,并使用第 0 列中的 x 标签创建第 7 列的条形图。从第 0 列获取标签不是问题,但读取来自col7 到列表中似乎不能作为 matplotlib 中的有效输入。有没有办法转换我的值列表,以便它们可以在 matplotlib 中读取?

import matplotlib.pyplot as plt
import csv
import numpy as np

res = csv.reader(open('StabilityResults.csv'), delimiter=',')
res.next() # do not read header

mut = []
tot = []
a = 0
width = 0.2

for col in res:
mut.append(col[0])
tot.append(col[7])
a += 1

ind = arange(a)

p1 = plt.bar(ind,tot,width,color='r')
labs = plt.xticks(ind+width,mut)

plt.show()

我还使用 numpy 的 genfromtxt 函数阅读第 7 列,但这给出了一个也不起作用的数组。

tot2 = np.genfromtxt('StabilityResults.csv', delimiter=',', dtype=None, names=True, deletechars=',', usecols=[7])

最佳答案

您应该将数据转换为整数类型(或 float )

tot.append(int(col[7]))

关于python - 从 csv 中读取值并使用 python 中的 matplotlib 绘制条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485473/

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