python - 为什么 Altair 热图上的 y 轴是颠倒的以及如何反转它?

标签 python heatmap yaxis altair

查看basic heatmap以 Altair 为例,我注意到 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'
)

enter image description here

关于python - 为什么 Altair 热图上的 y 轴是颠倒的以及如何反转它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58325670/

相关文章:

python - 使用 OpenCV 在鼠标光标下显示文本

python - 银行账户类。如何让它更强大/更高效? (面向对象)

r - 带有 pheatmap 的多面板图

python - 在 seaborn 热图中分隔组的线

r - 如何使用 2 个不同的 y 轴进行绘图?

python - Tkinter "entry"对象未更新 textvariable 变量(Python 3.4)

python - 在虚拟环境中安装我自己的 python 模块

python - matplotlib中的热图与pcolor?

r - ggplot2:如何在刻度线上结束 y 轴?

r - 使用基本 R 图并排绘制两个图,具有共享标题和共享 x 轴标签