首页 技术 正文
技术 2022年11月15日
0 收藏 920 点赞 4,112 浏览 2505 个字

1 绘制并列条形图,并在条形图上加标注

1.1 代码

from matplotlib import pyplot
from matplotlib import font_manager
import numpy as np
import random# 小学期间语文和数学的成绩
x_label = ['一年级','二年级','三年级','四年级','五年级','六年级']
chinese = [random.randint(40,100) for i in range(6)]
math = [random.randint(20,100) for i in range(6)]#x轴刻度 用数组方便数据运算
x = np.arange(len(x_label))
width = 0.3 # 条形图宽度plt.figure(figsize=(20,8),dpi=80)
# 语文条形图
# bar(self, x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
# 将chinese的条形图向左平移半个宽度
rects1=plt.bar(x-width/2,chinese,width=width,label='语文')
# 将math的条形图向左平移半个宽度
rects2=plt.bar(x+width/2,math,width=width,label='数学')# 添加标题、图例、网格、y轴标题、x刻度标签
my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/msyh.ttc',size=10)
plt.title('小学语文和数学分数对比',fontproperties=my_font)
plt.legend(prop=my_font,loc='upper right')
plt.grid(alpha=0.4)
plt.ylabel('分数',fontproperties=my_font)
plt.xticks(x,x_label,fontproperties=my_font)# 在条形图上加标注(水平居中)
# plt.bar()可以返回条形图的每个条形,所以前代码将各个柱形赋值给一个变量
for rect1 in rects1:
height = rect1.get_height()
plt.text(rect1.get_x()+rect1.get_width()/2,height+1,str(height),ha='center')for rect2 in rects2:
height = rect2.get_height()
plt.text(rect2.get_x()+rect2.get_width()/2,height+1,str(height),ha='center')plt.show()

1.2 图表展示

2 代码分解

2.1 并列条形图

在同一个绘图区内绘制两个条形图,此时两个条形图重叠在一起

为了解决重叠,调整两个条形图的x。如上图chinese条形图向左平移半个宽度,math条形图向右平移半个宽度,不用移动x轴

# 语文条形图
# bar(self, x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
# 将chinese的条形图向左平移半个宽度
rects1=plt.bar(x-width/2,chinese,width=width,label='语文')
# 将math的条形图向右平移半个宽度
rects2=plt.bar(x+width/2,math,width=width,label='数学')

2.2 中文显示

2.2.1 使用font_manager导入电脑中的字体

查看电脑支持的字体

查看Linux、Mac下支持的字体:

终端执行

fc-list

查看支持的中文:

# 冒号前有两个空格
fc-list :lang=zh
查看win下的字体:

‘C:/windows/Fonts/’

#### 在代码中引入中文
```python
my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/msyh.ttc',size=10)
plt.title('小学语文和数学分数对比',fontproperties=my_font)

注意

plt.title、plt.ylabel、plt.xticks等需要使用中文时,参数为fontproperties=my_font

但设置图例的时候使用中文,plt.legend中,参数为prop=my_font

2.2.2 使用rcParams引入中文

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

2.3 在条形图上加标注

plt.bar()可以返回条形图的每个条形,所以前代码将各个柱形赋值给一个变量

rects1=plt.bar(x-width/2,chinese,width=width,label='语文')

再使用for遍历每一个条形

获取到一个条形后,使用get_height()方法获取条形的高,即值

再使用plt.text加标注,text(self, x, y, s, fontdict=None, **kwargs)

注:

  • get_x()获取的是条形的左侧,若要标注居中,需要在get_x()的基础上再向右移动半个宽度;
  • 若标注的高仅使用get_height(),则会出现标注和条形的间隔过小,此时get_height()+1将标注往上,增加标注和条形之间的距离;
  • 标注内容要求为str类型,需要将值进行转换
  • 调整标注和定位点的对齐方式使用参数ha={‘center’,’right’,’left’}
for rect1 in rects1:
height = rect1.get_height()
plt.text(rect1.get_x()+rect1.get_width()/2,height+1,str(height),ha='center')
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290