General requirements for the font of illustrations in papers Times New Roman perhaps Helvetica typeface , The former need not say , The font is also good , It is also widely used , But there is a problem that the Chinese display is a box , The latter is MATLAB default font , At first, I didn't feel anything at all , Later, with the exploration of various Fonts , Of course, it is also the requirement of periodicals , Find her (Helvetica) It's beautiful !pycharm Use in python package matplotlib Carry out scientific research mapping , In these two fonts, I am thinking Helvetica Fonts can't be used , Although they are similar in appearance Arial The font can be , But with OCD, I decided to take time to solve this problem . Because I have never seen a real Helvetica What does it look like , Please listen to me carefully .
Although I prefer Helvetica( Below to H Instead ), however Times New Roman (T) Pretty good also , And the first official Western font is T, however , I don't know what's going on ,python In drawing Times New Roman Font cannot be set to thickness , It's not fatal , The fatal thing is that it always appears in bold . How about that ?( Seriously ,“ How should this be resolved ?”) A lot of methods have been searched , It's really a lot , Then with the help of the students in the group, I found the following blog posts , Solved the problem , With variable thickness T typeface , At least one word in hand, there is no worry about scientific research and drawing . I won't go into details here , Personal test 2 Time (python 3.7 and 3.8 This method was used twice ), link :
This is the focus of this paper .
First look at it. plt Medium font input requirements :
To display the corresponding font in the diagram , At least set family This one , Select the font family first ( The green arrow indicates ), then Set specific Fonts , Of course, it can also be set directly family For the name of a specific font .T Font in serif in ,H Font in sans-serif in .
Put the code directly :
import matplotlib.pyplot as plt
plt.rcParams['axes.linewidth'] = 1 # Border width
plt.rcParams['figure.dpi'] = 300 # plt.show Display resolution
font = {
'family': 'serif',
'serif': 'Times New Roman',
'weight': 'normal',
'size': 10}
plt.rc('font', **font)
plt.figure()
x = [1, 2, 3]
y = [k ** 2 for k in x]
plt.plot(x, y)
plt.title('This is Times New Roman Font', fontweight='normal')
plt.savefig(r"C:\Users\Asus\Desktop\times1.jpg", dpi=600, bbox_inches='tight', pad_inches=0.01)
plt.show()
If you want to use this setting at this time :
font = {
'family': 'sans-serif',
'sans-serif': 'Helvetica',
'weight': 'normal',
'size': 10}
congratulations , You will see the following error :
UserWarning: findfont: Font family [‘sans-serif‘] not found. Falling back to DejaVu Sans.
Then we use the default font to draw .
The solution comes from this blog :
Python Data visualization 2 speak :matplotlib Drawing Chinese font settings
as well as Font family [‘sans-serif’] not found. Falling back to DejaVu Sans. Solution
================== Methods described :
import matplotlib
print(matplotlib.matplotlib_fname())
F:\python38\lib\site-packages\matplotlib\mpl-data\matplotlibrc
find F:\python38\lib\site-packages\matplotlib\mpl-data
The path is just , Then download the font Helvetica.
The rich are the real ones , People without money ... No less than the original , Share a link :Helvetica typeface Free version Download home ; Genuine Links :Helvetica harm , Dozens of dollars, that's all . Um. .
Take the first method for example , Unzip as follows ( The decompression password is explained on the download home website ):
Take this .tff
Copy the file to this path :F:\python38\Lib\site-packages\matplotlib\mpl-data\fonts\ttf
.
Of course, you can further save it in the system font of the computer : Control panel \ Appearance and personalization \ typeface
, Then you can go to adobe It is also used in drawing software .
import matplotlib
print(matplotlib.get_cachedir())
C:\Users\Asus.matplotlib
Windows Manually delete the path file in the .
The file is still in the font path :
use txt、notepad++、python Can be opened for editing . Don't ask me why I know , Because I have tried . In about 228-259 That's ok .
# 1. Remove the following line # Number
font.family : sans-serif
# 2. Remove the words before the next line # Number , And add... After the colon Hevetica, If so, there is no need to add
font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
# 3. Remove the words before the next line # Number , And will True Change it to False
axes.unicode_minus : False
Shut down python, restart .
To the familiar experimental verification link , Prove the effectiveness of the method (effectiveness).
plt.rcParams['axes.linewidth'] = 1 # Border width
plt.rcParams['figure.dpi'] = 300 # Border width
font = {
'family': 'sans-serif',
'sans-serif': 'Helvetica',
'weight': 'normal',
'size': 10}
plt.rc('font', **font) # pass in the font dict as kwargs
plt.figure()
x = [1, 2, 3]
y = [k ** 2 for k in x]
plt.plot(x, y)
plt.title('This is Helvetica Font', fontweight='normal')
plt.savefig(r"C:\Users\Asus\Desktop\helvetica1.jpg", dpi=600, bbox_inches='tight', pad_inches=0.01)
plt.show()
And MATLAB Compare the :
Of course , As I said before ,Windows It will put Helvetica The font is made for you Arial typeface , So what you see H All are Arial, Include MATLAB and WORD The font inside , At first, I wanted to port font files directly … It turned out word There is no font file in the H typeface , How can he achieve Helvetica The font ? Asking is Arial. pit . I vomited. , I feel cheated for many years .
Okay , To this end .