欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何在google colab matplotlib画图中使用中文

程序员文章站 2022-03-15 15:01:56
如何在google colab matplotlib画图中使用中文首先要有中文字体:!gdown --id 1fsKERl26TNTFIY25PhReoCujxwJvfyHnimport matplotlib as mplzhfont = mpl.font_manager.FontProperties(fname='SimHei .ttf')其次在使用的中文字符串前加u,并在后面附上中文字体名。plt.title("拍摄角度:0,5,10",fontproperties=zhfont)plt...

如何在google colab matplotlib画图中使用中文

首先要有中文字体:

!gdown --id 1fsKERl26TNTFIY25PhReoCujxwJvfyHn
import matplotlib as mpl
zhfont = mpl.font_manager.FontProperties(fname='SimHei .ttf')

其次在使用的中文字符串前加u,并在后面附上中文字体名。

plt.title("拍摄角度:0,5,10",fontproperties=zhfont)
plt.xlabel(u"实际接触角",fontproperties=zhfont)
plt.legend(['0','5',u'10度'],loc='best',prop=zhfont)

完整代码:

!gdown --id 1fsKERl26TNTFIY25PhReoCujxwJvfyHn
import matplotlib as mpl
zhfont = mpl.font_manager.FontProperties(fname='SimHei .ttf')

import numpy as np
import matplotlib.pyplot as plt

theta = range(0,91)
alpha = range(0,46)
contact_angle = np.zeros((91,46),dtype=np.float64)

for j in alpha:
  for i in theta:
    pass
    contact_angle[i][j] = np.arctan(np.cos(j*np.pi/180)*np.tan(i*np.pi/180))*180/np.pi-i

plt.figure()

plt.title("拍摄角度:0,5,10",fontproperties=zhfont)
plt.xlabel(u"实际接触角",fontproperties=zhfont)
plt.ylabel(u"角度误差",fontproperties=zhfont)
for i in range(0,11,5):
  plt.plot(theta,contact_angle[:,i])
  plt.legends="x"
plt.legend([u'0度',u'5度',u'10度'],loc='best',prop=zhfont)
plt.grid()
plt.show()

本文地址:https://blog.csdn.net/weixin_37959984/article/details/110429418