Home Backend Development Python Tutorial Solutions to the Chinese display problem of python matplotlib drawings in Linux systems

Solutions to the Chinese display problem of python matplotlib drawings in Linux systems

Jun 18, 2017 am 11:39 AM
linux matplotlib python about system

This article mainly introduces how to solve the Chinese display problem of python matplotlib drawing in the Linux system. Friends who need it can refer to it

I want to learn some python data analysis content recently, just I made a crawler to crawl some data, and planned to use Anaconda's set of tools (pandas, numpy, scipy, matplotlib, jupyter) to conduct some preliminary data mining and analysis.

When using matplotlib to draw, the abscissa is in Chinese, but the abscissa of the drawn bar chart always shows "frame", so I went to check the information to solve it. I feel that this should be a relatively common problem. There are indeed a lot of Chinese materials on the Internet, but none of them completely solved the problem I encountered. It took almost 3 hours of piecemeal work to finally get it done. I would like to share this in the hope that it can help other children who have the same problem.

Running environment:

  • python2.7

  • Linux Centos7

  • Use conda to install ’s matplotlib and pandas

Question:

  • matplotlib drawing, unable to display Chinese

##Cause of the problem:

  • There is no Chinese font available in the linux operating system and matplotlib font library

  • ##The matplotlib package only supports ASCII code by default and does not support unicode code
  • Summary of online information:

    Modify matplotlib resources
  • Configuration file

    , for example, add "Simhei" font (this font is not all All Linux systems have it! Modification has no effect)

  • Install Chinese fonts for Linux and modify the resource configuration file of matplotlib. (Haha, it doesn’t work)

Solution: In fact, it is also a combination of various solutions on the Internet . Generally speaking, there are the following steps:

1. Get the path where the matplotlibrc file is located. Get in jupyter notebook:


import matplotlib
matplotlib.matplotlib_fname()
Copy after login

For example, my file is at:

u'~/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
Copy after login

Follow up The steps will modify the font parameter in this file.

2. Look at all the fonts in the system and the available Chinese fonts. Also in jupyter nb:

from matplotlib.font_manager import FontManager
import subprocess
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
print mat_fonts
output = subprocess.check_output(
  'fc-list :lang=zh -f "%{family}\n"', shell=True)
print '*' * 10, '系统可用的中文字体', '*' * 10
print output
zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n'))
available = mat_fonts & zh_fonts
print '*' * 10, '可用的字体', '*' * 10
for f in available:
  print f
Copy after login

