The Unfun Cat
The Unfun Cat

Reputation: 32028

Using xticks, ticks not aligned properly with figure

I use the simple code below to generate a boxplot with the labels of the x-values changed:

subplot(221)
boxplot(list_of_lists_of_lengths)
xlabel('max gap')
ylabel('cluster lengths')
xticks(range(len(max_distance)), max_distance, size='small')

As you can see the x values are not correctly offset- I want 0 to be below the first box and 100 below the second.

Played around with it, can't find the answer. Please help!

enter image description here

Upvotes: 0

Views: 741

Answers (1)

unutbu
unutbu

Reputation: 880947

Use

locs, labels = plt.xticks()

to find out the locations where matplotlib was going to place the xtick marks. Then change the labels:

plt.xticks(locs, max_distance, size='small')

For example,

import matplotlib.pyplot as plt

list_of_lists_of_lengths = [range(1000),range(500,2000)]
plt.subplot(1,1,1)    
plt.boxplot(list_of_lists_of_lengths)
plt.xlabel('max gap')
plt.ylabel('cluster lengths')
max_distance = (0, 100)
locs, labels = plt.xticks()
plt.xticks(locs, max_distance, size='small')
plt.show()

produces

enter image description here

Upvotes: 1

Related Questions