为什么 Altair 热图上的 y 轴是倒置的?如何反转它?

Why is the y-axis upside down on Altair heatmaps and how to reverse it?

查看来自 Altair 的 basic heatmap 示例,我注意到 y 轴反转了。 1)有什么特别的原因吗? 2)我怎样才能扭转它?

我尝试查看文档,但一直无法找到如何操纵坐标轴以反转 y 轴。

您可以使用 y 编码的 sort 属性 来更改默认排序顺序。例如:

import altair as alt
import numpy as np
import pandas as pd

# Compute x^2 + y^2 across a 2D grid
x, y = np.meshgrid(range(-5, 5), range(-5, 5))
z = x ** 2 + y ** 2

# Convert this grid to columnar data expected by Altair
source = pd.DataFrame({'x': x.ravel(),
                     'y': y.ravel(),
                     'z': z.ravel()})

alt.Chart(source).mark_rect().encode(
    x='x:O',
    y=alt.Y('y:O',
        sort=alt.EncodingSortField('y', order='descending')),
    color='z:Q'
)