In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use local fonts when Python drawing, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The font files in matplotlib are encapsulated in the font_manager submodule, and the fontManager.ttflist list covers all fonts supported by Matplotlib.
> import matplotlib.pyplot as plt > from matplotlib.font_manager import fontManager > print (fontManager.ttflist [0]) # this is the font file
There are seven basic properties in the Font class:
'The fname''name''size''stretch''style''variant''weight' stores path font name size stretching mode oblique bold style variant weight
If you want to set the font globally, you can do so by changing plt.rcParams, for example, by setting it to isoline font.
Plt.rcParams ['font.sans-serif'] =' DengXian'plt.text (0pje 0dh 'test', fontsize=20) plt.show ()
The effect is
Generally speaking, there are hundreds of font files built into the computer, so it is difficult to display them one by one, so next you want to display the first 100 fonts in one picture.
However, rcParams is not an axis property, but a property of the current drawing pane. If you want to enable other fonts locally, you can do so through the fontproperties parameter in plt.text.
Import numpy as npfrom matplotlib.font_manager import FontPropertiesx,y = np.indices ([1010]) .reshape (2100) / 10for i in range (100): font = fontManager.ttflist [I] plt.text (x [I], y [I], f "Test: {font.name}", fontsize=10, ha='left', va='bottom', fontproperties = FontProperties (fname=font.fname)) plt.show ()
Get the test font as shown in the figure
When drawing, select the font that supports Chinese, and there will be no Chinese garbled problem.
Supplement
Python realizes the Chinese display when drawing, and displays the Microsoft yahn font.
1. Install the library pip3 install matplotlib
two。 Add Chinese font Microsoft YaHei to Matplotlib (Microsoft Yahei)
The reason why Matplotlib cannot display characters in Chinese fonts by default is that it does not contain a Chinese font file, so you can import a Chinese font file into Matplotlib.
Download Microsoft Accor's ttf file online. Microsoft YaHei.ttf
D:\ python\ Lib\ site-packages\ matplotlib\ mpl-data (the new computer is on disk C python39, where the python is installed)
If you open this directory, you can see the mpl-data/fonts/ttf/, under this path. This is the path where Matplotlib introduces fonts. We just need to copy the ttf file of Microsoft Yaha font to it.
3. Modify the default font configuration for Matplotlib
Mpl-data/matplotlibrc this file, this is the Matplotlib configuration file.
Search this file for font.family, where font.family: sans-serif deletes # to open the comment on this line. The default font family for Matplotlib is now:
Font.family: sans-serif
Then search for font.sans-serif in this file, delete # to open the comment on this line, and then add Microsoft YaHei to the front of the configuration
Font.sans-serif: Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
Then Matplotlib's default sans-serif font family will be the first to use Microsoft YaHei to display fonts, and this Microsoft YaHei can display Chinese fonts.
(axes.unicode_minus: True this line comment is removed, change true to false, this is used to display the negative sign normally) (can not be done)
4. Clear the font cache (but not)
In order to better see the effect, it is best to clear the cache file generated by Matplotlib and open the ~ / .matplotlib path, where you can see fontList.json and tex.cache. The former is actually the cache file of the font list, which can be deleted: rm-rf fontList.json.
5. Of course, lazy ways to display Chinese fonts
You need to set it each time you use it.
Plt.rcParams ['font.sans-serif'] = [' SimHei'] # is used to normally display Chinese tags (dynamic settings) plt.rcParams ['axes.unicode_minus'] = False # to show all the contents of the article "how to use local fonts when drawing Python" above the minus sign. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.