无法在重复的条形图上添加文本 - Altair

Cannot add text on repeated bar graphs - Altair

我用 altair 创建了一个重复的条形图,但我似乎无法将相同的文本(列 = 等级)添加到所有条形图上的条形图。这是我收到的错误消息:'RepeatChart' object has no attribute 'mark_text'

chart1 = alt.Chart(merged, width=500, height=300).mark_bar().encode(
    alt.X("City", type='ordinal', sort="-y"),
    alt.Y(alt.repeat("column"), type='quantitative'),
    alt.Color('City', legend=None, scale=alt.Scale(scheme='tableau20'))).repeat(
    column=['Cost of a bottle of water(City)', 'Pollution(Index score) (City)', 'Annual average hours worked', 
            'Outdoor activities(City)','Number of take out places(City)', 'Cost of a monthly gym membership(City)', 
            'Cost of Living Index', 'Rent Index', 'Cost of Living Plus Rent Index', 'Groceries Index', 
            'Restaurant Price Index','Local Purchasing Power Index'])
chart1  

text = chart1.mark_text(baseline='middle',
    dx=20,
    angle=340,
    color='black',
).encode(
    text=alt.Text('Rank'))

chart1 + text

您不能更改 RepeatChart 的标记。您需要先创建图层,然后在分层图表上使用重复。像这样:

chart1 = alt.Chart(merged, width=500, height=300).mark_bar().encode(
    alt.X("City", type='ordinal', sort="-y"),
    alt.Y(alt.repeat("column"), type='quantitative'),
    alt.Color('City', legend=None, scale=alt.Scale(scheme='tableau20')))

text = chart1.mark_text(baseline='middle',
    dx=20,
    angle=340,
    color='black',
).encode(
    text=alt.Text('Rank'))

(chart1 + text).repeat(
    column=['Cost of a bottle of water(City)', 'Pollution(Index score) (City)', 'Annual average hours worked', 
            'Outdoor activities(City)','Number of take out places(City)', 'Cost of a monthly gym membership(City)', 
            'Cost of Living Index', 'Rent Index', 'Cost of Living Plus Rent Index', 'Groceries Index', 
            'Restaurant Price Index','Local Purchasing Power Index'])

这是一个完整的可重现示例:

import altair as alt
from vega_datasets import data


source = data.cars()


c = alt.Chart(source).mark_circle().encode(
    x=alt.X('Weight_in_lbs:Q', title=''),
    y=alt.Y(alt.repeat(), type='quantitative'),
)

(c + c.mark_line()).repeat(['Horsepower', 'Acceleration'])