方法1:
import matplotlib
font = {'family': 'MicroSoft Yahei',
'weight': 'bold',
'size': 12}
matplotlib.rc("font", **font)
实例:
import matplotlib
import matplotlib.pyplot as plt
import random
font = {'family': 'MicroSoft Yahei',
'weight': 'bold',
'size': 12}
matplotlib.rc("font", **font)
x = range(0, 120)
y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize = (20, 8), dpi = 80)
plt.plot(x, y)
# 调整x轴刻度
_x = list(x)[::3]
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
# 取步长,数字和字符串一一对应,数据长度一样
plt.xticks(_x, _xtick_labels[::3], rotation = 45) # rotation 旋转角度
plt.show()
运行结果:
方法2:
import matplotlib
matplotlib.rc("font", family = "MicroSoft YaHei", weight = "bold")
实例:
import matplotlib
import matplotlib.pyplot as plt
import random
matplotlib.rc("font", family = "MicroSoft YaHei", weight = "bold")
x = range(0, 120)
y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize = (20, 8), dpi = 80)
plt.plot(x, y)
# 调整x轴刻度
_x = list(x)[::3]
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
# 取步长,数字和字符串一一对应,数据长度一样
plt.xticks(_x, _xtick_labels[::3], rotation = 60) # rotation 旋转角度
plt.show()
运行输出:
方法3:
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname='/System/Library/Fonts/PingFang.ttc")
# 不同系统,路径有所不同,事先查找出系统中可用的中文字体以及路径
# 调用
plt.func(...,fontproperties=my_font)
注:Linux/Mac下字体查看:
fc-list :lang=zh # 冒号前有空格
以Ubuntu为例:
实例:
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import font_manager
import random
my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\STFANGSO.TTF")
x = range(0, 120)
y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize = (20, 8), dpi = 80)
plt.plot(x, y)
# 调整x轴刻度
_x = list(x)[::3]
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
# 取步长,数字和字符串一一对应,数据长度一样
plt.xticks(_x, _xtick_labels[::3], FontProperties = my_font, rotation = 90) # rotation 旋转角度
plt.show()
运行结果: