Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib缺少第一个x标签_Python_Pandas_Matplotlib_Label - Fatal编程技术网

Python 使用matplotlib缺少第一个x标签

Python 使用matplotlib缺少第一个x标签,python,pandas,matplotlib,label,Python,Pandas,Matplotlib,Label,这是我的数据: a3=pd.DataFrame({'OfficeName':['a','b','c','d','e','f','g','h'], 'Ratio': [0.1,0.15,0.2,0.3,0.2,0.25,0.1,0.4]}) 这是我画条形图的代码: fig, ax = plt.subplots() ind = np.arange(a3.loc[:,'OfficeName'].nunique()) # the x locations for

这是我的数据:

a3=pd.DataFrame({'OfficeName':['a','b','c','d','e','f','g','h'],
                 'Ratio': [0.1,0.15,0.2,0.3,0.2,0.25,0.1,0.4]})
这是我画条形图的代码:

fig, ax = plt.subplots()
ind = np.arange(a3.loc[:,'OfficeName'].nunique())    # the x locations for the groups
width = 0.35         # the width of the bars
p1 = ax.bar(ind,a3.loc[:,'Ratio'],width)
ax.set_title('Ratio of refunds to purchases')
ax.set_xticklabels(a3.loc[:,'OfficeName'],ha='center')
#ax.set_xticklabels(['a','b','c','d','e','f','g','h'],ha='center')
ax.set_xlabel('x Group')
ax.set_ylabel('Ratio')
plt.show()
但是,在我的图表中,第一个x标签缺失:

我认为这个问题与其他问题不同,因为我甚至不需要旋转x标签


有人能解释一下为什么会发生这种情况以及如何解决吗?

有一种更简单的方法可以用Pandas制作条形图:

a3.set_index('OfficeName').plot.bar(width=0.35, legend=False)
plt.xticks(rotation=0, ha='center')

(您仍然需要设置x轴和y轴标签以及标题。)

谢谢。你能告诉我为什么我的问题会发生吗?我只能告诉你,出于某种原因,
ax.bar()
在位置-1处添加了另一个勾号(调用
ax.getxticks()
亲自查看!))标签“a”被分配给了该勾号,并且不在图表上。谢谢。真的吗helps@FengChen我知道我有点晚了。然而,仅供参考:我在X轴和X轴上的标签上也遇到了同样的问题。set_xticks(…)完全按照我想要的方式完成了这项工作。请检查此问题以明确说明:在未指定标签位置的情况下,切勿使用
ax.set_tickles
,即
ax.set_tickles