在Altair中如何更改facet标题的位置?

4

如何将分面标题(在本例中为年份)移动到每个图表上方?默认情况似乎是在图表的侧面。这可以很容易地改变吗?

import altair as alt
from vega_datasets import data

df = data.seattle_weather()

alt.Chart(df).mark_rect().encode(
    alt.Y('month(date):O', title='day'),
    alt.X('date(date):O', title='month'),
    color='temp_max:Q'
).facet(
    row='year(date):N',
)

problematic plot

2个回答

2

一个通用的解决方案是使用header参数的labelOrient选项:

df = df[df.date.dt.year < 2014]  # make a smaller chart

alt.Chart(df).mark_rect().encode(
    alt.Y('month(date):O', title='day'),
    alt.X('date(date):O', title='month'),
    color='temp_max:Q'
).facet(
    row=alt.Row('year(date):N', header=alt.Header(labelOrient='top'))
)

heatmap with two years of data and labels on top


1
一种解决方案是移除行规范并将分面设置为单列。
import altair as alt
from vega_datasets import data

df = data.seattle_weather()

alt.Chart(df).mark_rect().encode(
    alt.Y('month(date):O', title='day'),
    alt.X('date(date):O', title='month'),
    color='temp_max:Q'
).facet('year(date):N', columns=1)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接