程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Times New Roman Fonts and Helvetica fonts for Python drawings

編輯:Python

Matplotlib Drawing Times New Roman&Helvetica

  • Problem description
  • plt How to set... In the drawing Times New Roman typeface ?
  • plt How to set... In the drawing Helvetica typeface ?
    • plt Drawing font settings
    • plt mapping Helvetica Font settings
      • Font file path
      • Font download
      • Delete matplotlib Buffer directory for
      • Modify file matplotlibrc, Increase the configuration
      • restart
      • Validation

Problem description

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 .

plt How to set... In the drawing Times New Roman typeface ?

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 :

  1. Use matplotlib Modify the font Times New Roman
  2. solve Matplotlib in Times New Roman Font cannot change font
    The methods of the above two articles are roughly the same , Hope to solve the font problem . Praise the two bloggers .

plt How to set... In the drawing Helvetica typeface ?

This is the focus of this paper .

plt Drawing font settings

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 .

plt mapping Helvetica Font settings

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 :

Font file path

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.

Font download

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 .

Delete matplotlib Buffer directory for

import matplotlib
print(matplotlib.get_cachedir())

C:\Users\Asus.matplotlib

Windows Manually delete the path file in the .

Modify file matplotlibrc, Increase the configuration

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

restart

Shut down python, restart .

Validation

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved