Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Chinese garbled code in matplotlib thoroughly in linux

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to thoroughly solve the problem of Chinese garbled in matplotlib in linux. I hope you will gain something after reading this article. Let's discuss it together.

Matplotlib is the most basic toolkit for python drawing, but matplotlib has always been not very friendly to Chinese, and the Chinese display is garbled by default.

Previously, when using windows, you only need to change the matplotlibrc configuration file to solve it directly, but when you want to batch process in the linux production environment, you will encounter a lot of problems, some linux systems do not have Chinese fonts, and most of the Chinese fonts provided are ttc format, while matplotlib only supports ttf format, not ttc format fonts.

The problem in linux and mac os is the same, so here's how to solve it once and for all:

Windows

Windows is the simplest, directly modify the font-related parameter settings in the matplotlibrc configuration file.

Linux

Suppose you don't have root permission, but just install it in an individual user (it would be more convenient to have root permission).

First, check whether there are supported Chinese fonts in the system.

You can see that all supported Chinese fonts are in ttc format, and the pictures drawn cannot be displayed in Chinese.

Next, let's install the new font, copy the Microsoft Acer ttf file from windows, put it in the current user's home directory .fonts directory (if you don't have this directory and create it yourself). After putting the font file away, execute fc-cache to update the font cache, and then check the system font to find the newly added font.

Copy the corresponding font file to the mpl-data/fonts/ttf directory of matplotlib

Modify the font.serif and font.sans-serif options in matplotlib's configuration file matplotlibrc (this is serif and sans serif fonts, you can search for the difference between serif and sans serif fonts)

Note: the font name is Microsoft YaHei. The font name can be seen in the fc-list: lang=zh output. If it is wrong, it will not take effect.

Then draw the picture and display it in Chinese, then the garbled code will not be displayed.

Mac OS X

The OS X system add operation is similar to the Linux operation, except that the font is placed in the / Users/ username / Library/Fonts/ directory, then fc-cache is executed to update the font cache, while the font file is placed in the matplotlib/mpl-data/fonts/ttf directory, and the rest of the operation is the same as the Linux system.

I have tested all three systems and succeeded in the end. This may not be necessary as to whether you need to copy the file to the file in the mpl-data path, because I copied it to the mpl-data/fonts directory instead of the mpl-data/fonts/ttf directory on mac os x, but still successfully displayed the Chinese, so you may just need to add it to the system font. If not, copy the font to the mpl-data/fonts/ttf directory.

This is not the most critical question about whether you need to update the matplotlib font cache. You can delete the font cache file in the .matplotlib file in the home directory, or restart ipython directly.

After reading this article, I believe you have a certain understanding of "how to thoroughly solve the problem of Chinese garbled code in matplotlib in linux". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report