简体   繁体   English

plot pandas Dataframe 从 csv 加载到条形图错误

[英]plot pandas Dataframe loaded from csv to barplot error

I have a very simple file called classes_distribution.csv that was saved as csv file from a pandas dataframe.我有一个名为 classes_distribution.csv 的非常简单的文件,它从 pandas Z6A80705D5DF479C5553 中保存为 csv 文件Such a file has one line of data as follows:这样的文件有一行数据如下:

HUMAN,OTHER,SYMBOL,ABSTRACT,OBJECT,FLAG,PLANT,ANIMAL 65578,69372,51157,72559,59316,52337,60081,73700人类,其他,符号,抽象,OBJECT,标志,植物,动物 65578,69372,51157,72559,59316,52337,60081,73700

I am trying to load the contents of such a file as a pandas dataframe again, and I am trying to plot the contents as a barplot.我正在尝试将此类文件的内容加载为 pandas dataframe 再次,我正在尝试将 plot 内容作为条形图。 I don't want legends, that means, the x coordinate will contain the names of the classes (first line of the csv file), and the y axis will contain the number of occurrences (the second line of the csv file).我不想要图例,这意味着,x 坐标将包含类的名称(csv 文件的第一行),y 轴将包含出现次数(csv 文件的第二行)。

Here is what I tried to do这是我试图做的

import pandas as pd
df=pd.read_csv("frames_stats_plots/classes_distribution.csv")
df.plot(legend=False).bar(x=df.columns, y=df.values).get_figure().savefig('plot.png')

which returns me the following error:这返回了以下错误:

 File "<stdin>", line 1, in <module>
 File "/usr/local/lib/python3.10/dist-packages/matplotlib/__init__.py", line 1412, in inner
 return func(ax, *map(sanitize_sequence, args), **kwargs)
 TypeError: Axes.bar() missing 1 required positional argument: 'height'

What am I supposed to inform as height argument?作为身高参数,我应该告知什么? how to solve this issue?如何解决这个问题?

Here is one way to do it:这是一种方法:

import pandas as pd

df = pd.read_csv("frames_stats_plots/classes_distribution.csv")

# Reshape the dataframe so as to be suitable for a bar chart
df = df.T.reset_index().rename(columns={"index": "class", 0: "value"})

df.plot(kind="bar", x="class", y="value", legend=False)

Output: Output:

在此处输入图像描述

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

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