python - Altair 数据聚合

标签 python charts altair

我正在尝试在 Altair 上绘制以下内容: x = 每日时间戳 y = 记录数,因为数据是名义上的而非定量的

当我尝试按月或季度汇总 x 时,是否可以将 y 显示为每天的平均记录数而不是总和?

chart = alt.Chart(data).mark_bar().encode(
    x = alt.X('Date:T'),
    y = alt.Y('count(Name):Q'),
    color = alt.Color('descriptor:N')
)
chart

当我尝试做的时候

chart = alt.Chart(data).mark_bar().encode(
    x = alt.X('month(Date):T'),
    y = alt.Y('count(Name):Q'),
    color = alt.Color('descriptor:N')
)
chart

它按月的总数汇总,是否可以对每个月的计数进行平均?

最佳答案

您可以使用 Altair 的 transform 应用多个聚合句法。对于您的图表,您可以这样做:

alt.Chart(data).transform_aggregate(
    daily_count = 'count(Name)',
    groupby=['Date', 'descriptor']
).mark_bar().encode(
    x = alt.X('month(Date):O'),
    y = alt.Y('mean(daily_count):Q'),
    color = alt.Color('descriptor:N')
)

关于python - Altair 数据聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62547160/

相关文章:

python - Visual Studio Code - Python 调试 - 执行时步入外部函数的代码

Python - 无法重命名文件名中包含特殊字符的文件

javascript - ZingChart 饼图工具提示不显示

ios - 如何在 iOS 中创建类似 d3 的气泡图

python - 在 Altair (Python) 中,有没有办法省略条形图的中间值?

tooltip - 格式化 Altair 等值线图图例比例和工具提示

python - 在 Ubuntu 中设置 Google Cloud Managed VM 时出现 Docker Daemon 连接错误

python - 无法从脚本访问数据库

javascript - 如何在图表js中将x轴标签最小化为一天的时间

python - Altair 中的条形图 : ValueError: Faceted charts cannot be layered