python - 在 altair 中被多个月装箱?

标签 python altair

这段代码创建了一个按月分类的条形图。

# Load libraries
import pandas as pd
import altair as alt
from vega_datasets import data

# Load data
df = data.seattle_weather()

# Create chart
alt.Chart(df).mark_bar().encode(
    x='month(date):T',
    y='mean(temp_max)')

是否可以每“n”个月装箱一次?例如,每个条形将是两个月(一月+二月、三月+四月、六月+七月...等)的数据。

enter image description here

最佳答案

唯一内置的多月分级是季度 time unit ;例如:

alt.Chart(df).mark_bar().encode(
    x='quarter(date):O',
    y='mean(temp_max)'
)

enter image description here

如果您想要更个性化的内容,可以使用 calculate transform包括适当的 vega expression根据需要对结果进行分组。例如:

alt.Chart(df).transform_calculate(
    group='month(datum.date) < 4 ? "Jan-Apr" : month(datum.date) < 8 ? "May-Aug" : "Sep-Dec"'
).mark_bar().encode(
    x='group:O',
    y='mean(temp_max)'
)

enter image description here

关于python - 在 altair 中被多个月装箱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59722354/

相关文章:

python - pyenv:pip:找不到命令

python - 为什么 (3.3==np.asarray([3.3])) 等于 [True] 而不是 False?

visual-studio-code - 无法从 VS Code 将 Altair 图表另存为 SVG

python - 程序在将字典传递给函数参数时显示 TypeError

python pandas合并多个csv文件

javascript - 在 Altair/Vega 中,有没有办法在点击时运行任意 Javascript 代码?

python - 查看转换后的数据

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

python - 如何在新选项卡中打开 Altair 图表中的 url (href)

python - 使用 Numpy 优化 Python 中的数组操作