当前位置:网站首页>Matplotlib drawing Chinese garbled code
Matplotlib drawing Chinese garbled code
2022-06-13 01:41:00 【Under the starry sky 0516】
Chinese garbled code may have the following problems , It needs to be solved one by one :
- You need to import Chinese fonts correctly .
- Import fonts correctly , however matplotlib There are no Chinese fonts in the font library ( Latest version of matplotlib Chinese fonts are not installed by default ).
- Put Chinese fonts in matplotlib After font library , But it is not clear matplotlib cache
- Unknown reasons
Solution
Aiming at problems 1:
modify matplotlibrc The file of , Use the following command to find matplotlibrc The location of the file :
import matplotlib
print(matplotlib.__path__) # Output matplotlib The full path of
Then enter matplotlib The full path of /mpl-data/ route , open matplotlibrc After the document , Open and modify as shown in the following figure :
take SimHei Fonts added font.sans-serif Inside .
Aiming at problems 2:
Get into matplotlib The full path of /mpl-data/fonts/ttf route , Will download SimHei.ttf Put the file in this folder . You can download fonts here :https://github.com/understars0516/popular-fonts and http://xiazaiziti.com/
Aiming at problems 3:
If the problem 1,2 Can not solve the problem of Chinese garbled code , That is not clear matplotlib Font cache , Just clear it , Specific cache location , Available below :
import matplotlib
print(matplotlib.get_cachedir())
Delete the folder , Chinese fonts can be used after re importing .
Results test :
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif']=['SimHei']
matplotlib.rcParams['axes.unicode_minus']=False
data = np.random.rand(100)
plt.plot(data)
plt.title(' Chinese test ')
plt.xlabel('x coordinate ')
plt.ylabel('y coordinate ')
plt.show()

Aiming at problems 4:
If the above three questions have been used , Still can not be solved , You can use the following temporary methods , This method is tried and tested . I.e. direct use matplotlib Under the FontProperties Import fonts temporarily , But you need to download ttf typeface :
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"./SimHei.ttf", size=22)
data = np.random.rand(100)
plt.plot(data)
plt.title(' Chinese test ', fontproperties=font)
plt.xlabel('x coordinate ', fontproperties=font)
plt.ylabel('y coordinate ', fontproperties=font)
plt.show()

边栏推荐
- D template instance does not match declaration
- Study notes on the introduction paper of face recognition deep facial expression recognition: a survey
- Project training (XVII) -- personal work summary
- Anims of phaser3
- September 3, 2021 visual notes
- Quickly set the computer to turn off automatically
- Summary of various installation methods of Lab View
- 受众群体应该选择观察模式还是定位模式?
- ES6 deconstruction assignment
- Leetcode question brushing 02 linked list operation
猜你喜欢

Leetcode find duplicates

Lecture on Compilation Principles

ES6 deconstruction assignment

Crypto JS reports uglifyjs error

开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧

如何通过受众群体定位解决实际问题?

Sliding window summary of TCP connections

TensorFlow2的Conv1D, Conv2D,Conv3D机器对应的MaxPooling详解

Leetcode question brushing 03 stack

机器学习基础 SVM(支持向量机)
随机推荐
How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
Quickly set the computer to turn off automatically
Introduction to common activation functions
Mysql database listening -canal
六、出库管理功能的实现
谷歌的受众群体是如何发挥作用的?
Jeux de plombiers
Stone from another mountain: Web3 investment territory of a16z
Create a simple game interface using pyGame
STM32 3*3矩阵按键(寄存器版本)
【软考】软件设计师知识点整理(待更新)
Traversal of binary tree - first order traversal, middle order traversal, and second order traversal
Using OpenCV in go
H5 open the app. If the app is not downloaded, jump to the download page. If the app has been downloaded, wake up the app
ng-tv-focusable
DFS and BFS notes (II): depth first search (implemented in C language)
Audiences with similar interests
Work and life
Simple operation of MySQL database
Transaction characteristics and isolation levels