I want to apply a number formatting and a string join to the axis labels, but I bumped into some weird behavior.
When I want to apply a format to an axis label, using format='~s' or labelExpr="format()" give different results. Also it is not clear what exactly datum.values and datum.labels returns as it sometimes works with labels.
If the scale is logarithmic, the issues are even more unclear to me; seems datum.values returns the minor ticks only or something.
Am I missing something obvious?
import altair as alt
import numpy as np
import pandas as pd
y = np.random.randint(1,1e6,1000)
y = np.linspace(1,1e6,1000)
x = np.linspace(1,1e3,1000)
df = pd.DataFrame({'x':x, 'y':y})
base = alt.Chart(df).mark_line(point=True).encode(
x=alt.X('x:Q'),
y=alt.Y('y:Q', axis=alt.Axis(format='~s', )),
).properties(width=300, height=200)
plot1 = base.encode(
y=alt.Y('y:Q', axis=alt.Axis(labelExpr="format(datum.value,'~s')+' ppm'"))
)
plot2 = base.encode(
y=alt.Y('y:Q', axis=alt.Axis(labelExpr="datum.value +' ppm'", format='~s'))
)
plot3 = base.encode(
y=alt.Y('y:Q', axis=alt.Axis(labelExpr="datum.label +' ppm'", format='~s'))
)
plot4 = base.encode(
y=alt.Y('y:Q', axis=alt.Axis(labelExpr="format(datum.label, '~s') +' ppm'"))
)
(base | plot1 | plot2) & (plot3 | plot4)
base = alt.Chart(df).mark_line(point=True).encode(
x=alt.X('x:Q'),
y=alt.Y('y:Q', scale=alt.Scale(type='log'), axis=alt.Axis(format='~s', )),
).properties(width=300, height=200)
plot1 = base.encode(
y=alt.Y('y:Q', scale=alt.Scale(type='log'), axis=alt.Axis(labelExpr="format(datum.value,'~s')+' ppm'"))
)
plot2 = base.encode(
y=alt.Y('y:Q', scale=alt.Scale(type='log'), axis=alt.Axis(labelExpr="datum.value +' ppm'", format='~s'))
)
plot3 = base.encode(
y=alt.Y('y:Q', scale=alt.Scale(type='log'), axis=alt.Axis(labelExpr="datum.label +' ppm'", format='~s'))
)
plot4 = base.encode(
y=alt.Y('y:Q', scale=alt.Scale(type='log'), axis=alt.Axis(labelExpr="format(datum.label, '~s') +' ppm'"))
)
(base | plot1 | plot2) & (plot3 | plot4)

