Reputation:
My current chart looks like this:
But I would like it to look like this with the percentages on the left side of the bar chart:
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
Upvotes: 1
Views: 1673
Reputation: 35115
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)
Upvotes: 2