条形图中的烦人空白(matplotlib,Python)

user2983638:

这可能是一个琐碎的问题,但是我试图用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轴的右侧获得了一个烦人的空白,如下图的红色椭圆所示:

在此处输入图片说明

您知道我该如何删除吗?提前致谢!

dnf0:

尝试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()输出:

没有xlim

现在plt.xlim使用箱数定义大小来绘制它

#plot it
ax = plt.subplot(111)
ax.bar(bin, data, width, color='r')
plt.xlim([0,bin.size])
plt.show()

结果:

与xlim

也许有更好的方法,但这应该对您有用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

条形图中意外的空白和值(matplotlib)

如何处理熊猫条形图中的烦人间隙

Matplotlib的条形图中条形上方的值

Python:如何使用 Matplotlib 在条形图中垂直放置文本

摆脱python matplotlib条形图中的灰色背景

按升序/降序,Matplotlib,Python在图中显示条形图

忽略堆积条形图中的空白

matplotlib条形图中的怪异伪像

matplotlib条形图中的“丢失”条

条形图中的Matplotlib Xticks值

Matplotlib:并排条形图中的自动标记

条形图中的类别颜色matplotlib

在Matplotlib,Python3的极/径向条形图中的条形顶部获取标签

如何控制seaborn条形图中条形之间的空白?

如何使条形图中的条形填补所有空白

如何消除Matplotlib条形图中的条形之间的间隙

如何在matplotlib pyplot条形图中反转条形大小?

matplotlib条形图中条形之间的空间太大

如何在python中使用plotly消除条形图中的空白

Python条形图中的斜条

Python Matplotlib-如何在条形图中的y轴上设置值

如何在Python(Pandas或matplotlib)的条形图中移动对数轴刻度位置(对于10)

Seaborn分布图中的随机空白空间/条形图

如何从Matplotlib条形图中删除保证金?

Matplotlib:根据条件识别条形图中的条

如何增加matplotlib条形图中误差线的粗细?

使用Pandas MultiIndex对matplotlib条形图中的标签进行分组

使用 matplotlib 在条形图中并排显示多列

matplotlib:在条形图中更改x网格的时间戳格式