使用csv文件作为输入在python中绘制直方图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30691699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:50:05  来源:igfitidea点击:

plot histogram in python using csv file as input

pythoncsvpandasmatplotlib

提问by Nick

I have a csv file which contains two columns where first column is fruit name and second column is count and I need to plot histogram using this csv as input to the code below. How do I make it possible. I just have to show first 20 entries where fruit names will be x axis and count will be y axis from entire csv file of 100 lines.

我有一个 csv 文件,其中包含两列,其中第一列是水果名称,第二列是计数,我需要使用此 csv 作为以下代码的输入绘制直方图。我如何使它成为可能。我只需要显示前 20 个条目,其中水果名称将是 x 轴,计数将是整个 100 行 csv 文件中的 y 轴。

import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('data.csv', header = None ,quoting=2)

data.hist(bins=10)
plt.xlim([0,100])
plt.ylim([50,500])
plt.title("Data")
plt.xlabel("fruits")
plt.ylabel("Frequency")
plt.show()

I edited the above program to plot a bar chart -

我编辑了上面的程序来绘制条形图 -

import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('data.csv', sep=',',header=None)
data.values
print data
plt.bar(data[:,0], data[:,1], color='g')
plt.ylabel('Frequency')
plt.xlabel('Words')
plt.title('Title')

plt.show()

but this gives me an error 'Unhashable Type '. Can anyone help on this.

但这给了我一个错误“Unhashable Type”。任何人都可以帮助解决这个问题。

采纳答案by Ed Smith

You can use the inbuilt plot of pandas, although you need to specify the first column is index,

您可以使用 的内置图pandas,尽管您需要指定第一列是索引,

import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('data.csv', sep=',',header=None, index_col =0)

data.plot(kind='bar')
plt.ylabel('Frequency')
plt.xlabel('Words')
plt.title('Title')

plt.show()

If you need to use matplotlib, it may be easier to convert the array to a dictionary using data.to_dict()and extract the data to numpy array or something.

如果您需要使用matplotlib,使用将数组转换为字典data.to_dict()并将数据提取到 numpy 数组或其他东西可能会更容易。