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

Matplotlib font setting method

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

Share

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

This article introduces the "matplotlib font setting method" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

1 introduction

Matplotlib as a sharp tool for data visualization, is widely used in data analysis, but not very friendly is written in matplotlib non-English text content, if you do not set the font in advance, you will find that the drawn, such as "Chinese" are displayed as "box garbled", and today's content, we will come to a small summary of matplotlib font settings in common skills.

2 common methods of setting fonts in matplotlib

The method of setting fonts in matplotlib can be divided into global settings and temporary settings according to the scope of action:

2.1 set global fonts in matplotlib

When we use matplotlib to write Chinese content by default, the reason why it appears garbled is that the default font of matplotlib is English font, so many unicode-encoded languages can't be written.

The way to set fonts globally means that we can artificially use the specified fonts for the matplotlib under the current kernel by modifying local configuration files or setting font-related parameters at the beginning of the program. There are mainly the following situations:

"use system built-in fonts"

The so-called "system comes with fonts". For example, for windows, there are many fonts that have been installed in the system under the system disk Windows/Fonts:

Figure 2

Of course, matplotlib can detect more than these fonts, and we can view all available fonts that have been recognized by default in matplotlib in the following ways:

Import matplotlib.pyplot as plt

From matplotlib import font_manager

For font in font_manager.fontManager.ttflist:

# View font name and corresponding font file name

Print (font.name, -', font.fname)

Figure 3

The name attribute of each object iterated in font_manager.fontManager.ttflist is the name we want to set the font to use. Take "Chinese Amber" as an example, we first look at the corresponding font file name:

Figure 4

Then we write two lines of program to find its corresponding name:

Figure 5

Then follow the format of the plt.rcParams ['font.sans-serif'] = font name:

Figure 6

We can see that all our characters have become "Chinese amber" at this time.

"use any font"

If the font you want is not in the system default font, and you only want to use it temporarily and do not want to install it into the system font, you can use font_manager.fontManager.addfont () to pass in the font file path to temporarily register a font. Take the tripartite font file MaShanZheng-Regular.ttf I downloaded as an example:

Figure 7 figure 82.2 setting local fonts in matplotlib

If you only want to temporarily use fonts in some elements in data visualization works, such as text annotations, you can use font_manager.FontProperties () to pass the font path as a fname parameter and pass it to the corresponding parameters of the corresponding matplotlib drawing API. Take text () as an example:

Figure 9

The scale label can refer to the following ways:

Figure 10

This is the end of the content of "matplotlib font setting method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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