user3452643
user3452643

Reputation: 182

Altair shortened form of x-axis label issues

I'm trying to make the x axis be a slice of the string of longspecies, but I want longspecies to show up as the legend. I've tried a couple different ways and without the domain the below code works, but once you add the domain it at least doubles the length of the x axis, probably adding the correlated values from species and longspecies.

I'm not sure how to just use longspecies and slice the tickmarks either (value and label seem like they aren't what I'm looking for). Any help would be appreciated.

the data setup:

testspecies = ['oak', 'elm', 'willow']
testmean = [np.random.rand()*25+75 for _ in range(len(testspecies))]
testlongspecies = [testspecies[i] + ' long form name' for i in range(len(testspecies))]
testset = zip(testmean, testspecies, testlongspecies, strict=True)
testdf = pd.DataFrame(testset, columns = ['average', 'species', 'long species'])

The chart:

alt.Chart(testdf).mark_bar().encode(
    alt.X('species', title = None),
    alt.Y('average', title = 'mean', scale = alt.Scale(domain = (50,100))),
    color='long species'
)

Upvotes: 1

Views: 375

Answers (1)

joelostblom
joelostblom

Reputation: 49064

If I add clip=True inside the mark, I get the following graph which sounds like it is what you are looking for (the x-axis labels are shorter than those in the legend):

alt.Chart(testdf).mark_bar(clip=True).encode(
    alt.X('species', title = None),
    alt.Y('average', title = 'mean', scale = alt.Scale(domain = (50,100))),
    color='long species'
)

enter image description here

Upvotes: 1

Related Questions