Python Matplotilb条形图:对角线刻度标签

Python Matplotilb条形图:对角线刻度标签,python,charts,matplotlib,bar-chart,Python,Charts,Matplotlib,Bar Chart,我正在用python使用matplotlib.pyplot绘制条形图。图表将包含大量的条形图,每个条形图都有自己的标签。因此,标签重叠,它们不再可读。我希望标签以对角方式显示,以便它们不会重叠,例如在图像中 这是我的代码: import matplotlib.pyplot as plt N =100 menMeans = range(N) ind = range(N) ticks = ind fig = plt.figure() ax = fig.add_subplot(111) rect

我正在用python使用
matplotlib.pyplot
绘制条形图。图表将包含大量的条形图,每个条形图都有自己的标签。因此,标签重叠,它们不再可读。我希望标签以对角方式显示,以便它们不会重叠,例如在图像中

这是我的代码:

import matplotlib.pyplot as plt
N =100
menMeans = range(N)
ind = range(N)  
ticks = ind 
fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, align = 'center')
ax.set_xticks(ind)
ax.set_xticklabels( range(N) )
plt.show()

标签如何对角显示?

文档中的示例使用:

plt.setp(xtickNames, rotation=45, fontsize=8)
因此,在你的情况下,我认为:
ax.set\u标签(范围(N),旋转=45,字体大小=8)
会给出角度,但它们仍然重叠。因此,请尝试:

import matplotlib.pyplot as plt
N =100
menMeans = range(N)
ind = range(N)  
ticks = ind 
fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, align = 'center')
ax.set_xticks(range(0,N,10))
ax.set_xticklabels( range(0,N,10), rotation=45 )
plt.show()

您可以使用
旋转
参数来执行以下操作,而不是使用
设置图标
设置图标

通过这种方式,您可以指定记号标签的旋转,同时让matplotlib为您管理其频率/间距。请注意,使用
ha=“right”
右对齐标签文本并不重要,如果您的标签都很短(在这种情况下,您可能希望将其删除),但如果您的标签很长且长度可变,则这一点很重要-它确保勾号标签的末端直接位于其标签的勾号下方,并防止标签间距不一致甚至重叠

一个完整的工作示例,基于问题中的代码:

import matplotlib.pyplot as plt
N =100
menMeans = range(N)
ind = range(N)  
ticks = ind 
fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, align = 'center')
plt.xticks(rotation=45, ha="right")
plt.show()
输出:


应该添加
ax.bar
现在接受
勾选标签
import matplotlib.pyplot as plt
N =100
menMeans = range(N)
ind = range(N)  
ticks = ind 
fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, align = 'center')
plt.xticks(rotation=45, ha="right")
plt.show()