use*_*638 21 python matplotlib bar-chart
这可能是一个微不足道的问题,但我试图用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")
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但是我在x轴的右边获得了一个恼人的空白区域,如下图中的红色椭圆所示:

你知道我怎么能把它删除吗?提前致谢!
dab*_*lox 30
尝试plt.xlim()使用垃圾箱数量来调用,例如
plt.xlim([0,bins.size])
Run Code Online (Sandbox Code Playgroud)
这是一个例子:
#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()
Run Code Online (Sandbox Code Playgroud)
没有plt.xlim()输出:

现在plt.xlim使用bin的数量绘制它以定义大小:
#plot it
ax = plt.subplot(111)
ax.bar(bin, data, width, color='r')
plt.xlim([0,bin.size])
plt.show()
Run Code Online (Sandbox Code Playgroud)
结果:

可能有更好的方法,但这应该对你有用.