Article Series Catalog
- Chapter 1Python common library matplotlib of drawing text in Chinese display
- Chapter IIPython common library matplotlib of drawing the meaning of the various modules and modification methods
- Chapter IIIPython common library matplotlib multiple subplots plotting
preamble
In data visualization, it is often necessary to use a legend to explain the meaning of different elements in a diagram. In thesis writing, especially thesis writing, you need to display Chinese characters in the diagrams, and use the new roman font for the numbers. However, the matplotlib library does not support Chinese display by default, so you need to make some settings to display Chinese when you draw the legend. This article will introduce how to use matplotlib library to draw Chinese legend.
I. What is matplotlib?
Matplotlib is a Python library for creating visual charts.It supports a wide range of chart types, including line, scatter, bar, pie, and 3D charts, and is one of the most commonly used visualization tools for data analysis, scientific computing, and engineering applications.The Matplotlib library is widely used in Python for data analysis, machine learning, scientific computing as well as in research, education and industrial production. It is open source software and can be used in several versions of Python.
The installation of the matplotlib library is simple
pip install matplotlib
The version of matplotlib used in this article is 3.6.2. You can install this version directly by typing the following command.
pip install matplotlib==3.6.2
II. Methods of use
Take the following piece of simple code as an example, to tell how to change the text in the drawing.
import numpy as np import as plt # Drawing curves x = (0, 2*, 100) y1 = (x) y2 = (x) (x, y1, label=u'Sine Curve') (x, y2, label=u'Cosine curve') ('Transverse Axis') ('Vertical axis') ('Functions') # Add legend (loc='upper right') # Display image ()
The above code implements the plotting of sine and cosine curves on the same graph, which is displayed as follows
It can be seen that if you don't add the font setting, the horizontal and vertical coordinates and the Chinese of the legend are boxed.
1. Find the desired font
(1) Download fonts
linux
It usually doesn't come with fonts like Microsoft Athens Black, Song, or New Roman. We need to go and find them. If you're aroundwindows
If you have a computer with the following system, you can go to the following paths to copy it
Font Name | trails |
---|---|
Microsoft Black and White (computer science) | C:\Windows\Fonts\ |
Mincho | C:\Windows\Fonts\ |
New Rome | C:\Windows\Fonts\ |
If you're too lazy, you can also download the font pack I uploaded.
Font Files
Extract code: ehtw
(2) Placement into the appropriate path
linux mint
The font files under the folder are usually placed in "/usr/share/fonts/truetype", if the directory does not exist, you can create it manually. Generally the font files will be in the truetype folder there is another layer of folders before to the font files.
Take dejavu as an example of this.
So we can also create a yahei folder for its font files
# Create yahei folder sudo mkdir /usr/share/fonts/truetype/yahei # Copy the font file to this folder sudo cp /usr/share/fonts/truetype/yahei/
(3) Refresh font cache
sudo fc-cache -f -v
(4) Confirm that the font file has been installed
Type the following code in the terminal to see the installation path of Microsoft Black
fc-list | grep -i yahei
Similarly, it is possible to view the new Roman
fc-list | grep TIMES
Check out the Song
fc-list | grep SUN
(5) Delete matplotlib cache
This step is very important and there are a lot of people who forget about it
rm rf ~/.cache/matplotlib
2. Setting fonts
(1) Setting fonts globally
Just add the following code in front of the previous code.
# Set the font [''] = 'Microsoft YaHei'
The final code is as follows:
import numpy as np import as plt # Set the font [''] = 'Microsoft YaHei' # [''] = 'SimSun' # [''] = 'Times New Roman' # Drawing curves x = (0, 2*, 100) y1 = (x) y2 = (x) (x, y1, label=u'Sine Curve') (x, y2, label=u'Cosine curve') ('Transverse Axis') ('Vertical axis') ('Functions') # Add legend (loc='upper right') # Display image ()
Run the code and see that the drawing appears in Chinese.
particle marking the following noun as a direct object[''] = 'Microsoft YaHei'
This line is commented out and replaced with[''] = 'SimSun'
It will be displayed in Song font.
If you turn on the[''] = 'Times New Roman'
. We found that the box appeared again, apparently the new Roman does not support the Chinese display, only the numbers become the new Roman.
To set different fonts for targets in different locations, you can modify the code as follows
import numpy as np import as plt # Set the font [''] = 'SimSun' [''] = 'Times New Roman' # Drawing curves x = (0, 2*, 100) y1 = (x) y2 = (x) (x, y1, label=u'Sine Curve') (x, y2, label=u'Cosine curve') # Set the horizontal axis font ('Transverse Axis', fontfamily='SimSun') # Set the vertical axis font ('Vertical axis', fontfamily='SimSun') # Set the title font ('Functions', fontfamily='SimSun') # Set the scale font (fontfamily='Times New Roman') (fontfamily='Times New Roman') # Set the legend font (loc='upper right', fontsize=12, edgecolor='black', fancybox=False, framealpha=1, prop={'family': 'SimSun'}) # Display image ()
Final Results
(2) Setting fonts locally (highly recommended)
The above setup seems to be a bit cumbersome when you have to copy or download the font files again on a new machine. Now we introduce the second method, directly put yourself in the same directory as the running program or script. This way the fonts follow the code and can be run directly in a new environment with matplotlib installed.
import numpy as np import as plt import matplotlib.font_manager as fm # Specify the font font_path = '' # Here you can also fill in the absolute path of other fonts, just fill in the name means that this program is in the same directory as the font. font_prop = (fname=font_path, size=12) en_font_path = '' # Here you can also fill in the absolute path of other fonts, just fill in the name means that this program is in the same directory as the font. en_font_prop = (fname=en_font_path, size=12) x = (0, 2 * , 100) y1 = (x) y2 = (x) (x, y1, label=u'Sine Curve') (x, y2, label=u'Cosine curve') ('Transverse Axis', fontproperties=font_prop) ('Vertical axis', fontproperties=font_prop) ('Functions', fontproperties=font_prop) (fontproperties=en_font_prop) (fontproperties=en_font_prop) (loc='upper right', fontsize=12, edgecolor='black', fancybox=False, framealpha=1, prop=font_prop) ()
View Display Results
summarize
This article briefly describes how to use the matplotlib library to add Chinese fonts display approach. One for the global settings, one for the current program settings.
to this article on the Python common library matplotlib learning notes to draw the text of the Chinese display of the article is introduced to this, more related to matplotlib drawing text Chinese display content please search my previous posts or continue to browse the following related articles I hope you will support me in the future!