以下默认字体,在mac ventura上测试能成功显示中文:
python
import matplotlib.pyplot as plt
import matplotlib
#from matplotlib import font_manager
#plt.rcParams['font.sans-serif'] = ['Heiti TC']
#plt.rcParams['font.sans-serif'] = ['Songti SC']
#plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['font.sans-serif'] = ['PingFang HK']
# 创建数据
x = [1, 2, 3, 4, 5]
y1 = [2, 1, 9, 8, 10]
y2 = [100, 300, 500, 700, 900]
# 创建图形和轴
fig, ax1 = plt.subplots()
# 绘制左侧Y轴数据
ax1.plot(x, y1, 'b-')
ax1.set_xlabel('X轴')
ax1.set_ylabel('左侧Y轴', color='b')
# 创建第二个Y轴
ax2 = ax1.twinx()
# 绘制右侧Y轴数据
ax2.plot(x, y2, 'r--')
ax2.set_ylabel('右侧Y轴', color='r')
# 显示图形
plt.show()
显示效果: