这可能是一个琐碎的问题,但是我试图用matplotlib绘制条形图,并在x轴上旋转文本。我正在使用的代码如下所示:
fig = plt.figure()
x_labels_list = []
for i in range(0, pow(2, N)):
x_labels_list.append(str(f(i))) # The function f() converts i to a binary string
ax = plt.subplot(111)
width = 1.0
bins = map(lambda x: x-width, range(1,pow(2,N)+1))
ax.bar(bins, my_data, width=width)
ax.set_xticks(map(lambda x: x-width/2, range(1,pow(2,N)+1)))
ax.set_xticklabels(x_labels_list, rotation=90, rotation_mode="anchor", ha="right")
它可以正常工作,但是我在x轴的右侧获得了一个烦人的空白,如下图的红色椭圆所示:
您知道我该如何删除吗?提前致谢!
尝试plt.xlim()
用垃圾箱数量打电话,例如
plt.xlim([0,bins.size])
这是一个例子:
#make some data
N = 22
data = np.random.randint(1,10,N)
bin = np.arange(N)
width = 1
#plot it
ax = plt.subplot(111)
ax.bar(bin, data, width, color='r')
plt.show()
无plt.xlim()
输出:
现在plt.xlim
使用箱数定义大小来绘制它:
#plot it
ax = plt.subplot(111)
ax.bar(bin, data, width, color='r')
plt.xlim([0,bin.size])
plt.show()
结果:
也许有更好的方法,但这应该对您有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句