本文实例讲述了Python实现绘制双柱状图并显示数值功能。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# -*- coding:utf-8 -*-
#! python3
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
#定义函数来显示柱状上的数值
def autolabel(rects):
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2. - 0.2 , 1.03 * height, '%s' % float (height))
if __name__ = = '__main__' :
l1 = [ 68 , 96 , 85 , 86 , 76 , 87 , 95 ]
l2 = [ 85 , 68 , 79 , 89 , 94 , 82 , 90 ]
name = [ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'E' ]
total_width, n = 0.8 , 2
width = total_width / n
x = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ]
plt.rc( 'font' , family = 'SimHei' , size = 12 ) #设置中文显示,否则出现乱码!
a = plt.bar(x, l1, width = width, label = '数学' ,fc = 'y' )
for i in range ( len (x)):
x[i] = x[i] + width
b = plt.bar(x, l2, width = width, label = '语文' ,tick_label = name,fc = 'r' )
autolabel(a)
autolabel(b)
plt.xlabel( '学生' )
plt.ylabel( '成绩' )
plt.title( '学生成绩' )
plt.legend()
plt.show()
|
运行结果:
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/weixin_40198632/article/details/78858663