Python 绘图字体控制 + 文字在图片中的位置调整
文章目录
一、字体控制
1、字体名称 - family
2、字体风格 - style
3、字体粗细 - weight
4、字体颜色 - color
5、字体大小 - size
6、字体拉伸
6、其他
二、关于图片内容在图上的位置
一、字体控制
参考:
python画图字体设置:https://blog.csdn.net/HsinglukLiu/article/details/107821714
Matplotlib中修改字体属性:https://zhuanlan.zhihu.com/p/32150177
matplotlib入门–font:https://zhuanlan.zhihu.com/p/35983270
font = {'family': 'serif',
'style': 'italic',
'weight': 'normal',
'color': 'darkred',
'size': 16,
}
python 中经常使用一个字典来控制字体,如上,字体的属性常用的有5个:
1、字体名称 - family
通过输出matplotlib.font_manager.fontManager.ttflist中的字体来查看都可以设置哪些可选字体
import matplotlib
a = sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
previous_font = ' '
count = 0
for font_name in a:
if(font_name != previous_font):
count = count + 1
print(font_name, '\t\t\t\t', end='')
if(count > 2):
print('{:35}'.format(font_name), end='')
if(count % 4 == 0):
print()
previous_font = font_name
得到的可以设置的字体可选集合如下:
Adobe Devanagari Agency FB
Algerian Arial Arial Rounded MT Bold Arial Unicode MS
Baskerville Old Face Bauhaus 93 Bell MT Berlin Sans FB
Berlin Sans FB Demi Bernard MT Condensed Blackadder ITC Bodoni MT
Book Antiqua Bookman Old Style Bookshelf Symbol 7 Bradley Hand ITC
Britannic Bold Broadway Brush Script MT Calibri
Californian FB Calisto MT Cambria Candara
Castellar Centaur Century Century Gothic
Century Schoolbook Chiller Colonna MT Comic Sans MS
Consolas Constantia Cooper Black Copperplate Gothic Bold
Copperplate Gothic Light Corbel Courier New Curlz MT
DejaVu Sans DejaVu Sans Display DejaVu Sans Mono DejaVu Serif
DejaVu Serif Display DengXian ESRI AMFM Electric ESRI AMFM Gas
ESRI AMFM Sewer ESRI AMFM Water ESRI ArcPad ESRI Arrowhead
ESRI Business ESRI Cartography ESRI Caves 1 ESRI Caves 2
ESRI Caves 3 ESRI Climate & Precipitation ESRI Commodities ESRI Conservation
ESRI Crime Analysis ESRI Default Marker ESRI Dimensioning ESRI ERS Infrastructures S1
ESRI ERS Operations S1 ESRI Elements ESRI Enviro Hazard Analysis ESRI Enviro Hazard Incident
ESRI Enviro Hazard Sites ESRI Environmental & Icons ESRI Fire Incident NFPA ESRI Geology
ESRI Geology AGSO 1 ESRI Geology USGS 95-525 ESRI Geometric Symbols ESRI Hazardous Materials
ESRI Hydrants ESRI IGL Font16 ESRI IGL Font20 ESRI IGL Font21
ESRI IGL Font22 ESRI IGL Font23 ESRI IGL Font24 ESRI IGL Font25
ESRI Meteorological 01 ESRI Mil2525C Modifiers ESRI MilMod 01 ESRI MilMod 02
ESRI MilRed 01 ESRI MilSym 01 ESRI MilSym 02 ESRI MilSym 03
ESRI MilSym 04 ESRI MilSym 05 ESRI NIMA City Graphic LN ESRI NIMA City Graphic PT
ESRI NIMA DNC LN ESRI NIMA DNC PT ESRI NIMA VMAP1&2 LN ESRI NIMA VMAP1&2 PT
ESRI North ESRI Oil, Gas, & Water ESRI Ordnance Survey ESRI Pipeline US 1
ESRI Public1 ESRI SDS 1.95 1 ESRI SDS 1.95 2 ESRI SDS 2.00 1
ESRI SDS 2.00 2 ESRI Shields ESRI Surveyor ESRI Telecom
ESRI Transportation & Civic ESRI US Forestry 1 ESRI US Forestry 2 ESRI US MUTCD 1
ESRI US MUTCD 2 ESRI US MUTCD 3 ESRI Weather Ebrima
Edwardian Script ITC Elephant Engravers MT Eras Bold ITC
Eras Demi ITC Eras Light ITC Eras Medium ITC Euclid
Euclid Extra Euclid Fraktur Euclid Math One Euclid Math Two
Euclid Symbol FZCuHeiSongS-B-GB FZLanTingHeiS-UL-GB FZShuTi
FZYaoTi FangSong Felix Titling Fences
Footlight MT Light Forte Franklin Gothic Book Franklin Gothic Demi
Franklin Gothic Demi Cond Franklin Gothic Heavy Franklin Gothic Medium Franklin Gothic Medium Cond
Freestyle Script French Script MT Gabriola Gadugi
Garamond Georgia Gigi Gill Sans MT
Gill Sans MT Condensed Gill Sans MT Ext Condensed Bold Gill Sans Ultra Bold Gill Sans Ultra Bold Condensed
Gloucester MT Extra Condensed Goudy Old Style Goudy Stout Haettenschweiler
Harlow Solid Italic Harrington High Tower Text HoloLens MDL2 Assets
Impact Imprint MT Shadow Informal Roman Javanese Text
Jokerman Juice ITC KaiTi Kristen ITC
Kunstler Script Leelawadee Leelawadee UI LiSu
Lucida Bright Lucida Calligraphy Lucida Console Lucida Fax
Lucida Handwriting Lucida Sans Lucida Sans Typewriter Lucida Sans Unicode
MS Gothic MS Outlook MS Reference Sans Serif MS Reference Specialty
MT Extra MT Extra Tiger MV Boli Magneto
Maiandra GD Malgun Gothic Marlett Matura MT Script Capitals
Meiryo Microsoft Himalaya Microsoft JhengHei Microsoft MHei
Microsoft NeoGothic Microsoft New Tai Lue Microsoft PhagsPa Microsoft Sans Serif
Microsoft Tai Le Microsoft Uighur Microsoft YaHei Microsoft Yi Baiti
MingLiU-ExtB Mistral Modern No. 20 Mongolian Baiti
Monotype Corsiva Myanmar Text Niagara Engraved Niagara Solid
Nirmala UI NumberOnly OCR A Extended Old English Text MT
Onyx OriginGISSymbols Palace Script MT Palatino Linotype
Papyrus Parchment Perpetua Perpetua Titling MT
Playbill Poor Richard Pristina Rage Italic
Ravie Rockwell Rockwell Condensed Rockwell Extra Bold
STCaiyun STFangsong STHupo STIXGeneral
STIXNonUnicode STIXSizeFiveSym STIXSizeFourSym STIXSizeOneSym
STIXSizeThreeSym STIXSizeTwoSym STKaiti STLiti
STSong STXihei STXingkai STXinwei
STZhongsong Script MT Bold Segoe MDL2 Assets Segoe Print
Segoe Script Segoe UI Segoe UI Emoji Segoe UI Historic
Segoe UI Symbol Segoe WP Showcard Gothic SimHei
SimSun SimSun-ExtB Sitka Small Snap ITC
Stencil Sylfaen Symbol Symbol Tiger
Symbol Tiger Expert Tahoma Tempus Sans ITC Tiger
Tiger Expert Times New Roman Trebuchet MS Tw Cen MT
Tw Cen MT Condensed Tw Cen MT Condensed Extra Bold Verdana Viner Hand ITC
Vivaldi Vladimir Script Webdings Wide Latin
Wingdings Wingdings 2 Wingdings 3 YouYuan
Yu Gothic ZWAdobeF cmb10 cmex10
cmmi10 cmr10 cmss10 cmsy10
cmtt10 hakuyoxingshu7000 icomoon
如果只需要中文字体,那么可以选择:
字体 字体名
黑体 SimHei
楷体 KaiTi
隶书 LiSu
幼圆 YouYuan
华文细黑 STXihei
华文楷体 STKaiti
华文宋体 STSong
华文中宋 STZhongsong
华文仿宋 STFangsong
方正舒体 FZShuTi
方正姚体 FZYaoti
华文彩云 STCaiyun
华文琥珀 STHupo
华文隶书 STLiti
华文行楷 STXingkai
华文新魏 STXinwei
2、字体风格 - style
字体的style一般可选的有如下三个:
normal:常规的
italic:斜体字
oblique:倾斜的文字,对于没有斜体(italic)的字体应该使用Oblique属性值来实现倾斜的文字效果
3、字体粗细 - weight
这个属性可以使用数字0-1000,也可以使用英文单词
weight: A numeric value in the range 0-1000
or one of ‘ultralight’, ‘light’, ‘normal’,
‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’,
‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’
其中normal指的是正常字体粗细,对应的大小为400;bold指的是粗体字,对应的大小为700;light是比正常字体更细的字体。数字后面的单位,笔者还不知
4、字体颜色 - color
关于颜色,除了使用单词之外,我们还可以使用16进制来表示颜色。比如#CECECE,表示一种淡淡的灰色。更多关于的颜色的介绍可参看:
https://blog.csdn.net/Gou_Hailong/article/details/121787030
5、字体大小 - size
字体大小可以用数字表示,也可以使用单词表示
size: Either an relative value of ‘xx-small’, ‘x-small’,
‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’
or an absolute font size, e.g., 12
1
2
3
从 xx-small 到 xx-large 依次增大,默认是medium。关于字体大小的单位,笔者暂不知,盲猜是以磅做单位的,1英寸=72磅,28磅大约为1cm。
6、字体拉伸
它的取值范围可以是数字0-1000,也可以使用英文单词
stretch: A numeric value in the range 0-1000
or one of ‘ultra-condensed’, ‘extra-condensed’,
‘condensed’, ‘semi-condensed’, ‘normal’, ‘semi-expanded’,
‘expanded’, ‘extra-expanded’ or ‘ultra-expanded’
ultra-condensed是最宽的值,而 ultra-expanded是最窄的值,由于它的使用频率并不高,所以就不过多赘述。
6、其他
字体设置好之后,如果想对text进行一些其他操作,比如旋转等,下面给了一些示例:
text('Hello world',rotation=30) #绕着左下角点逆时针旋转30度
1
二、关于图片内容在图上的位置
之前研究过Matlab的图像,如下
其实Python的也很类似
fig=plt.figure(figsize=(12,6),dpi=100,facecolor='w')
ax=plt.subplot(2, 2, 2)
font2 = {'weight': 550, 'size': 12}
font = {'family': 'STXingkai',
'style': 'italic',
'stretch': 1000,
'weight': 'bold',
'color': 'darkred',
'size': 28,
}
s='Hello World!'
ax.text(0.01, 0.01, s, bbox=dict(facecolor='none', alpha=0.0, pad=6), fontdict=font, transform=ax.transAxes)
ax.set_title('Correlation between features', fontsize=18, position=(-0.5,1.05))
plt.show()