当前位置:   article > 正文

python用matplotlib或boxplot作图的时候,中文标注无法正常显示,乱码为小方框的解决办法_画的箱线图 横坐标没有汉字 只有方框

画的箱线图 横坐标没有汉字 只有方框

第一种

import matplotlib.pyplot as plt
plt.rc("font",family="SimHei",size="22") 

# 生成fig
plt.figure(figsize=(20, 20), dpi=80)
dataPathBj.boxplot(column=['行政面积(K㎡)','户籍人口(万人)','男性','女性','GDP(亿元)','常住人口(万人)'])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
效果图

在这里插入图片描述

第二种

from matplotlib import pyplot as plt
font = {'family' : 'MicroSoft YaHei',
        'weight' : 'bold',
        'size'   : 20}
plt.rc("font", **font)

plt.figure(figsize=(20, 20), dpi=80)
pathTj.boxplot(column=['行政面积(K㎡)','户籍人口(万人)','男性','女性','GDP(亿元)','常住人口(万人)'])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
效果图

在这里插入图片描述

第三种

import matplotlib
from matplotlib.font_manager import *  
import matplotlib.pyplot as plt
#路径在C:\Windows\Fonts
myfont = FontProperties(fname=r'C:\Windows\Fonts\禹卫书法行书简体.ttf')  #自定义字体
#解决负号'-'显示为方块的问题  
matplotlib.rcParams['axes.unicode_minus']=False  

x=range(11,15)
y=[-1,2,-5,3]

xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x,xtick_labels,fontproperties=myfont)
plt.plot(x,y)  
plt.title(u'想要博客还有视频下载交流?博客和视频下载学习交流群:1106112426 满足你的需求,资料都已经上传群文件,可以自行下载!',fontproperties=myfont)  
plt.show()

# 绘制网格
plt.grid(alpha=0.2) 
# 增加标题,坐标描述
plt.xlabel("年龄", fontproperties=myfont)
plt.ylabel("数量(个)", fontproperties=myfont)
# 图形保存到该文件路径下
plt.savefig("pie.png")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
效果图

在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号