user12755836
user12755836

Reputation:

How to remove ticks on Y-axis but maintain labels in Altair chart

My chart looks like this:

1

But I want it to match this chart almost exactly:

2

How can I remove the ticks on the Y-axis but keep the percent labels? Also, is there a way to create more space between the percent labels and the start of the bars like in the original image?

This is the code I'm using:

chart = alt.Chart(percentages_df).mark_bar(size=17, color= '#e6ab02').encode(
    # encode x as the percent, and hide the axis
    x=alt.X(
        'PERCENT',axis=None),
    y=alt.Y(
        'PERCENT_TEXT',
         axis=alt.Axis(tickCount=5,title='') ,sort='-y'))

text = alt.Chart(percentages_df).mark_text().encode(
    y=alt.Y('PERCENT_TEXT',axis=None, sort='-y'),
    text='EMOJI'
)
new_chart = alt.hconcat(text, chart).configure_view(strokeWidth=0).configure_axis(grid=False)
new_chart

Upvotes: 4

Views: 3001

Answers (1)

jakevdp
jakevdp

Reputation: 86330

You can do this with the tickSize axis property; for example:

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

alt.Chart(source).mark_bar().encode(
    x = 'b',
    y=alt.Y('a', axis=alt.Axis(tickSize=0))
)

enter image description here

Upvotes: 3

Related Questions