python设置折线图标题字体为宋体 您所在的位置:网站首页 matplotlib的legend字体 python设置折线图标题字体为宋体

python设置折线图标题字体为宋体

2023-04-15 12:05| 来源: 网络整理| 查看: 265

python

设置折线图标题字体为宋体

_python

matplotlib

画折线

图实例(坐标。。。

最近在⽤python中的matplotlib画折线图,遇到了坐标轴 “数字+刻度” 混合显⽰、标题中⽂显⽰、批量处理等诸多问题。通过学习解决

了,来记录下。如有错误或不⾜之处,望请指正。

⼀、最简单的基本框架如下:已知x,y,画出折线图并保存。此时x和y均为数字。

1 #-*- coding: utf-8 -*-

2

3 import matplotlib.pyplot as plt #引⼊matplotlib的pyplot⼦库,⽤于画简单的2D图

4 importrandom5

6 x= range(0,20)7 y= [random.randint(0,20) for _ in range(20)]8

9 #建⽴对象

10 fig = plt.figure(figsize=(8,6))11 ax =fig.add_subplot()12

13 #画图

14 plt.plot(x,y,‘o-‘,label=u"线条") #画图

15 plt.show()16 plt.savefig("temp.png")

⼆、坐标轴增加字母元素:

from matplotlib.ticker import FuncFormatter, MaxNLocator

labels = list(‘abcdefghijklmnopqrstuvwxyz‘)

def format_fn(tick_val, tick_pos):

if int(tick_val) in xs:

return labels[int(tick_val)]

else:

return ‘‘

ax.xaxis.set_major_formatter(FuncFormatter(format_fn))

ax.xaxis.set_major_locator(MaxNLocator(integer=True))

稍微改动,⽤到了之前的程序⾥:

1 #-*- coding: utf-8 -*-

2

3 import matplotlib.pyplot as plt #引⼊matplotlib的pyplot⼦库,⽤于画简单的2D图

4

5 from matplotlib.ticker importFuncFormatter, MaxNLocator6

7 importrandom8

9 x= range(20)10

11 y= [random.randint(0,20) for _ in range(20)]12



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有