Matplotlib xticks标签重叠问题

16

以下代码无法让xticks之间的间距更加美观:

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()

如何在x轴上让刻度线之间的间距更大,以避免它们看起来重叠?

提前感谢。

1个回答

18

你可以尝试更改图形大小、xticklabels的大小、它们的旋转角度等。

# 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')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接