gpt4 book ai didi

python - 在 python altair 图的热图中添加间距

转载 作者:行者123 更新时间:2023-11-28 20:12:38 24 4
gpt4 key购买 nike

是否可以在 Altair python 图中使用 mark_rect() 创建的热图中添加一些间距?图 1 中的热图将转换为图 2 中的热图。您可以假设这是来自 dataframe 并且每一列对应一个变量。我特意画了这样的白条,以避免任何硬编码索引解决方案。基本上,我正在寻找一种解决方案,我可以在其中提供列名称和/或索引名称以获取垂直和/或水平绘制的白色间距。

enter image description here

enter image description here

最佳答案

您可以使用 scale.bandPaddingInner 配置参数指定热图中的间距,该参数是一个介于 0 和 1 之间的数字,指定应填充的矩形标记的分数,默认为零.例如:

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='y:O',
color='z:Q'
).configure_scale(
bandPaddingInner=0.1
)

enter image description here

关于python - 在 python altair 图的热图中添加间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57137594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com