In the matplotlib drawing, often appear this error, although I know because there is no corresponding fonts, but the fonts will be downloaded to the target path, but still can not be used, and finally realized that, in addition to downloading the fonts to the corresponding directory, you also need to clear the cache and modify the corresponding configuration to take effect.
Solution Steps:
1. Get the font directory of matplot
import matplotlib matplotlib.matplotlib_fname()
The font path can be determined by the path: YOUR_PATH/site-packages/matplotlib/mpl-data/fonts/ttf.
2. Download the corresponding fonts
Generally, we use Simhei a little bit more, just download it directly from the internet, and the final file is.
3. Place the fonts in the corresponding positions of the fonts
Place the fonts in the corresponding folder YOUR_PATH/site-packages/matplotlib/mpl-data/fonts/ttf.
4. Clearing the cache
Get the cache directory:
import matplotlib matplotlib.get_cachedir()
Then, use rm -rf to get the path to delete the cache.
This step feels quite necessary, if you do not do this step, it seems that as long as the Chinese language will still report errors, but does not appear in Chinese, there will not be the above error.
5. Modification of configuration files
5.1 Open the file that was displayed earlier when obtaining the file directory.
YOUR_PATH/site-packages/matplotlib/mpl-data/matplotlibrc
5.2 Amend the corresponding section
(1) Delete the preceding # sign
#: sans-serif -> : sans-serif
(2) Delete the # before # and add Simhei
#: DejaVu Serif,... -> : SimHei, DejaVu Serif,...
(3) Delete the # sign and change True to False
#axes.unicode_minus: True -> axes.unicode_minus: False
6. Add two lines of code when writing the code
["-serif"]=["SimHei"] #Set the font ["axes.unicode_minus"]=False #This statement resolves the image's“-”Glitch problem with negative sign
To this point this article on matplotlib Font family ['sans-serif'] not found problem solving article is introduced to this, more related matplotlib Font family ['sans-serif'] not found content please search my previous articles or continue to browse the following related articles hope you will support me in the future! -serif'] not found content please search my previous posts or continue to browse the following related articles I hope you will support me more in the future!