如何在 Altair 水平条形图中将轴标签移动到另一侧

时间:2021-06-25 02:54:13

标签: python altair

我当前的图表如下所示:

1

但我希望它看起来像这样,条形图左侧的百分比是这样的:

2

改变这种状况的最简单方法是什么?我应该在图表代码中添加更多轴属性吗?这是我迄今为止的可视化代码:

bars = alt.Chart(percentages_df).mark_bar().encode(
    y=alt.Y('EMOJI',sort='-x'), 
    x=alt.X('PERCENT', axis=None)
)
    
    
text = bars.mark_text(
    align='left',
#    baseline='middle',
    dx=3
).encode(
    text=alt.Text('PERCENT_TEXT:N')
)

chart=(text+bars).configure_mark(
    color='#DAC352'
).configure_scale(
    bandPaddingInner = 0.1
).properties(
    width = 450,
    height = 180
).configure_view(
    strokeWidth = 0
)

chart

1 个答案:

答案 0 :(得分:1)

我以官方参考中的水平条形图为例。首先,我将 y 轴向左移动,并将那里的标签值设置为整个 y 轴的位置。

import altair as alt
from vega_datasets import data

source = data.wheat()

bars = alt.Chart(source).mark_bar().encode(
    x='wheat:Q',
    y=alt.Y("year:O", axis=alt.Axis(ticks=False, domain=False, offset=25))
)

text = bars.mark_text(
    align='right',
    baseline='middle',
    dx=3
).encode(
    x=alt.value(-5),
    text='wheat:Q'
)

(bars + text).properties(height=900)

enter image description here

相关问题