Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中绘制平均数据直方图?_Python_Matplotlib_Histogram - Fatal编程技术网

如何在python中绘制平均数据直方图?

如何在python中绘制平均数据直方图?,python,matplotlib,histogram,Python,Matplotlib,Histogram,我有一个类似这样的历史记录: 你看,每年(X)都有几个Y,所以柱状图有重叠条。 如何仅为max Y构建它?你的意思是什么 我的代码是: My dataframe is: Data Amount 1996 65155 1984 88705 1996 115551 2010 87222 1995 3043 1994 54789 2007 87655 1996 55189

我有一个类似这样的历史记录:

你看,每年(X)都有几个Y,所以柱状图有重叠条。 如何仅为max Y构建它?你的意思是什么

我的代码是:

  My dataframe is:
    Data  Amount
    1996    65155
    1984    88705
    1996    115551
    2010    87222
    1995    3043
    1994    54789
    2007    87655
    1996    55189
    2005    34914
    2005    122643
    1995    111700
    1996    64065
    1995    76783
    1994    85687
    1995    88515
    1996    48352
    1995    315025
    1995    80074
    1995    133998
    2000    40918
    2000    108585
    1996    119506
    2003    105385
    2003    93374
    1996    63970
    1995    15261
    1996    128078
    1995    83593
    1994    54544
    2006    108167
    1996    141421
    2005    83725

year=df[['Year']].as_matrix()
amount=df[['Amount']].as_matrix()

stepsize = 10
fig, ax = plt.subplots()
ax.bar(year, amount, width=1)
start, end = ax.get_xlim()
ax.set_xlabel('Year')
ax.set_ylabel('Amount')
ax.set_title(r'Amount - year')
ax.xaxis.set_ticks(np.arange(start, end, stepsize))
plt.show()

由于您正在使用pandas读取数据,因此可以使用dataframe对数据进行分组、根据需要进行聚合并绘制这些值。在下面的示例中,我使用了
max
值,但您也可以使用
mean
min
。。。不管怎样

还要注意我是如何使用
StringIO
使这个示例完全可以复制/粘贴的

from io import StringIO
import pandas

datafile = StringIO("""\
Year  Amount
1996    65155
1984    88705
1996    115551
2010    87222
1995    3043
1994    54789
2007    87655
1996    55189
2005    34914
2005    122643
1995    111700
1996    64065
1995    76783
1994    85687
1995    88515
1996    48352
1995    315025
1995    80074
1995    133998
2000    40918
2000    108585
1996    119506
2003    105385
2003    93374
1996    63970
1995    15261
1996    128078
1995    83593
1994    54544
2006    108167
1996    141421
2005    83725
""")

df = pandas.read_csv(datafile, sep='\s+')
ax = df.groupby(by=['Year']).max().plot.bar(legend=False)
ax.set_ylabel("Amount")

您需要制作一个可复制的示例(包括从零开始生成代表性数据的代码,没有本地文件、数据库连接等)好的,问题是编辑的,就像我说的,“没有本地文件”,您是世界上唯一拥有
data.csv
的人。这样更好,但这仍然取决于帮助您将数据复制并粘贴到文本文件或使用
pandas.read_clipboard
自己的人。如果你用一个
StringIO
对象来消除这种摩擦,那就太理想了。这是一个获取数据的问题:)但是你有什么理论上的想法吗?我该怎么做?。。