简体   繁体   中英

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

My current chart looks like this:

1

But I would like it to look like this with the percentages on the left side of the bar chart:

2

What would be the easiest way to change this? Should I add more axis properties to my chart code? This is the code I have so far for the visualization:

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

I used the horizontal bar graph in the official reference as an example. First I moved the y-axis to the left and set the label value there to the position of the overall y-axis.

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM