網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Python+Matplotlib+LaTeX玩轉(zhuǎn)數(shù)學(xué)公式_python
作者:pythonic生物人 ? 更新時(shí)間: 2022-04-28 編程語(yǔ)言1、Matplotlib中使用LaTeX 公式和符號(hào)
一些配置
安裝兩個(gè)軟件,鏈接給出,提取碼:1234
protext-3.2-033020.zip
gs9531w64.exe
添加到環(huán)境變量中
以下兩句放到環(huán)境變量中。C:\Users\xx\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64;C:\Program Files\gs\gs9.53.1\bin;
matplotlib.rcParams修改
import?numpy?as?np import?matplotlib?as?mpl import?matplotlib.pyplot?as?plt plt.style.use('fivethirtyeight') mpl.rcParams['text.usetex']?=?True#默認(rèn)為false,此處設(shè)置為TRUE
Matplotlib中使用Latex字符和公式
mpl.rcParams['lines.linewidth']?=?1 fig,?ax?=?plt.subplots(dpi=120) N?=?500 delta?=?0.6 X?=?np.linspace(-1,?1,?N) ax.plot(X,?(1?-?np.tanh(4?*?X?/?delta))?/?2,???? ????????X,?(1.4?+?np.tanh(4?*?X?/?delta))?/?4,?"C2",? ????????X,?X?0,?"k--")???????????????????????? ? ax.set_xlabel(r'No.1:?$\alpha?>?\beta) #上下標(biāo),上標(biāo)^,下標(biāo) ax.set_ylabel(r'No.2:?$\alpha_i?>?\beta^i,rotation=45) #?#累加、累積 ax.legend((r'No.3:?$\displaystyle\sum_{i=0}^\infty?x_i,?r'No.4:?$\displaystyle\prod_{i=0}^\infty?x_i), ??????????shadow=True,?loc=(0.01,?0.52),?handlelength=1.5,?) #分?jǐn)?shù)?? ax.set_title(r'No.4:?$\frac{3}{4}) #二項(xiàng)式 ax.text(0.3,1.1,r'No.5:?$\frac{5?-?\frac{1}{x}}{4}) #開(kāi)根號(hào) ax.text(0.8,1.1,r'No.6:?$\sqrt[3]{x}) #修改字體 ##?Roman、Italic、Typewriter、CALLIGRAPHY等 ax.text(-0.8,1.1,r'No.7:?$\mathit{Italic}) ax.text(-0.8,1.0,r'$\mathsf{fonts}) #聲調(diào) ax.text(-1.2,1.1,r'No.8:?$\breve?a) #選個(gè)范圍 ax.text(-1.4,0.8,r'No.9:?$\widetilde{xyz}) #?the?arrow ax.annotate("",?xy=(-delta?/?2.,?0.1),?xytext=(delta?/?2.,?0.1), ????????????arrowprops=dict(arrowstyle="<->",?connectionstyle="arc3")) #??其它TeX?symbols ax.set_xticks([-1,?0,?1]) ax.set_xticklabels([r"No.10:?$\delta$",?r"$\pm$",?r"$\$"],?color="r",?size=15) ax.set_yticks([0,?0.5,?1]) ax.set_yticklabels([r"No.10:?$\AA$",?r"$\Downarrow$",?"$\\odot$"],?color="r",?size=15) ax.text(1.02,?0.5,?r"$\phi$",fontsize=20,?rotation=90, ????????horizontalalignment="left",?verticalalignment="center", ????????clip_on=False,?transform=ax.transAxes) #?積分、微分公式 eq1?=?(r"\begin{eqnarray*}" ???????r"\frac{\partial?\phi}{\partial?t}?+?U|\nabla?\phi|?&=&?0?" ???????r"\end{eqnarray*}") ax.text(1,?0.9,?eq1,horizontalalignment="right",?verticalalignment="top") eq2?=?(r"\begin{eqnarray*}" ???????r"\mathcal{F}?&=&?\int?f\left(?\phi,?c?\right)?dV,?\\?" ???????r"\frac{?\partial?\phi?}?{?\partial?t?}?&=&?-M_{?\phi?}?" ???????r"\frac{?\delta?\mathcal{F}?}?{?\delta?\phi?}" ???????r"\end{eqnarray*}") ax.text(0.18,?0.18,?eq2) ax.text(-1,?.30,?r"gamma:?$\gamma$",?color="r") ax.text(-1,?.18,?r"Omega:?$\Omega$",?color="b") plt.show()
2、latexify生成LaTeX 數(shù)學(xué)公式
import?math import?latexify @latexify.with_latex#調(diào)用latexify的裝飾器 def?solve(a,?b,?c): ??return?(-b?+?math.sqrt(b**2?-?4*a*c))?/?(2*a) solve
3、handcalcs生成LaTeX 數(shù)學(xué)公式
一個(gè)求積分公式,借助scipy的quad
import?handcalcs.render from?scipy.integrate?import?quad#借助scipy.quad實(shí)現(xiàn)積分
%%render a?=?2 b?=?6 n=100 z?=?quad(f,a,b)
一個(gè)混合公式,借助math模塊,
from?math?import?sqrt,cos,sin,tan,asin import?handcalcs.render
%%render #symbolic f?=?a-c**2?/?b?+?sqrt(cos(sin(b-?2?/?c)))?+?tan(a/b)?-?asin(a/c)?#Comment?part
4、Latex symbols對(duì)照表
symbols爬取自網(wǎng)站:https://matplotlib.org/tutorials/text/mathtext.html、制作速查表。
plt.figure(dpi=400) fig?=?sns.scatterplot(x='sepal?length(cm)',y='sepal?width(cm)',data=pd_iris, ????????????style=geek[:150],#添加不同類變量按照不同marker顯示 ????????????markers=[r"$"+geek[i]+"$"?for?i?in?range(150)],#自定義marker形狀 ????????????**dict(s=320), ????????????color='#01a2d9' ?????????????????????? ???????????) fig.legend(ncol=5, ???????????fontsize=10, ???????????loc=8, ???????????bbox_to_anchor=(0.45,?1),? ???????????facecolor='#eaeaea',?????????? ??????????) sns.set(style="whitegrid",font_scale=1)
原文鏈接:https://mp.weixin.qq.com/s/77GSejkc-XmzBZx0A6dp7A
相關(guān)推薦
- 2022-05-28 python非單一.py文件用Pyinstaller打包發(fā)布成exe_python
- 2022-08-22 Golang實(shí)現(xiàn)快速求冪的方法詳解_Golang
- 2022-05-02 Python?私有屬性與私有方法_python
- 2022-09-24 ASP.NET?MVC格式化日期_實(shí)用技巧
- 2023-07-05 讓Linux中的SCP遠(yuǎn)程復(fù)制不再需要輸入密碼
- 2022-03-15 PEM_read_bio_X509_AUX() failed (SSL: error:0906D06
- 2022-03-15 跨域報(bào)錯(cuò):Response to preflight request doesn‘t pass ac
- 2022-09-08 Go語(yǔ)言中并發(fā)的工作原理_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支