{"id":1042936,"date":"2024-12-31T13:01:40","date_gmt":"2024-12-31T05:01:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1042936.html"},"modified":"2024-12-31T13:01:43","modified_gmt":"2024-12-31T05:01:43","slug":"python%e7%94%bb%e5%9b%be%e5%9d%90%e6%a0%87%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e5%b8%8c%e8%85%8a%e5%ad%97%e6%af%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1042936.html","title":{"rendered":"python\u753b\u56fe\u5750\u6807\u5982\u4f55\u8f93\u5165\u5e0c\u814a\u5b57\u6bcd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2470bf8a-0b80-4e5f-93e6-42da8001d67c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u753b\u56fe\u5750\u6807\u5982\u4f55\u8f93\u5165\u5e0c\u814a\u5b57\u6bcd\" \/><\/p>\n<p><p> <strong>\u5728Python\u753b\u56fe\u4e2d\u8f93\u5165\u5e0c\u814a\u5b57\u6bcd\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684TeX\u98ce\u683c\u5b57\u7b26\u4e32\u3001Unicode\u7f16\u7801\u548cLaTeX\u8868\u8fbe\u5f0f\u3002<\/strong> <strong>\u4f7f\u7528TeX\u98ce\u683c\u5b57\u7b26\u4e32<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c<strong>\u652f\u6301\u4e30\u5bcc\u7684\u6570\u5b66\u8868\u8fbe\u548c\u7b26\u53f7\u8f93\u5165<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528TeX\u98ce\u683c\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u7f8e\u5143\u7b26\u53f7($)\u6765\u542f\u7528TeX\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5728\u5750\u6807\u8f74\u6807\u7b7e\u548c\u56fe\u4f8b\u4e2d\u8f93\u5165\u5e0c\u814a\u5b57\u6bcd\u3002\u5e38\u89c1\u7684\u5e0c\u814a\u5b57\u6bcd\u7b26\u53f7\u5305\u62ec \\alpha\u3001\\beta\u3001\\gamma \u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.xlabel(r&#39;$\\alpha$&#39;)<\/p>\n<p>plt.ylabel(r&#39;$\\beta$&#39;)<\/p>\n<p>plt.title(r&#39;Graph of $\\alpha$ vs $\\beta$&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cxlabel\u3001ylabel \u548c title \u90fd\u4f7f\u7528\u4e86 TeX \u98ce\u683c\u5b57\u7b26\u4e32\u6765\u663e\u793a\u5e0c\u814a\u5b57\u6bcd \u03b1 \u548c \u03b2\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Unicode\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528 Unicode \u5b57\u7b26\u6765\u8868\u793a\u5e0c\u814a\u5b57\u6bcd\u3002\u5728 Matplotlib \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b Unicode \u5b57\u7b26\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff0c\u5e0c\u814a\u5b57\u6bcd \u03b1 \u7684 Unicode \u7f16\u7801\u662f \\u03B1\uff0c\u03b2 \u7684 Unicode \u7f16\u7801\u662f \\u03B2\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.xlabel(&#39;\u03b1&#39;)<\/p>\n<p>plt.ylabel(&#39;\u03b2&#39;)<\/p>\n<p>plt.title(&#39;Graph of \u03b1 vs \u03b2&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cxlabel\u3001ylabel \u548c title \u90fd\u4f7f\u7528\u4e86 Unicode \u5b57\u7b26\u6765\u663e\u793a\u5e0c\u814a\u5b57\u6bcd \u03b1 \u548c \u03b2\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528LaTeX\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>Matplotlib \u8fd8\u652f\u6301\u4f7f\u7528 LaTeX \u8868\u8fbe\u5f0f\u6765\u8f93\u5165\u5e0c\u814a\u5b57\u6bcd\u548c\u5176\u4ed6\u6570\u5b66\u7b26\u53f7\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>rcParams<\/code> \u6765\u542f\u7528 LaTeX \u8868\u8fbe\u5f0f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u7684\u6587\u672c\u90fd\u5c06\u7531 LaTeX \u8fdb\u884c\u6e32\u67d3\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib as mpl<\/p>\n<p>mpl.rcParams[&#39;text.usetex&#39;] = True<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.xlabel(r&#39;$\\alpha$&#39;)<\/p>\n<p>plt.ylabel(r&#39;$\\beta$&#39;)<\/p>\n<p>plt.title(r&#39;Graph of $\\alpha$ vs $\\beta$&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u8bbe\u7f6e <code>mpl.rcParams[&#39;text.usetex&#39;]<\/code> \u4e3a <code>True<\/code>\uff0c\u542f\u7528\u4e86 LaTeX \u8868\u8fbe\u5f0f\u3002xlabel\u3001ylabel \u548c title \u4e2d\u7684 TeX \u98ce\u683c\u5b57\u7b26\u4e32\u5c06\u7531 LaTeX \u6e32\u67d3\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u6790TeX\u98ce\u683c\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 TeX \u98ce\u683c\u5b57\u7b26\u4e32\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u6570\u5b66\u8868\u8fbe\u5f0f\u548c\u7b26\u53f7\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 TeX \u98ce\u683c\u5b57\u7b26\u4e32\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5e0c\u814a\u5b57\u6bcd\uff1a\\alpha, \\beta, \\gamma, \\delta, \\epsilon, \\zeta, \\eta, \\theta, \\iota, \\kappa, \\lambda, \\mu, \\nu, \\xi, \\omicron, \\pi, \\rho, \\sigma, \\tau, \\upsilon, \\phi, \\chi, \\psi, \\omega<\/li>\n<li>\u4e0a\u6807\u548c\u4e0b\u6807\uff1ax^2, x_1<\/li>\n<li>\u5206\u6570\uff1a\\frac{a}{b}<\/li>\n<li>\u5f00\u65b9\uff1a\\sqrt{a}<\/li>\n<li>\u6c42\u548c\uff1a\\sum_{i=1}^{n}<\/li>\n<li>\u79ef\u5206\uff1a\\int_{a}^{b}<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.xlabel(r&#39;$x^2$&#39;)<\/p>\n<p>plt.ylabel(r&#39;$\\frac{a}{b}$&#39;)<\/p>\n<p>plt.title(r&#39;Graph of $x^2$ vs $\\frac{a}{b}$&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cxlabel \u663e\u793a\u7684\u662f x^2\uff0cylabel \u663e\u793a\u7684\u662f\u5206\u6570 a\/b\uff0ctitle \u663e\u793a\u7684\u662f x^2 \u5bf9 a\/b \u7684\u56fe\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728 Matplotlib \u4e2d\u4f7f\u7528 TeX \u98ce\u683c\u5b57\u7b26\u4e32\u3001Unicode \u7f16\u7801\u548c LaTeX \u8868\u8fbe\u5f0f\u6765\u8f93\u5165\u5e0c\u814a\u5b57\u6bcd\u548c\u5176\u4ed6\u6570\u5b66\u7b26\u53f7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u542f\u7528 LaTeX \u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>plt.rc(&#39;text&#39;, usetex=True)<\/p>\n<p>plt.rc(&#39;font&#39;, family=&#39;serif&#39;)<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure(figsize=(8, 6))<\/p>\n<p>plt.plot(x, y, label=r&#39;$\\sin(x)$&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>plt.xlabel(r&#39;$x$ (\u5355\u4f4d\uff1a$\\alpha$)&#39;, fontsize=14)<\/p>\n<p>plt.ylabel(r&#39;$y = \\sin(x)$&#39;, fontsize=14)<\/p>\n<p>plt.title(r&#39;$\\sin(x)$ \u7684\u56fe\u5f62&#39;, fontsize=16)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend(loc=&#39;upper right&#39;, fontsize=12)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86 LaTeX \u8868\u8fbe\u5f0f\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u6807\u7b7e\u3001\u6807\u9898\u548c\u56fe\u4f8b\u3002xlabel \u5305\u542b\u4e86\u5355\u4f4d \u03b1\uff0cylabel \u663e\u793a\u7684\u662f\u51fd\u6570 y = sin(x)\uff0ctitle \u663e\u793a\u7684\u662f sin(x) \u7684\u56fe\u5f62\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728 Matplotlib \u4e2d\u8f7b\u677e\u5b9e\u73b0\u590d\u6742\u7684\u6570\u5b66\u8868\u8fbe\u548c\u7b26\u53f7\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7ed8\u56fe\u4e2d\u4f7f\u7528\u5e0c\u814a\u5b57\u6bcd\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u56fe\u5f62\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u6807\u7b7e\u4e2d\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u548c\u5e0c\u814a\u5b57\u6bcd\u7684\u540d\u79f0\u6765\u8f93\u5165\u5e0c\u814a\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>r&#39;$\\alpha$&#39;<\/code>\u53ef\u4ee5\u663e\u793a\u5e0c\u814a\u5b57\u6bcd\u03b1\u3002\u786e\u4fdd\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff08\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0ar\uff09\u4ee5\u6b63\u786e\u89e3\u6790\u53cd\u659c\u6760\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5e0c\u814a\u5b57\u6bcd\u7684\u6837\u5f0f\u548c\u5927\u5c0f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7684\u6587\u672c\u5c5e\u6027\u6765\u8c03\u6574\u5e0c\u814a\u5b57\u6bcd\u7684\u5b57\u4f53\u3001\u5927\u5c0f\u548c\u6837\u5f0f\u3002\u5728\u8c03\u7528<code>plt.text()<\/code>\u6216<code>plt.xlabel()<\/code>\u7b49\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165<code>fontsize<\/code>\u3001<code>fontweight<\/code>\u7b49\u53c2\u6570\uff0c\u4ee5\u81ea\u5b9a\u4e49\u6587\u672c\u7684\u5916\u89c2\u3002<\/p>\n<p><strong>\u5728\u56fe\u4f8b\u4e2d\u5982\u4f55\u663e\u793a\u5e26\u6709\u5e0c\u814a\u5b57\u6bcd\u7684\u6807\u7b7e\uff1f<\/strong><br \/>\u5728\u56fe\u4f8b\u4e2d\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u5e0c\u814a\u5b57\u6bcd\u3002\u60a8\u53ef\u4ee5\u5728<code>plt.legend()<\/code>\u51fd\u6570\u7684\u6807\u7b7e\u53c2\u6570\u4e2d\u4f7f\u7528\u7c7b\u4f3c\u7684\u8bed\u6cd5\u3002\u4f8b\u5982\uff0c<code>plt.legend([&#39;\u6570\u636e\u96c6 1: $\\\\alpha$&#39;, &#39;\u6570\u636e\u96c6 2: $\\\\beta$&#39;])<\/code>\u4f1a\u5728\u56fe\u4f8b\u4e2d\u663e\u793a\u5e26\u6709\u5e0c\u814a\u5b57\u6bcd\u7684\u6807\u7b7e\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u5177\u4e13\u4e1a\u6027\u548c\u79d1\u5b66\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u753b\u56fe\u4e2d\u8f93\u5165\u5e0c\u814a\u5b57\u6bcd\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684TeX\u98ce\u683c\u5b57\u7b26\u4e32\u3001Unicode\u7f16\u7801\u548c [&hellip;]","protected":false},"author":3,"featured_media":1042943,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042936"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1042936"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042936\/revisions"}],"predecessor-version":[{"id":1042948,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042936\/revisions\/1042948"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1042943"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1042936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1042936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1042936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}