Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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水平条形图向条形图添加值_Python_Matplotlib - Fatal编程技术网

Python Matplotlib水平条形图向条形图添加值

Python Matplotlib水平条形图向条形图添加值,python,matplotlib,Python,Matplotlib,使用以下代码: mixedgenres.sort_values(by = "rating").plot(kind = "barh", color = "steelblue", legend = False, grid = True) for i, v in enumerate(mixedgenres.rating): plt.text(v + 1, i - 0.25, str(round(v, 2)), color='steelblue') 我得到以下图表: 如何将值包含在图形框架

使用以下代码:

mixedgenres.sort_values(by = "rating").plot(kind = "barh", color = "steelblue", legend = False, grid = True) 
for i, v in enumerate(mixedgenres.rating):
    plt.text(v + 1, i - 0.25, str(round(v, 2)), color='steelblue')
我得到以下图表:

如何将值包含在图形框架内并向左对齐,以便将它们整齐地放置在一行中

以下是帮助您了解以下信息的示例数据:

sampledata = {'genre': ["Drama", "Western", "Horror", "Family", "Music", "Comedy", "Crime", "War"], 
              'rating': [7, 7.6, 8, 8.1, 7.8, 6.9, 7.5, 7.7]}
test = pd.DataFrame(sampledata, index = sampledata["genre"])
test
绘图样本数据

test.sort_values(by = "rating").plot(kind = "barh", color = "steelblue", legend = False, grid = True) 
for i, v in enumerate(test.rating):
    plt.text(v + 1, i - 0.25, str(round(v, 2)), color='steelblue')
和结果


以下是完整的工作解决方案(跳过导入)。有两件事:1)您在标签中使用了未排序的评级值;2)您在水平方向上添加了太多的偏移/移位。编辑:@ImportanceOfBeingErnest建议的文本垂直对齐

fig = plt.figure()
ax = fig.add_subplot(111)

sampledata = {'genre': ["Drama", "Western", "Horror", "Family", "Music", "Comedy", "Crime", "War"], 
              'rating': [7, 7.6, 8, 8.1, 7.8, 6.9, 7.5, 7.7]}
test = pd.DataFrame(sampledata,  index=sampledata['genre'])
test.sort_values(by = "rating").plot(kind = "barh", color = "steelblue", legend = False, grid = True, ax = ax) 
plt.xlim(0, 8.9)

for i, v in enumerate(sorted(test.rating)):
    plt.text(v+0.2, i, str(round(v, 2)), color='steelblue', va="center")
输出
以下是完整的工作解决方案(跳过导入)。有两件事:1)您在标签中使用了未排序的评级值;2)您在水平方向上添加了太多的偏移/移位。编辑:@ImportanceOfBeingErnest建议的文本垂直对齐

fig = plt.figure()
ax = fig.add_subplot(111)

sampledata = {'genre': ["Drama", "Western", "Horror", "Family", "Music", "Comedy", "Crime", "War"], 
              'rating': [7, 7.6, 8, 8.1, 7.8, 6.9, 7.5, 7.7]}
test = pd.DataFrame(sampledata,  index=sampledata['genre'])
test.sort_values(by = "rating").plot(kind = "barh", color = "steelblue", legend = False, grid = True, ax = ax) 
plt.xlim(0, 8.9)

for i, v in enumerate(sorted(test.rating)):
    plt.text(v+0.2, i, str(round(v, 2)), color='steelblue', va="center")
输出

看看我的答案,也看看。这是重复的。这一行
test=pd.DataFrame(sampledata,index=test.genre)
不正确,抛出错误1)对不起,我认为这不是重复的。很抱歉,样本数据已更正。请在下面检查我的答案。请看一下我的答案。这是重复的。这一行
test=pd.DataFrame(sampledata,index=test.genre)
不正确,抛出错误1)对不起,我认为这不是重复的。无法从这些中找出答案。2)抱歉,样本数据已更正。请检查下面的我的答案。这不起作用。似乎您没有包含部分代码。NameError:未定义名称“ax”-您能告诉我如何处理此问题吗?如果我删除这两行,我只会得到一张只有浅蓝色值的空图片。谢谢!工作!:)垂直位置的定义似乎相当随意。它实际上应该是
i
本身
plt.text(v+0.2,i,str(round(v,2)),color='steelblue',va=“center”)
@ImportanceOfBeingErnest:修复了它。非常感谢:)不行。似乎您没有包含部分代码。NameError:未定义名称“ax”-您能告诉我如何处理此问题吗?如果我删除这两行,我只会得到一张只有浅蓝色值的空图片。谢谢!工作!:)垂直位置的定义似乎相当随意。它实际上应该是
i
本身
plt.text(v+0.2,i,str(round(v,2)),color='steelblue',va=“center”)
@ImportanceOfBeingErnest:修复了它。非常感谢:)