是否可以mark_rect()
在Altair python图中使用创建的热图中添加一些间距?图1中的热图将转换为图2中的热图。您可以假定这是来自a,dataframe
并且每一列都对应一个变量。我故意这样画白条,以避免任何硬编码的索引解决方案。基本上,我正在寻找一种解决方案,可以提供列名和/或索引名以获取垂直和/或水平绘制的白色间距。
您可以使用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
)
Run Code Online (Sandbox Code Playgroud)
创建这些带区的一种方法是使用自定义箱对图表进行分面。这是一种方法,用于pandas.cut
创建垃圾箱。
import pandas as pd
import altair as alt
df = (pd.util.testing.makeDataFrame()
.reset_index(drop=True) # drop string index
.reset_index() # add an index column
.melt(id_vars=['index'], var_name="column"))
# To include all the indices and not create NaNs, I add -1 and max(indices) + 1 to the desired bins.
bins= [-1, 3, 9, 15, 27, 30]
df['bins'] = pd.cut(df['index'], bins, labels=range(len(bins) - 1))
# This was done for the index, but a similar approach could be taken for the columns as well.
alt.Chart(df).mark_rect().encode(
x=alt.X('index:O', title=None),
y=alt.Y('column:O', title=None),
color="value:Q",
column=alt.Column("bins:O",
title=None,
header=alt.Header(labelFontSize=0))
).resolve_scale(
x="independent"
).configure_facet(
spacing=5
)
Run Code Online (Sandbox Code Playgroud)
请注意resolve_scale(x='independent')
不要在每个面上重复轴,并且spacing
参数用于configure_facet
控制间距的宽度。我labelFontSize=0
在标题中进行了设置,这样我们就不会在每个方面的顶部看到 bin 名称。
归档时间: |
|
查看次数: |
186 次 |
最近记录: |