python柱状图的柱子上方显示文本/数据

想在如下图表的柱子上添加数据
python柱状图的柱子上方显示文本/数据_第1张图片
变成这样
python柱状图的柱子上方显示文本/数据_第2张图片
第一张图的代码是

import matplotlib.pyplot as plt
import numpy as np
 
n = 12
X = np.arange(n)
Y1 = (1 - X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1 - X/float(n)) * np.random.uniform(0.5,1.0,n)
 
plt.bar(X,+Y1)
plt.bar(X,-Y2)

plt.xlim(-.5,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(())

第二张图的代码是

import matplotlib.pyplot as plt
import numpy as np
 
n = 12
X = np.arange(n)
Y1 = (1 - X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1 - X/float(n)) * np.random.uniform(0.5,1.0,n)

plt.bar(X,+Y1)
plt.bar(X,-Y2)
 
for x,y in zip(X,Y1):
    plt.text(x+0.05,y+0.05,'%.2f' %y, ha='center',va='bottom')
    
for x, y in zip(X, Y2):
    plt.text(x+0.05,-y-0.15,'%.2f'%y, ha='center',va='bottom')
 
plt.xlim(-.5,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(())
#参考https://blog.csdn.net/DeniuHe/article/details/86671301?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

如果要在柱子上方添加文本而非柱状图的二维坐标数据,如图
python柱状图的柱子上方显示文本/数据_第3张图片
代码如下

import matplotlib.pyplot as plt
import numpy as np
 
n = 12
X = np.arange(n)
Y1 = (1 - X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1 - X/float(n)) * np.random.uniform(0.5,1.0,n)
ls = ['壹','贰','叁','肆','伍','陆','柒','捌','玖','拾','十一','十二']
plt.bar(X,+Y1)
plt.bar(X,-Y2)
 
for x,y,z in zip(X,Y1,ls):
    plt.text(x+0.05,y+0.05,z, ha='center',va='bottom')
    
for x, y,z in zip(X, Y2,ls):
    plt.text(x+0.05,-y-0.15,z, ha='center',va='bottom')
 
plt.xlim(-.5,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(())

#参考https://blog.csdn.net/DeniuHe/article/details/86671301?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

你可能感兴趣的:(问题bugs)