Altair: Setting tick labels to list of strings

Created on 11 Jun 2018  路  17Comments  路  Source: altair-viz/altair

Hi everyone,

Is there a way to set the tick labels in a chart to some list of strings? Here is the chart:

df=pd.DataFrame({'days_between': [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6],
                 'person': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,2],
                 'value': [10,11,12,13,14,15,10,11,12,13,14,15]})

alt.Chart(df).mark_square(size=400).encode(
    x=alt.X('days_between:Q', axis=alt.Axis(values=[1,2,3,4,5,6])),
    y=alt.Y('person:Q', axis=alt.Axis(values=[1,2])),
    color=alt.Color('value:Q', scale=alt.Scale(range=['green','yellow','red']))).properties(width=400, height=400).interactive()

visualization 5

I would have thought this would work (similar to matplotlib in some sense):

 alt.Axis(..., labels=['string 1', 'string 2', 'string 3', 'string 4', 'string 5', 'sting 6'])

Is there a way to do this when using quantitative data types as I am in the above example?

question

Most helpful comment

Am I able to do this with date values ala:

axis=alt.Axis(values=[date(2008, 9, 1)]),

All 17 comments

Altair is designed to properly display whatever data you input regardless of the type, unlike matplotlib which requires you to input quantitative data and then manually re-map the axes to whatever non-quantitative data you want it to represent.

So I'd say the best approach to this for Altair is to actually use the values of interest within the dataset itself, and then no manual axis label mapping will be necessary.

For example:

df=pd.DataFrame({'days_between': [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6],
                 'person': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,2],
                 'value': [10,11,12,13,14,15,10,11,12,13,14,15]})
df['days_between'] = df['days_between'].map({i + 1: 'ABCDEF'[i] for i in range(6)})

alt.Chart(df).mark_square(size=400).encode(
    x=alt.X('days_between:O'),
    y=alt.Y('person:Q', axis=alt.Axis(values=[1,2])),
    color=alt.Color('value:Q', scale=alt.Scale(range=['green','yellow','red']))
).properties(width=400, height=400).interactive()

visualization 8

Hi Jake, yes this works, but see now the interaction only works in one direction since the x axis is nominal. I understand that the interaction is designed to respond only to quantitative data types, so I was trying to work around that.

Ah, I see. No, there's not really any good way to do that that I'm aware of.

Maybe @domoritz or @kanitw have ideas from the vega-lite side of things.

It sounds like the real solution to your problem is not remapping axis labels, but to somehow allow selections to be bound to non-quantitative scales.

Yes, exactly! Nice way of specifying the issue directly in the spirit of grammar of graphics.

Selections work on categorical axes as well. See https://vega.github.io/vega-lite/examples/interactive_concat_layer.html.

Do you have an example of selections being bound to scales for non-quantitative axes? Seems not to be possible in Vega-Lite, unless I'm missing something.

Hmm, we can bind to input elements (https://vega.github.io/vega-lite/docs/bind.html#input-element-binding) but not to axes as far as I know. That seems to be missing in the grammar. I created https://github.com/vega/vega-lite/issues/3874.

I think that example is a bit different... what I have in mind is this except with x/y encodings that are nominal or categorical.

Currently, that results in a vega warning "[Warning] Scale bindings are currently only supported for scales with unbinned, continuous domains."

Do you want zooming and panning?

Binding a selection to the domain of a categorical scale should change the scale's domain, I think.

Yes, I believe what @Alcampopiano is asking for here is the ability to zoom & pan on non-quantitative scales. Sorry if that was unclear.

This is a frequent confusion for Altair users... we have that interactive() method that adds a selection: {type: 'interval', bind: 'scales'} and most users expect that to result in a zoomable/pannable plot regardless of whether the scales are quantitative or categorical.

If there were a different way to enable zooming & panning in the case of categorical scales, we could make altair use that instead.

We will not resolve this even if we support binding selections to the domain of ordinal scales. We can think about how scrolling changes a multi selection and maybe implement it that way. @arvind said he wanted to think about how the selection grammar can be extended.

Thanks for looking into this guys.

Al

Hi folks,

Panning/zooming categorical scales is a really interesting use case. As the interactions are continuous operations (e.g., I could pan/zoom just a little bit or a lot) but would need to map to a discrete domain, what would be the desired behaviour? For example, should we push/pop just a single element regardless of the magnitude of the pan/zoom? If not, how do we define proportionality (especially the default)?

@arvind Apologies for not getting back. I simply don't know enough to be able to offer any guidance. As unhelpful as it might be, as a user, I would enjoy behaviour similar to what plotly does when panning/zooming on a chart with a categorical axis. You can see an example here:

https://plot.ly/python/bar-charts/

Thanks for all your hard work.

Sincerely,
Allan

Am I able to do this with date values ala:

axis=alt.Axis(values=[date(2008, 9, 1)]),
Was this page helpful?
0 / 5 - 0 ratings

Related issues

SuperShinyEyes picture SuperShinyEyes  路  3Comments

LukeMathWalker picture LukeMathWalker  路  3Comments

bmcfee picture bmcfee  路  3Comments

fischcheng picture fischcheng  路  4Comments

firasm picture firasm  路  3Comments