文章目录
遇到的问题解决方法参考解决方法:在画图前添加这样一句代码
plt.rcParams['font.sans-serif'] = ['SimHei']
遇到的问题
环境:win10,编辑器Geany,遇到的问题
C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\matplotlib\backends\backend_agg.py:238: RuntimeWarning: Glyph 24180 missing from current font.font.set_text(s, 0.0, flags=flags)
所运行的代码
import csvimport matplotlib.pyplot as pltfilename = 'data/sitka_weather__simple.csv'with open(filename) as f:reader = csv.reader(f)header_row = next(reader)# 从文件中获取最高温度highs = []for row in reader:high = int(row[5])highs.append(high)# 根据最高温度绘制图形plt.style.use('seaborn')plt.fig, ax = plt.subplots()ax.plot(highs, c='red')# 设置图形格式ax.set_title("7月每日最高温度", fontsize=24)ax.set_xlabel('', fontsize=16)ax.set_ylabel("温度(F)", fontsize=16)ax.tick_params(axis='both', which='major', labelsize=16)plt.show()
运行结果:标题上的中文是乱码
解决方法
加上下面这句话:
plt.rcParams['font.sans-serif'] = ['SimHei']
添加位置位于plot前面
plt.style.use('seaborn')plt.rcParams['font.sans-serif'] = ['SimHei']fig, ax = plt.subplots()ax.plot(highs, c='red')