After completing the above operation, you will find that the "Available Fonts" is empty. Because there are no Chinese fonts for matplotlib (that's why Chinese characters are all displayed as "boxes")

3. Assume that there are no Chinese fonts in the operating system. At this time, download a ttf Chinese font and install it in cenos. You must install a system that can detect

font-family

, otherwise it will be invalid. I downloaded it from this website: http://font.chinaz.com/130130474870.htmUnzip the rar file. Create the folder yourfontdir to store this font under the /usr/share/fonts path, and copy the downloaded ttf file to yourfontdir (you can change the file to an English name for easier operation)

4. Install cenos this font.

cd /usr/share/fonts/yourfontsdir
#生成字体索引信息. 会显示字体的font-family
sudo mkfontscale
sudo mkfontdir
#更新字体缓存:
fc-cache
Copy after login

5. Modify the matplotlibrc file

Modify the matplotlibrc file configuration obtained in step 1.

Remove the comments in the font.family part and add a Chinese font to the font.serif support font. Here, add the font-family of the Chinese font you just downloaded. You can search it through the fc-list command (so it is best to write it down before). What I added here is the "WenQuanYi Zen Hei Mono" font.

The following comment must be removed, otherwise the Chinese minus sign will also display a square:

axes.unicode_minus : False
Copy after login

6. This step is the most important! Add Chinese fonts to matplotlib

After completing step 5, and then proceed to step 2, you will find that the "Available Chinese Fonts" already has the font you just installed, but the drawing still cannot display Chinese. This is because you installed the font in centos and told matplotlib to use this font, but matplotlib cannot find the ttf file of this font. . . . So I need to get one for it.

Copy the downloaded ttf font to the following path:

~/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf
Copy after login

and

delete the

related cache. In the following path:

~/.cache/matplotlib
Copy after login

Delete the cache related to the font

7. Now try to draw a picture again. Done.

The above is the detailed content of Solutions to the Chinese display problem of python matplotlib drawings in Linux systems. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Unable to log in to mysql as root Unable to log in to mysql as root Apr 08, 2025 pm 04:54 PM

The main reasons why you cannot log in to MySQL as root are permission problems, configuration file errors, password inconsistent, socket file problems, or firewall interception. The solution includes: check whether the bind-address parameter in the configuration file is configured correctly. Check whether the root user permissions have been modified or deleted and reset. Verify that the password is accurate, including case and special characters. Check socket file permission settings and paths. Check that the firewall blocks connections to the MySQL server.

Solutions to the errors reported by MySQL on a specific system version Solutions to the errors reported by MySQL on a specific system version Apr 08, 2025 am 11:54 AM

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

How to solve mysql cannot be started How to solve mysql cannot be started Apr 08, 2025 pm 02:21 PM

There are many reasons why MySQL startup fails, and it can be diagnosed by checking the error log. Common causes include port conflicts (check port occupancy and modify configuration), permission issues (check service running user permissions), configuration file errors (check parameter settings), data directory corruption (restore data or rebuild table space), InnoDB table space issues (check ibdata1 files), plug-in loading failure (check error log). When solving problems, you should analyze them based on the error log, find the root cause of the problem, and develop the habit of backing up data regularly to prevent and solve problems.

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

How to optimize database performance after mysql installation How to optimize database performance after mysql installation Apr 08, 2025 am 11:36 AM

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

How to solve the problem of missing dependencies when installing MySQL How to solve the problem of missing dependencies when installing MySQL Apr 08, 2025 pm 12:00 PM

MySQL installation failure is usually caused by the lack of dependencies. Solution: 1. Use system package manager (such as Linux apt, yum or dnf, Windows VisualC Redistributable) to install the missing dependency libraries, such as sudoaptinstalllibmysqlclient-dev; 2. Carefully check the error information and solve complex dependencies one by one; 3. Ensure that the package manager source is configured correctly and can access the network; 4. For Windows, download and install the necessary runtime libraries. Developing the habit of reading official documents and making good use of search engines can effectively solve problems.

Solutions to the service that cannot be started after MySQL installation Solutions to the service that cannot be started after MySQL installation Apr 08, 2025 am 11:18 AM

MySQL refused to start? Don’t panic, let’s check it out! Many friends found that the service could not be started after installing MySQL, and they were so anxious! Don’t worry, this article will take you to deal with it calmly and find out the mastermind behind it! After reading it, you can not only solve this problem, but also improve your understanding of MySQL services and your ideas for troubleshooting problems, and become a more powerful database administrator! The MySQL service failed to start, and there are many reasons, ranging from simple configuration errors to complex system problems. Let’s start with the most common aspects. Basic knowledge: A brief description of the service startup process MySQL service startup. Simply put, the operating system loads MySQL-related files and then starts the MySQL daemon. This involves configuration

Can mysql run on android Can mysql run on android Apr 08, 2025 pm 05:03 PM

MySQL cannot run directly on Android, but it can be implemented indirectly by using the following methods: using the lightweight database SQLite, which is built on the Android system, does not require a separate server, and has a small resource usage, which is very suitable for mobile device applications. Remotely connect to the MySQL server and connect to the MySQL database on the remote server through the network for data reading and writing, but there are disadvantages such as strong network dependencies, security issues and server costs.

See all articles