python中matplotlib作图时plt.legend()中无法显示中文,乱码为小方框
程序员文章站
2024-01-19 20:11:34
...
引入模块:
from matplotlib import font_manager
定义一个变量:
font = font_manager.FontProperties(fname = ‘电脑中字体的路径’)
然后再调用plt.legend(prop = font)
import matplotlib.pyplot as plt
from matplotlib import font_manager #matplotlib中 中文设置模块
import numpy as np
#中文字体路径(先看好自己电脑中的路径)
font = font_manager.FontProperties(fname = 'C:/Windows/fonts/simkai.ttf')
arr = np.genfromtxt('F:/Program Files/JetBrains/PycharmProjects/untitled/dat.csv', delimiter=',')
x_labels = np.array(['01.02', '01.03', '01.06', '01.07', '01.08', '01.09', '01.10', '01.13'])
x_arr = np.arange(len(arr[:,0]))
plt.bar(x_arr,arr[:,0],tick_label = x_labels,label = '百度')
plt.bar(x_arr,arr[:,1],bottom = arr[:,0],tick_label = x_labels,label = '阿里')
plt.bar(x_arr,arr[:,2],bottom = arr[:,1],tick_label = x_labels,label = '腾讯')
plt.legend(prop = font,loc = 'best') #记得添加prop = font
plt.show()
上一篇: 为WordPress 添加GitHub/BitBucket 项目页
下一篇: php如何删除多级目录