日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

【Python】Python+Matplotlib+LaTeX玩转数学公式

發(fā)布時間:2025/3/12 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】Python+Matplotlib+LaTeX玩转数学公式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文介紹如何在Matplotlib中使用LaTeX 公式和符號Python如何生成LaTeX數(shù)學(xué)公式


1、Matplotlib中使用LaTeX 公式和符號

一些配置

  • 安裝兩個軟件,鏈接給出。

  • https://mirrors.cqu.edu.cn/CTAN/systems/windows/protext/protext-3.2-033020.zip

  • https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs9531/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?pltplt.style.use('fivethirtyeight') mpl.rcParams['text.usetex']?=?True#默認(rèn)為false,此處設(shè)置為TRUE

Matplotlib中使用Latex字符和公式

mpl.rcParams['lines.linewidth']?=?1fig,?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})#開根號 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ù)學(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é)公式

  • 一個求積分公式,借助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)
  • 一個混合公式,借助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)

?

參考資料

https://matplotlib.org/tutorials/text/usetex.html
https://github.com/connorferster/handcalcs
https://github.com/google/latexify_py

-END-

往期精彩回顧適合初學(xué)者入門人工智能的路線及資料下載(圖文+視頻)機(jī)器學(xué)習(xí)入門系列下載中國大學(xué)慕課《機(jī)器學(xué)習(xí)》(黃海廣主講)機(jī)器學(xué)習(xí)及深度學(xué)習(xí)筆記等資料打印《統(tǒng)計學(xué)習(xí)方法》的代碼復(fù)現(xiàn)專輯 AI基礎(chǔ)下載機(jī)器學(xué)習(xí)交流qq群955171419,加入微信群請掃碼:

總結(jié)

以上是生活随笔為你收集整理的【Python】Python+Matplotlib+LaTeX玩转数学公式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。