Python+Matplotlib+LaTeX玩轉(zhuǎn)數(shù)學公式
1、Matplotlib中使用LaTeX 公式和符號
一些配置
安裝兩個軟件,鏈接給出,提取碼:1234
添加到環(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#默認為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)
#上下標,上標^,下標
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,?)
#分數(shù)??
ax.set_title(r'No.4:?$\frac{3}{4})
#二項式
ax.text(0.3,1.1,r'No.5:?$\frac{5?-?\frac{1}{x}}{4})
#開根號
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)
#選個范圍
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ù)學公式
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ù)學公式
一個求積分公式,借助scipy的quad
import?handcalcs.render from?scipy.integrate?import?quad#借助scipy.quad實現(xiàn)積分
%%render a?=?2 b?=?6 n=100 z?=?quad(f,a,b)

一個混合公式,借助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對照表
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)



以上就是Python+Matplotlib+LaTeX玩轉(zhuǎn)數(shù)學公式的詳細內(nèi)容,更多關(guān)于Python Matplotlib數(shù)學公式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python給指定csv表格中的聯(lián)系人群發(fā)郵件(帶附件的郵件)
這篇文章主要介紹了python給指定csv表格中的聯(lián)系人群發(fā)郵件,本文通過代碼講解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
python中的os.mkdir和os.makedirs的使用區(qū)別及如何查看某個模塊中的某些字母開頭的屬性方法
這篇文章主要介紹了python中的os.mkdir和os.makedirs的使用區(qū)別及如何查看某個模塊中的某些字母開頭的屬性方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
python生成多個只含0,1元素的隨機數(shù)組或列表的實例
今天小編就為大家分享一篇python生成多個只含0,1元素的隨機數(shù)組或列表的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
教你怎么用Python selenium操作瀏覽器對象的基礎(chǔ)API
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著怎么用Python selenium操作瀏覽器對象的基礎(chǔ)API展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
flask-SQLALchemy連接數(shù)據(jù)庫的實現(xiàn)示例
sqlalchemy是數(shù)據(jù)庫的orm框架,讓我們操作數(shù)據(jù)庫的時候不要再用sql語句了,本文就介紹了flask-SQLALchemy連接數(shù)據(jù)庫的實現(xiàn)示例,感興趣的可以了解一下2022-06-06
PyTorch的深度學習入門教程之構(gòu)建神經(jīng)網(wǎng)絡(luò)
這篇文章主要介紹了PyTorch的深度學習入門教程之構(gòu)建神經(jīng)網(wǎng)絡(luò),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06

