簡體   English   中英

如何在 Altair 水平條形圖中將軸標簽移動到另一側

[英]How to move axis label to opposite side in Altair horizontal bar chart

我當前的圖表如下所示:

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

我以官方參考中的水平條形圖為例。 首先,我將 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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM