<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【Python】Python+Matplotlib+LaTeX玩轉(zhuǎn)數(shù)學(xué)公式

          共 3878字,需瀏覽 8分鐘

           ·

          2022-02-17 05:56

          本文介紹如何在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?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})

          #二項式
          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實現(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-


          往期精彩回顧




          瀏覽 63
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  97红桃视频 | 超91av在线视频 | 国产一级免费在线 | 欧美性猛交XXXXX水多 | 欧美亚洲综合色图 |