是否可以将黑色网格线添加到python Altair热图图中?

2024-10-02 22:37:49 发布

您现在位置:Python中文网/ 问答频道 /正文

有可能在牛郎星热图上添加黑色网格线吗

我知道可以用scale.bandPaddingInner来增加单元格之间的间距,我想也许可以用黑色来给间距上色

我想要的是这样的:Heatmap with Black Grid lines


Tags: withgridblack热图linesscale黑色间距
1条回答
网友
1楼 · 发布于 2024-10-02 22:37:49

是的,您可以使用rect标记的stroke属性来执行此操作。改编Altair文档中的heatmap example

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(stroke='black', strokeWidth=2).encode(
    x='x:O',
    y='y:O',
    color='z:Q'
)

enter image description here

相关问题 更多 >