python - 如何在条形图顶部显示值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53066633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python - how to show values on top of bar plot
提问by Dan
Python newbie here. I want to show values above each bin in the following graph:
Python新手在这里。我想在下图中显示每个 bin 上方的值:
This is my code:
这是我的代码:
x=[i for i in range(1,11)]
y=[0.95,
0.95,
0.89,
0.8,
0.74,
0.65,
0.59,
0.51,
0.5,
0.48]
plt.bar(x, height= y)
xlocs, xlabs = plt.xticks()
xlocs=[i+1 for i in range(0,10)]
xlabs=[i/2 for i in range(0,10)]
plt.xlabel('Max Sigma')
plt.ylabel('Test Accuracy')
plt.xticks(xlocs, xlabs)
plt.show()
this is the graph that i want : enter image description here
这是我想要的图表: 在此处输入图像描述
回答by Marianne Halloran
Simply add
只需添加
for i, v in enumerate(y):
plt.text(xlocs[i] - 0.25, v + 0.01, str(v))
before plt.show()
. You can adjust the centralization or height of the text by changing the (-0.25) and (0.01) values, respectively.
之前plt.show()
。您可以通过分别更改 (-0.25) 和 (0.01) 值来调整文本的居中或高度。
回答by daronjp
plt.text()
will allow you to add text to your chart. It only enables you to add text to one set of coordinates at a time, so you'll need to loop through the data to add text for each bar.
plt.text()
将允许您向图表添加文本。它只允许您一次向一组坐标添加文本,因此您需要遍历数据为每个条添加文本。
Below are the main adjustments I made to your code:
以下是我对您的代码所做的主要调整:
# assign your bars to a variable so their attributes can be accessed
bars = plt.bar(x, height=y, width=.4)
# access the bar attributes to place the text in the appropriate location
for bar in bars:
yval = bar.get_height()
plt.text(bar.get_x(), yval + .005, yval)
I added .005
to the y-value so that the text would be placed above the bar. This can be modified to obtain the appearance you are looking for.
我添加.005
了 y 值,以便将文本放置在栏上方。这可以修改以获得您正在寻找的外观。
Below is a full working example based on the original code. I made a few modifications to make it less brittle as well:
下面是一个基于原始代码的完整工作示例。我做了一些修改以使其不那么脆弱:
import matplotlib.pyplot as plt
# set the initial x-values to what you are wanting to plot
x=[i/2 for i in range(10)]
y=[0.95,
0.95,
0.89,
0.8,
0.74,
0.65,
0.59,
0.51,
0.5,
0.48]
bars = plt.bar(x, height=y, width=.4)
xlocs, xlabs = plt.xticks()
# reference x so you don't need to change the range each time x changes
xlocs=[i for i in x]
xlabs=[i for i in x]
plt.xlabel('Max Sigma')
plt.ylabel('Test Accuracy')
plt.xticks(xlocs, xlabs)
for bar in bars:
yval = bar.get_height()
plt.text(bar.get_x(), yval + .005, yval)
plt.show()
回答by Jose Garcia
Try:
尝试:
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
This makes it simple
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
这使它变得简单