首页 > 解决方案 > 使用 matplotlib 在条形顶部添加文本标签

问题描述

我有下面的python代码来使用matplotlib进行绘图。

我需要在每个数据栏顶部添加标签的帮助。

import numpy as np

import matplotlib.pyplot as plt

N = 4
ind = np.arange(N) 
width = 0.25

xvals = [95.73,94.70,92.29,76.48]
bar1 = plt.bar(ind, xvals, width, color = 'r')

yvals = [78.47,76.65,63.61,41.58]
bar2 = plt.bar(ind+width, yvals, width, color='g')

zvals = [69.30,54.46,50.92,39.21]
bar3 = plt.bar(ind+width*2, zvals, width, color = 'b')

plt.xlabel("Teams")
plt.ylabel('Data (%)')
plt.title("Overall data")

plt.xticks(ind+width,['Team 1', 'Team 2', 'Team 3', 'Team 4'])

plt.legend( (bar1, bar2, bar3), ('Data 1', 'Data 2', 'Data 3') )


plt.show()

标签: pythonmatplotlib

解决方案


推荐阅读