Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib xticks labels overlap

I am not able to get nicer spaces between the xticks with the following code:

import random
import matplotlib.pyplot as plt

coverages = [random.randint(1,10)*2] * 100
contig_names = ['AAB0008r'] * len(coverages)
fig = plt.figure()
fig.clf()
ax = fig.add_subplot(111)

ax.yaxis.grid(True, linestyle='-', which='major', color='grey', alpha=0.5)

ind = range(len(coverages))
rects = ax.bar(ind, coverages, width=0.2, align='center', color='thistle')
ax.set_xticks(ind)
ax.set_xticklabels(contig_names)
#function to auto-rotate the x axis labels
fig.autofmt_xdate()

plt.show()

How to get more space between the xticks so they do not look like overlapped anymore?

Thank you in advance.

like image 233
user977828 Avatar asked Sep 05 '12 02:09

user977828


People also ask

How do I stop MatPlotLib overlapping?

Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.

How do I skip a label in MatPlotLib?

Matplotlib removes both labels and ticks by using xticks([]) and yticks([]) By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis.

How do you stop overlapping in Seaborn?

Faceting. Faceting is one of the methods you can use to avoid overlapping. Seaborn has a function FacetGrid() for faceting.

How do you get more space between Xticks?

The spacing between ticklabels is exclusively determined by the space between ticks on the axes. Therefore the only way to obtain more space between given ticklabels is to make the axes larger.


1 Answers

You can try changing the figure size, the size of the xticklabels, their angle of rotation, etc.

# Set the figure size
fig = plt.figure(1, [20, 8])

# Set the x-axis limit
ax.set_xlim(-1,100)
# Change of fontsize and angle of xticklabels
plt.setp(ax.get_xticklabels(), fontsize=10, rotation='vertical')
like image 180
imsc Avatar answered Oct 12 '22 01:10

imsc