Matplotlib does not display Chinese solutions

  In the process of learning matplotlib drawing, it is inevitable that the x-axis, y-axis scale, figure title, and legend display in Chinese. However, I found that matplotlib does not display Chinese by default, and it needs to be set through fonts. The author encountered some small problems in the process of solving the problem and successfully solved them, hoping to help everyone.

  Here I choose to draw a movie box office bar chart as a case explanation.

  It can be seen that the name of each movie should have been displayed on the scale of the x-axis, but it has become a box.

The main idea to solve this problem is: set a font variable my_font, let the path be the local font in your computer, and then set the font as this font variable my_font in the code that needs to display Chinese.

The first step is to find the Chinese font path in your computer.

  Most computers are stored in the Fonts folder, and the fonts of my computer are stored in the c drive, the specific location: C:\Windows\Fonts

Assuming we want to set the Chinese font to Microsoft YaHei, we will find the target font Microsoft YaHei UI. You can find that there are 3 fonts here, so continue to double-click.

You can see that Yahei has 3 fonts, we can choose the first regular one.

Note that while the fonts are Microsoft YaHei UI conventions, the font pathnames are not. Right-click on the font and click Properties. You can see that the name of the font is msyh.ttc

So the path to the font is:

C:\Windows\Fonts\msyh.ttc

The second step is to set the font variable my_font

You also need to import font_manager before adding font variables

from matplotlib import font_manager

After importing, set the font variable


my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")

 

The previous code has been typed, and the next step is to find the part of the code that needs to display Chinese.

The third step is to find the code that displays Chinese and add the my_font attribute

In the initial result graph, you can see that the ticks on the x-axis need to display Chinese.

Replace the code marked in the red box with:

# 设置字符串x轴
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=90)

Chinese can be displayed. 

Next, attach the code of this case:

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")

a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5\n:最后的骑士","摔跤吧!爸爸","加勒比海盗5\n:死无对证","金刚:骷髅岛","极限特工\n:终极回归",
     "生化危机6\n:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3\n:殊死一战","蜘蛛侠\n:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]

b = [56.01,26.94,17.53,16.49,15.15,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]

# 设置图形大小
plt.figure(figsize=(14,30),dpi=80)

# 绘制条形图
plt.bar(range(len(a)),b,width=0.3)

# 设置字符串x轴
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=90)

plt.show()
# plt.savefig("./t6.png")

 At the same time, there are deficiencies in this case, and the code for the horizontal bar graph is attached.

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")

a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归",
     "生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]

b = [56.01,26.94,17.53,16.49,15.15,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]

# 设置图形大小
plt.figure(figsize=(20,8),dpi=80)

# 绘制条形图
plt.barh(range(len(a)),b,height=0.3,color="orange")

# 设置字符串y轴
plt.yticks(range(len(a)),a,fontproperties=my_font)

plt.grid(alpha=0.3)

plt.show()
# plt.savefig("./t7.png")

 

 

 

Guess you like

Origin blog.csdn.net/qq_43604183/article/details/126487965