【问题标题】:How to plot (mean, SD, median, variance) histogram on python如何在python上绘制(均值、标准差、中值、方差)直方图
【发布时间】:2024-06-05 05:00:01
【问题描述】:

我有这个代码:

import pandas as pd
import matplotlib.pyplot as plt

allData= None
allData = pd.read_csv("football.csv")

print("mean is:",allData['Points'].mean())
print("median is:",allData['Points'].median())
print("variance is:",allData['Points'].var())
print("SD is:",allData['Points'].std())

我可以使用以下方法绘制所有数据:

allData.plot.bar()

结果:

我的问题:是否有任何可能的方法或内置函数可以为一列(在我的示例中为点列)绘制统计直方图(平均值、标准差、中值、方差)。

“football.csv”中使用的数据如下:

Team,Games,Wins,Losses,Draws,Goals,Goals Allowed,Points
Arsenal,38,26,9,3,79,36,87
Liverpool,38,24,8,6,67,30,80
Manchester United,38,24,5,9,87,45,77
Newcastle,38,21,8,9,74,52,71
Leeds,38,18,12,8,53,37,66
Chelsea,38,17,13,8,66,38,64
West_Ham,38,15,8,15,48,57,53
Aston_Villa,38,12,14,12,46,47,50
Tottenham,38,14,8,16,49,53,50
Blackburn,38,12,10,16,55,51,46
Southampton,38,12,9,17,46,54,45
Middlesbrough,38,12,9,17,35,47,45
Fulham,38,10,14,14,36,44,44
Charlton,38,10,14,14,38,49,44
Everton,38,11,10,17,45,57,43
Bolton,38,9,13,16,44,62,40
Sunderland,38,10,10,18,29,51,40
Ipswich,38,9,9,20,41,64,36
Derby,38,8,6,24,33,63,30
Leicester,38,5,13,20,30,64,28

【问题讨论】:

    标签: python python-3.x plot histogram


    【解决方案1】:

    我认为为一列绘制汇总统计数据的直方图没有用处。相反,您可以使用直方图,然后为汇总统计数据添加线条,或者您也可以使用包含这些统计数据的图,例如箱线图或小提琴图。

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    # histogram with lines for summary stats
    df.Points.hist()
    
    # add your own colors and linestyles for each stat
    summary_stats = (
        (5, {color}, {linestyle}),
        (10, {color}, {linestyle}),
        (15, {color}, {linestyle}),
    )
    
    for item in summary_stats:
        x, color, linestyle = item
        plt.axvline(x=x, color=color, linestyle=linestyle)
    
    # plot a boxplot
    # if you are in ipython terminal with autodiplay
    # then put both commands on same line w/ semicolon
    # sns.boxplot(df.Points); sns.stripplot(df.Points)
    sns.boxplot(df.Points, color='lightgray')
    sns.stripplot(df.Points)
    
    # plot a violin plot
    # same caveat as above for boxplot in ipython terminal
    sns.violinplot(df.Points, color='lightgray')
    sns.stripplot(df.Points)
    
    

    【讨论】: