Matplotlib使用Latex中文
Win10下安装了MiKTEX,Matplotlib需要使用 pgf 后端,将图形导出为可以使用 pdflatex,xelatex 或 lualatex 处理的 pgf 绘图命令。 这里使用XeLaTeX ,其具有完整的 unicode 支持,可以使用安装在操作系统中的任何字体,利用 OpenType,AAT 和 Graphite 的高级排版功能。 由plt.savefig('figure.pgf')
创建的 pgf 图片可以作为原始命令嵌入到 LaTeX 文档中。 图形也可以通过切换到该后端,直接编译并使用plt.savefig('figure.pdf')
保存到 PDF。需注意的是,必须要保存到pgf或pdf图片,Matplotlib中设置的"pgf.texsystem": "xelatex"才有效,否则会使用pdflatex编译,会涉及到Unicode报错(没有深入研究原因)。
具体示例如下:
import matplotlib as mpl
mpl.use("pgf")
mpl.rcParams.update({"text.usetex": True,# use default xelatex"pgf.texsystem": "xelatex","font.family": "sans-serif","font.sans-serif": "Times New Roman","pgf.preamble": ["\\usepackage{xeCJK}", # load CJK package"\\usepackage{metalogo}","\\usepackage{unicode-math}", # unicode math setup"\\usepackage{fontspec}", r"\setmainfont{Times New Roman}", r"\setCJKmainfont{SimHei}", # serif font via preambler"\makeatletter \newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #1@} \makeatother",]})import matplotlib.pyplot as pltplt.rcParams['text.latex.preamble']=r'\makeatletter \newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #1@} \makeatother'figure = plt.figure(figsize=(18, 10))
ax = figure.add_axes([0.05,0.1,0.88,0.72])
ax.clear()
ax.set_xlabel(r'操作电阻'+r'$R$/$m\Omega$', fontsize=20)
ax.set_ylabel(r'熔池功率'+r'$P/MW$', fontsize=20)
ax.set_xlim([0, 50])
ax.set_ylim([0, 25])
x_locator = plt.MultipleLocator(5)
y_locator = plt.MultipleLocator(5)
ax.xaxis.set_major_locator(x_locator)
ax.yaxis.set_major_locator(y_locator)
plt.tick_params(labelsize=18)
plt.xticks(fontfamily="Times New Roman")
plt.yticks(fontfamily="Times New Roman")plt.title(r"18MVA TENOVA电炉$\rom{4}$"+'\n'+"二次电压"+r'$V_2$:196-651V 13步'+'\n'+r'有效功率:'+r'$P_E=\frac{V_2^2}{(R+r)^2+X^2}R$', fontsize=22)plt.text(-1.5, 26.0, r'$cos\phi=\frac{R+r}{\sqrt{(R+r)^2+X^2}}$ 线电流$I_2 = \frac{U_2cos\phi}{R+r}=\frac{V_2/\sqrt{3}}{\sqrt{(R+r)^2+X^2}}$'+'\n'+r'$P_E =3U_EI_2=3I_2^2R$ $R=\frac{U_E}{I_2}$ $U_E$:电极至炉膛电压'+'\n短网电阻$r=0.0172m\Omega$ 短网电抗$X=1.71m\Omega$', fontsize=14, bbox=dict(facecolor='gold', alpha=1))for i in range(9):plt.vlines(5*(i+1), 0, 25, colors='grey', linestyle="-")
for j in range(0, 20, 5):plt.hlines((j+5), 0, 50, colors='grey', linestyle="-")omega = np.arange(0, 50, 0.1)
#短网电阻mΩ
contactR = 0.0172
#短网电抗mΩ
factorR = 1.71
minvolt = 196
maxvolt = 651
powermin = (minvolt**2/((omega+contactR)**2+factorR**2))*omega/1e3
plt.text(12.5, 2.5, str("%sV"%minvolt), fontsize=16, bbox=dict(facecolor='white', alpha=0.5))
powermax = (maxvolt**2/((omega+contactR)**2+factorR**2))*omega/1e3
plt.text(18.0, 22.5, str("%sV"%maxvolt), fontsize=16, bbox=dict(facecolor='white', alpha=0.5))
ax.plot(omega, powermin, linewidth=4, color='r')
ax.plot(omega, powermax, linewidth=4, color='r')
for i in range(231, 651, 35):power_temp = (i**2/((omega+contactR)**2+factorR**2))*omega/1e3ax.plot(omega, power_temp, linewidth=1, color='r')
plt.savefig('pic-TENOVA.pdf',dpi=400)
Matplotlib使用Latex中文相关推荐
- latex 中文_【小白向】LaTeX 中文入门
注:本文尚未撰写完毕,先暂存一下~(2020/06/27) 参考学习路线[1] 如何从零开始,入门 LaTeX? @孟晨 1. 卸载 CTeX 套装,安装 TeX Live 原因及教程见:TeX Li ...
- python画图中文显示_解决Linux系统中python matplotlib画图的中文显示问题
最近想学习一些python数据分析的内容,就弄了个爬虫爬取了一些数据,并打算用Anaconda一套的工具(pandas, numpy, scipy, matplotlib, jupyter)等进行一些 ...
- mac中python怎么打开中文txt文件-mac在matplotlib中显示中文的操作方法
Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形 . 通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方 ...
- python怎么显示汉字_mac在matplotlib中显示中文的操作方法
Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形 . 通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方 ...
- python不能保存中文_Python 关于matplotlib无法显示中文字体的解决方法
#### 以下面的例子说明如何解决matplotlib画图中出现中文字体无法显示问题 要画出10-12点气温变化的图像 ``` from matplotlib import pyplot as plt ...
- matplotlib 无法显示中文字体的解决方法
matplotlib 无法显示中文字体的解决方法 参考文章: (1)matplotlib 无法显示中文字体的解决方法 (2)https://www.cnblogs.com/lingLongBaby/p ...
- 解决Linux系统中python matplotlib画图的中文显示问题
解决Linux系统中python matplotlib画图的中文显示问题 参考文章: (1)解决Linux系统中python matplotlib画图的中文显示问题 (2)https://www.cn ...
- R+markdown+LaTeX 中文编译解决方案
R+markdown+LaTeX 中文编译解决方案 参考文章: (1)R+markdown+LaTeX 中文编译解决方案 (2)https://www.cnblogs.com/loca/p/45416 ...
- matplotlib 中文_Python 关于matplotlib无法显示中文字体的解决方法
#### 以下面的例子说明如何解决matplotlib画图中出现中文字体无法显示问题 要画出10-12点气温变化的图像 ``` from matplotlib import pyplot as plt ...
最新文章
- thinkpad重装系统不引导_重装系统时,如何判断Windows的启动方式是Legacy还是UEFI?...
- ConfigParser MissingSectionHeaderError: File contains no section headers.
- 我们真的需要那么多功能吗? - 国外主流开源 CMS 功能评点
- 软件测试方法单元测试例子,service单元测试例子
- 工作心得之-------关于“表现”
- ML之DataScience:基于机器学习处理数据科学(DataScience)任务(数据分析、特征工程、科学预测等)的简介、流程、案例应用执行详细攻略
- 如何在 SAP 电商云 Spartacus UI 里访问 CMS Component data 数据
- table 合并列_Table.Group相邻的两个数合并案例
- testmeshpro合批_TextMesh Pro Emoji Align With Text(表情和文字对齐)
- 雷林鹏分享:C# 匿名方法
- 使用Faster_RCNN做文本检测
- Acrobat Pro DC 教程,如何使用文本和图片文件创建 PDF?
- matlab2010b数值分析,matlab2010b教程
- 旅游推荐系统毕业设计总结(包含旅游信息爬取、算法应用和旅游推荐系统实现)
- CrownCAD 旋转凸台特征
- 软件项目管理第4版课后习题第十六章
- [转载]你们要的GIF动图制作全攻略!看完就会做!(实操教程)
- 几何光学学习笔记(18)- 5.1光阑在光学系统中的作用
- n个元素进栈,有几种出栈方式
- leetcode(575)分糖果