文章目录
- 一、现象:
- 二、解决方案
- 1.将 **SimHei.ttf** ,下载到当前运行目录中
- 2.绘图中涉及标题、横纵坐标等,加上 **FontProperties = font** 即可
环境说明:macOS系统
一、现象:
原先代码是这样的
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig, axs = plt.subplots(2, 5, figsize=(10, 4), sharex=True, sharey=True)
fig.suptitle('样例1', size=20)
for i in range(2):
for j in range(5):
axs[i][j].scatter(np.random.randn(10), np.random.randn(10))
axs[i][j].set_title('第%d行,第%d列'%(i+1,j+1))
axs[i][j].set_xlim(-5,5)
axs[i][j].set_ylim(-5,5)
if i==1: axs[i][j].set_xlabel('横坐标')
if j==0: axs[i][j].set_ylabel('纵坐标')
fig.tight_layout()
二、解决方案
后面经过找各种资料,尝试一番,终于解决了我的问题
1.将 SimHei.ttf ,下载到当前运行目录中
2.绘图中涉及标题、横纵坐标等,加上 FontProperties = font 即可
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
from matplotlib.font_manager import FontProperties
font = FontProperties(fname='SimHei.ttf', size = 15)
fig, axs = plt.subplots(2, 5, figsize=(10, 4), sharex=True, sharey=True)
plt.suptitle('样例1', size=20, FontProperties = font)
for i in range(2):
for j in range(5):
axs[i][j].scatter(np.random.randn(10), np.random.randn(10))
axs[i][j].set_title('第%d行,第%d列'%(i+1,j+1), FontProperties = font)
axs[i][j].set_xlim(-5,5)
axs[i][j].set_ylim(-5,5)
if i==1: axs[i][j].set_xlabel('横坐标', FontProperties = font)
if j==0: axs[i][j].set_ylabel('纵坐标', FontProperties = font)
plt.show()
数据代码参考:阿里云天池
解决方案:网上各种资料尝试一番