question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting tick labels to list of strings

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
palewirecommented, Oct 17, 2018

Am I able to do this with date values ala:

axis=alt.Axis(values=[date(2008, 9, 1)]),
2reactions
jakevdpcommented, Jun 11, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change x-axis ticks to custom strings [duplicate] - Stack Overflow
I want to change the x-axis ticklabels to custom strings, but the following does not work. How can I set the ticklabels to...
Read more >
Setting tick labels from a list of values - Matplotlib
Using Axes.set_xticks causes the tick labels to be set on the currently chosen ticks. However, you may want to allow matplotlib to dynamically...
Read more >
Set or query x-axis tick labels - MATLAB xticklabels - MathWorks
This MATLAB function sets the x-axis tick labels for the current axes. ... Specify labels as a string array or a cell array...
Read more >
Matplotlib - Setting Ticks and Tick Labels - GeeksforGeeks
In this article, we are going to discuss how set Ticks and Tick labels in a graph. Ticks are the markers denoting data...
Read more >
Customizing Ticks | Python Data Science Handbook
We see here that each major tick shows a large tickmark and a label, while each minor tick shows a ... IndexFormatter, Set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found