Tino Kanngießer
Tino Kanngießer

Reputation: 136

Is there a way to show percentage (0-100%) in an Altair normalized Stacked Bar Chart?

Is there a way to show 0% - 100% instead 0.0 - 1.0 in an Altair Normalized Stacked Bar Chart ?

I tried

x=alt.X('sum(yield)', stack="normalize", scale=alt.Scale(range=[0, 100]))

but this does not give the expected result.

import altair as alt
from vega_datasets import data

source = data.barley()

alt.Chart(source).mark_bar().encode(
    x=alt.X('sum(yield)', stack="normalize"),
    y='variety',
    color='site'
)

Upvotes: 5

Views: 6511

Answers (1)

jakevdp
jakevdp

Reputation: 86523

You can use the axis format argument along the axis in question. For example:

import altair as alt
from vega_datasets import data

source = data.barley()

alt.Chart(source).mark_bar().encode(
    x=alt.X('sum(yield)', stack="normalize", axis=alt.Axis(format='%')),
    y='variety',
    color='site'
)

enter image description here

Altair uses d3 format codes; more information on these is available here.

Upvotes: 12

Related Questions