牛郎星分面图可以有交替的背景色吗?

2024-04-19 00:53:55 发布

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

可以在刻面图中设置备用背景色吗?以下尝试将填充设置为等于标称列的结果是所有黑色背景

df = pd.DataFrame(data = {'source': ['a','a','a','a','a','b','b','b','b','b','c','c','c','c','c']
                   ,'x':[5,4,3,2,1,5,4,3,2,1,5,4,3,2,1]
                   ,'y':[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
                   ,'fill':['yes','yes','yes','yes','yes','no','no','no','no','no','yes','yes','yes','yes','yes']})

print(df)

   source  x  y fill
0       a  5  1  yes
1       a  4  2  yes
2       a  3  3  yes
3       a  2  4  yes
4       a  1  5  yes
5       b  5  1   no
6       b  4  2   no
7       b  3  3   no
8       b  2  4   no
9       b  1  5   no
10      c  5  1  yes
11      c  4  2  yes
12      c  3  3  yes
13      c  2  4  yes
14      c  1  5  yes

alt.Chart(df).mark_line().encode(x='x:Q',y='y:Q'
        ).properties(height=100,width=200
        ).facet(column='source').configure_view(fill='fill:N')

结果: black background

所需的结果是如下所示的交替背景色: alternating background


Tags: nosourcedataframedfdataaltfillyes
2条回答

不,目前在Altair的刻面图中无法做到这一点(Vega Lite不提供刻面背景编码;当您写入fill='fill:N'时,渲染器将fill:N解释为不存在的HTML颜色的名称)

如果需要交替背景,当前唯一的方法是手动连接具有不同背景的图表

对于手动连接图表,我认为不可能将图表连接到背景或配置对象。解决方法可以是在背景层中手动创建矩形标记:

import altair as alt
from vega_datasets import data

source = data.cars()
chart = alt.Chart(source).mark_circle().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
)

bg = chart.mark_rect(color='coral').encode(
    x=alt.value(0),
    y=alt.value(0),
    x2=alt.value(400),
    y2=alt.value(300))

(bg + chart) | (bg.mark_rect(color='gold') + chart)

enter image description here

相关问题 更多 >