python - 使用 Python 和 ggplot 绘制平均值

标签 python graph ggplot2 bar-chart mean

我有以下代码生成的图表:

enter image description here

from pandas import *
from ggplot import *

plot = ggplot(data, aes('x','y')) \ #from dataframe 'data', columns x and y
+ geom_bar(stat='bar', fill='blue') + ggtitle('Graph of X and Y') \
+scale_x_continuous(name="X-Axis", breaks=[0, 4, 8, 12, 16, 20, 23], \
                    labels=["Midnight", "4:00am", "8:00am", "12:00pm","4:00pm","8:00pm","11:00pm"])\
+ylab("Y-Axis") + xlim(0, 23) 

print plot

y 表示的变量是几周内每小时事件数的计数。我想检查该时间段内每小时的平均事件数,而不是每小时的事件总数。

如何在 Python 中使用 ggplot 绘制“y”的平均值,而不是仅仅绘制“y”?

谢谢!

编辑:

所以,我想我真正需要的是一种获取每小时平均 y 事件 (x) 的方法。目前,当我尝试这样做时,我会返回一个在所有小时内具有相同值的图表。

最佳答案

我不确定代码中的 y 值来自何处,但对任何数据集进行平均的基本方法是将所有值加在一起,然后将总和除以值的数量。

因此,您可以使用这样的函数从值列表中生成平均值:

def average(list_):
    output = 0
    for i in list_;
         output += i
    output /= len(list_)
    return output

关于python - 使用 Python 和 ggplot 绘制平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28178457/

相关文章:

algorithm - 线性时间单对最短路径算法?

javascript - 在 Highcharts 图表中动态定位文本和圆圈?

r - 是否可以将 geom_ribbon 扩展到 xlimits?

python - 从多个 HDF5 文件/数据集链接数据集

python - 如何为一个 Python/C 扩展源文件指定不同的编译器标志?

python - 显示为 Python 列表类型的 Matplotlib 图

r - 将世界地图子集为北温带纬度 ggplot2

python - Pandas:每行采用一个字符串,用逗号分隔,并向列表中添加唯一的单词

python - 通过减去 pandas 列表中的两列来返回新列

r - 当 aes(group = ...) 而不是 aes(fill/shape = ...) 时,闪避误差条的位置错误