<p>它可以像<a href="https://matplotlib.org/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py" rel="nofollow noreferrer">shown here</a>那样做,只需稍加修改</p>
<pre><code>import matplotlib.pyplot as plt
X= [-0.9384815619939103, 1.0755888058123153, 0.061274066731665564,0.65064830688728]
x_labels = ['A' ,'B', 'C', 'D']
error = [0.23722952107696088, 0.25505883348061764, 0.26038015798295744, 0.26073839861422]
pvalue = [0.000076, 0.000025, 0.813956, 0.012581]
fig, ax = plt.subplots()
rects = ax.bar(x_labels, X, width=0.4, align = 'center', yerr=error)
def autolabel(rects, pvalue, xpos='center',):
"""
Attach a text label above each bar in *rects*, displaying its height.
*xpos* indicates which side to place the text w.r.t. the center of
the bar. It can be one of the following {'center', 'right', 'left'}.
"""
xpos = xpos.lower() # normalize the case of the parameter
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
offset = {'center': 0.5, 'right': 0.57, 'left': 0.43} # x_txt = x + w*off
for i, rect in enumerate(rects):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
'p = {}'.format(pvalue[i]), ha=ha[xpos], va='bottom')
autolabel(rects, pvalue, "left")
plt.show()
</code></pre>
<p>结果是<a href="https://i.stack.imgur.com/Qeafv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qeafv.png" alt="enter image description here"/></a></p>