{"id":1087366,"date":"2025-01-08T13:34:23","date_gmt":"2025-01-08T05:34:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087366.html"},"modified":"2025-01-08T13:34:26","modified_gmt":"2025-01-08T05:34:26","slug":"python%e7%94%bb%e5%9b%be%e6%97%b6%e5%a6%82%e4%bd%95%e8%ae%a9%e8%b4%9f%e5%8f%b7%e6%ad%a3%e5%b8%b8%e6%98%be%e7%a4%ba-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087366.html","title":{"rendered":"python\u753b\u56fe\u65f6\u5982\u4f55\u8ba9\u8d1f\u53f7\u6b63\u5e38\u663e\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200333\/f0b9a498-580b-4eed-8d17-66a07ecacb02.webp\" alt=\"python\u753b\u56fe\u65f6\u5982\u4f55\u8ba9\u8d1f\u53f7\u6b63\u5e38\u663e\u793a\" \/><\/p>\n<p><p> \u5728Python\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u8d1f\u53f7\u7684\u663e\u793a\u95ee\u9898\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528Matplotlib\u7ed8\u56fe\u65f6\u3002<strong>\u89e3\u51b3\u8d1f\u53f7\u663e\u793a\u95ee\u9898\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u5b89\u88c5\u5e76\u914d\u7f6e\u5408\u9002\u7684\u5b57\u4f53\u3001\u8bbe\u7f6eMatplotlib\u7684\u53c2\u6570\u3001\u786e\u4fdd\u7cfb\u7edf\u73af\u5883\u652f\u6301\u8d1f\u53f7\u663e\u793a<\/strong>\u3002\u5176\u4e2d\uff0c\u914d\u7f6eMatplotlib\u7684\u53c2\u6570\u662f\u6700\u5e38\u7528\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u5e76\u914d\u7f6e\u5408\u9002\u7684\u5b57\u4f53<\/strong><\/h2>\n<ol>\n<li>\u5b89\u88c5\u652f\u6301\u8d1f\u53f7\u7684\u5b57\u4f53<\/li>\n<\/ol>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cMatplotlib\u4f7f\u7528\u7684\u5b57\u4f53\u53ef\u80fd\u4e0d\u652f\u6301\u8d1f\u53f7\u663e\u793a\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u652f\u6301\u8d1f\u53f7\u663e\u793a\u7684\u5b57\u4f53\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u5e38\u7528\u7684\u652f\u6301\u8d1f\u53f7\u7684\u5b57\u4f53\u6709SimHei\u548cDejaVu Sans\u3002<\/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>\u8bbe\u7f6e\u5b57\u4f53\u4e3aSimHei\uff0c\u786e\u4fdd\u8d1f\u53f7\u663e\u793a<\/strong><\/h2>\n<p>plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]  # \u7528\u6765\u6b63\u5e38\u663e\u793a\u4e2d\u6587\u6807\u7b7e<\/p>\n<p>plt.rcParams[&#39;axes.unicode_minus&#39;] = False  # \u7528\u6765\u6b63\u5e38\u663e\u793a\u8d1f\u53f7<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = x  2<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&#39;\u793a\u4f8b\u56fe\u8868&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u6b63\u786e\u5b89\u88c5<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53\uff0c\u786e\u4fdd\u8be5\u5b57\u4f53\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u5728\u4f60\u7684\u7cfb\u7edf\u4e2d\uff0c\u5e76\u4e14Matplotlib\u80fd\u591f\u627e\u5230\u5e76\u4f7f\u7528\u8be5\u5b57\u4f53\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eMatplotlib\u7684\u5b57\u4f53\u8def\u5f84\u6765\u6307\u5b9a\u5b57\u4f53\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.font_manager as fm<\/p>\n<h2><strong>\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u5b57\u4f53\u8def\u5f84<\/strong><\/h2>\n<p>font_path = &#39;\/path\/to\/your\/font.ttf&#39;<\/p>\n<p>font_prop = fm.FontProperties(fname=font_path)<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = x  2<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&#39;\u793a\u4f8b\u56fe\u8868&#39;, fontproperties=font_prop)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;, fontproperties=font_prop)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;, fontproperties=font_prop)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u8bbe\u7f6eMatplotlib\u7684\u53c2\u6570<\/strong><\/h2>\n<ol>\n<li>\u4f7f\u7528rcParams\u8bbe\u7f6e\u5168\u5c40\u53c2\u6570<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7Matplotlib\u7684rcParams\uff0c\u53ef\u4ee5\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u548c\u8d1f\u53f7\u663e\u793a\u53c2\u6570\uff0c\u786e\u4fdd\u5728\u6240\u6709\u56fe\u8868\u4e2d\u8d1f\u53f7\u6b63\u5e38\u663e\u793a\u3002<\/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>\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u548c\u8d1f\u53f7\u663e\u793a<\/strong><\/h2>\n<p>plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]  # \u7528\u6765\u6b63\u5e38\u663e\u793a\u4e2d\u6587\u6807\u7b7e<\/p>\n<p>plt.rcParams[&#39;axes.unicode_minus&#39;] = False  # \u7528\u6765\u6b63\u5e38\u663e\u793a\u8d1f\u53f7<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = x  2<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&#39;\u793a\u4f8b\u56fe\u8868&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528rc\u8bbe\u7f6e\u5c40\u90e8\u53c2\u6570<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u53ea\u60f3\u5728\u7279\u5b9a\u56fe\u8868\u4e2d\u8bbe\u7f6e\u8d1f\u53f7\u663e\u793a\uff0c\u53ef\u4ee5\u4f7f\u7528rc\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u5c40\u90e8\u8bbe\u7f6e\u53c2\u6570\u3002<\/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>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = x  2<\/p>\n<p>with plt.rc_context({&#39;font.sans-serif&#39;: [&#39;SimHei&#39;], &#39;axes.unicode_minus&#39;: False}):<\/p>\n<p>    plt.plot(x, y)<\/p>\n<p>    plt.title(&#39;\u793a\u4f8b\u56fe\u8868&#39;)<\/p>\n<p>    plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>    plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>    plt.grid(True)<\/p>\n<p>    plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u786e\u4fdd\u7cfb\u7edf\u73af\u5883\u652f\u6301\u8d1f\u53f7\u663e\u793a<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7cfb\u7edf\u73af\u5883\u53ef\u80fd\u4f1a\u5f71\u54cdMatplotlib\u7684\u8d1f\u53f7\u663e\u793a\u3002\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u73af\u5883\u652f\u6301\u8d1f\u53f7\u663e\u793a\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u68c0\u67e5\u548c\u914d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u68c0\u67e5\u7cfb\u7edf\u73af\u5883<\/li>\n<\/ol>\n<p><p>\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u652f\u6301UTF-8\u7f16\u7801\uff0c\u5e76\u4e14\u6b63\u786e\u5b89\u88c5\u4e86\u652f\u6301\u8d1f\u53f7\u663e\u793a\u7684\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import locale<\/p>\n<h2><strong>\u68c0\u67e5\u5f53\u524d\u7cfb\u7edf\u73af\u5883<\/strong><\/h2>\n<p>print(locale.getpreferredencoding())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bbe\u7f6e\u7cfb\u7edf\u73af\u5883\u53d8\u91cf<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u7cfb\u7edf\u73af\u5883\u4e0d\u652f\u6301\u8d1f\u53f7\u663e\u793a\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>os.environ[&#39;LC_ALL&#39;] = &#39;C.UTF-8&#39;<\/p>\n<p>os.environ[&#39;LANG&#39;] = &#39;C.UTF-8&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u7ed8\u56fe\uff0c\u5e76\u786e\u4fdd\u8d1f\u53f7\u6b63\u5e38\u663e\u793a\u3002<\/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>\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u548c\u8d1f\u53f7\u663e\u793a<\/strong><\/h2>\n<p>plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]  # \u7528\u6765\u6b63\u5e38\u663e\u793a\u4e2d\u6587\u6807\u7b7e<\/p>\n<p>plt.rcParams[&#39;axes.unicode_minus&#39;] = False  # \u7528\u6765\u6b63\u5e38\u663e\u793a\u8d1f\u53f7<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = x  2<\/p>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.plot(x, y, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.title(&#39;\u793a\u4f8b\u56fe\u8868&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5728Python\u4f7f\u7528Matplotlib\u7ed8\u56fe\u65f6\uff0c\u8d1f\u53f7\u80fd\u591f\u6b63\u5e38\u663e\u793a\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7ed8\u56fe\u4e2d\u786e\u4fdd\u8d1f\u53f7\u6b63\u5e38\u663e\u793a\uff1f<\/strong><br \/>\u5728Python\u7ed8\u56fe\u4e2d\uff0c\u8d1f\u53f7\u7684\u663e\u793a\u901a\u5e38\u4e0e\u5b57\u4f53\u548c\u56fe\u5f62\u5e93\u8bbe\u7f6e\u6709\u5173\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f7f\u7528\u7684\u5b57\u4f53\u652f\u6301\u8d1f\u53f7\u5b57\u7b26\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528Matplotlib\u4e2d\u7684<code>rcParams<\/code>\u6765\u8bbe\u7f6e\u9ed8\u8ba4\u5b57\u4f53\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>matplotlib.rcParams[&#39;font.family&#39;] = &#39;sans-serif&#39;<\/code>\u6765\u9009\u62e9\u4e00\u4e2a\u5e38\u89c1\u7684\u65e0\u886c\u7ebf\u5b57\u4f53\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u8d1f\u53f7\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Matplotlib\u65f6\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8c03\u8282\u8d1f\u53f7\u7684\u663e\u793a\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ax.set_ylim()<\/code>\u8bbe\u7f6ey\u8f74\u7684\u8303\u56f4\uff0c\u4ee5\u786e\u4fdd\u8d1f\u6570\u503c\u4e0d\u4f1a\u88ab\u88c1\u526a\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>ax.xaxis.set_major_formatter(ScalarFormatter())<\/code>\u548c<code>ax.yaxis.set_major_formatter(ScalarFormatter())<\/code>\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5750\u6807\u8f74\u7684\u683c\u5f0f\uff0c\u4ece\u800c\u786e\u4fdd\u8d1f\u53f7\u6b63\u5e38\u663e\u793a\u3002<\/p>\n<p><strong>\u5982\u679c\u8d1f\u53f7\u4ecd\u7136\u65e0\u6cd5\u6b63\u5e38\u663e\u793a\uff0c\u6709\u4ec0\u4e48\u5176\u4ed6\u8c03\u8bd5\u65b9\u6cd5\u5417\uff1f<\/strong><br \/>\u53ef\u4ee5\u68c0\u67e5\u7ed8\u56fe\u73af\u5883\u7684\u8bbe\u7f6e\uff0c\u4f8b\u5982Jupyter Notebook\u6216\u5176\u4ed6IDE\u3002\u6709\u65f6\u5019\uff0c\u73af\u5883\u7684\u5b57\u4f53\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5f71\u54cd\u8d1f\u53f7\u7684\u663e\u793a\u3002\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u8fdb\u884c\u5c1d\u8bd5\uff0c\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u7ed8\u56fe\u5e93\u5982Seaborn\u6216Plotly\u6765\u67e5\u770b\u8d1f\u53f7\u7684\u8868\u73b0\u662f\u5426\u6b63\u5e38\u3002\u5982\u679c\u95ee\u9898\u4f9d\u7136\u5b58\u5728\uff0c\u8003\u8651\u66f4\u65b0Python\u548c\u76f8\u5173\u5e93\u5230\u6700\u65b0\u7248\u672c\uff0c\u4ee5\u786e\u4fdd\u517c\u5bb9\u6027\u548c\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u8d1f\u53f7\u7684\u663e\u793a\u95ee\u9898\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528Matplotlib\u7ed8\u56fe\u65f6\u3002\u89e3\u51b3\u8d1f [&hellip;]","protected":false},"author":3,"featured_media":1087377,"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\/1087366"}],"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=1087366"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087366\/revisions"}],"predecessor-version":[{"id":1087380,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087366\/revisions\/1087380"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087377"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}