条形图中的烦人空白(matplotlib,Python) [英] Annoying white space in bar chart (matplotlib, Python)
本文介绍了条形图中的烦人空白(matplotlib,Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个琐碎的问题,但是我正在尝试使用matplotlib绘制条形图,并在x轴上旋转文本. 我正在使用的代码如下所示:
It's probably a trivial question, but I am trying to plot a bar chart with matplotlib and with rotated text on the x axis. The code I'm using is shown below:
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轴的右侧获得了一个烦人的空白,如下图的红色椭圆所示:
It works perfectly, but I obtain an annoying white space on the right of the x axis, as shown by the red ellipse in the following picture:
您知道如何删除它吗?预先感谢!
Do you know how I can remove it? Thanks in advance!
推荐答案
尝试使用箱数(例如,
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
使用箱数定义大小来绘制它:
Now plot it with plt.xlim
using the number of bins to define the size:
#plot it
ax = plt.subplot(111)
ax.bar(bin, data, width, color='r')
plt.xlim([0,bin.size])
plt.show()
结果:
也许有更好的方法,但这应该对您有用.
There may be a better way, but this should work for you.
这篇关于条形图中的烦人空白(matplotlib,Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文