简体   繁体   English

使用matplotlib的Python中的直方图

[英]Histograms in Python using matplotlib

I'm trying to make a histogram, and i've been doing some searches and trying to find the right code, but everything I try doesn't end up working. 我正在尝试制作直方图,并且一直在进行一些搜索并尝试找到正确的代码,但是我尝试的所有方法最终都无法正常工作。 This is my code right now, 这是我的代码,

import matplotlib.pyplot as plt
import numpy as np

with open('gaubg.csv') as f:
       v = np.loadtxt(f, delimiter= ',', dtype="float", skiprows=1, usecols='None')

plt.hist(v, bins=100)
plt.xlabel("G-r0")
plt.ylabel('# of stars')
plt.title("Bottom half g-r0")

plt.show()

gaubg.csv is a csv file that includes about 600,000 (float, not int) data points that have to do with the color of stars. gaubg.csv是一个csv文件,其中包含大约600,000个(浮点数,而不是int)数据点,这些数据点与星星的颜色有关。 Every time I run this through python, this is the error message that shows up 每次我通过python运行此命令时,都会显示此错误消息

Traceback (most recent call last): File "gaub.py", line 5, in v = np.loadtxt(f, delimiter= ',', dtype="float", skiprows=1, usecols='None') File "/sdss/ups/prd/numpy/v1_6_1/Linux/lib/python2.7/sitepackages/numpy/lib/npyio.py", line 794, in loadtxt vals = [vals[i] for i in usecols] TypeError: list indices must be integers, not str 追溯(最近一次通话):文件“ gaub.py”,第5行,在v = np.loadtxt(f,delimiter =',',dtype =“ float”,skiprows = 1,usecols ='None')文件“ /sdss/ups/prd/numpy/v1_6_1/Linux/lib/python2.7/sitepackages/numpy/lib/npyio.py”,第794行,在loadtxt vals = [vals [i] for usecols中的i] TypeError:列表索引必须是整数,而不是str

I have no idea what that means. 我不知道那是什么意思。 I've been trying to fix the code but I'm not sure how. 我一直在尝试修复代码,但不确定如何。 If you could point out the obvious error(s) I'd be grateful! 如果您能指出明显的错误,我将不胜感激!

usecols= 'None' 

should be 应该

usecols= None

Or you can skip adding the usecols argument altogether. 或者,您可以完全跳过添加usecols参数。 When you specified a string numpy tried to iterate through each character with the assumption that it's an integer. 当您指定字符串numpy时,假定它是一个整数,它试图遍历每个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM