{"id":1168973,"date":"2025-01-15T16:04:54","date_gmt":"2025-01-15T08:04:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168973.html"},"modified":"2025-01-15T16:04:57","modified_gmt":"2025-01-15T08:04:57","slug":"python%e7%bb%98%e5%9b%be%e5%a6%82%e4%bd%95%e5%ad%97%e4%bd%93%e6%96%b0%e7%bd%97%e9%a9%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1168973.html","title":{"rendered":"python\u7ed8\u56fe\u5982\u4f55\u5b57\u4f53\u65b0\u7f57\u9a6c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070100\/e4a7ef3d-4785-4f9d-b805-b8b765828246.webp\" alt=\"python\u7ed8\u56fe\u5982\u4f55\u5b57\u4f53\u65b0\u7f57\u9a6c\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7ed8\u56fe\u65f6\uff0c\u8bbe\u7f6e\u5b57\u4f53\u4e3a\u65b0\u7f57\u9a6c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Matplotlib\u3001\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u3001\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u6b63\u786e\u3001\u52a0\u8f7d\u5b57\u4f53\u6587\u4ef6\u3001\u4fdd\u5b58\u5e76\u663e\u793a\u56fe\u50cf<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u8be6\u7ec6\u7684\u6b65\u9aa4\u662f\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u52a0\u8f7d\u5b57\u4f53\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u6b65\u5b9e\u73b0\uff1a\u9996\u5148\uff0c\u627e\u5230Times New Roman\u5b57\u4f53\u6587\u4ef6\u7684\u8def\u5f84\uff1b\u5176\u6b21\uff0c\u5c06\u5b57\u4f53\u6587\u4ef6\u52a0\u8f7d\u5230Matplotlib\uff1b\u6700\u540e\uff0c\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027\u6216\u5728\u7ed8\u56fe\u65f6\u6307\u5b9a\u5b57\u4f53\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>PYTHON\u7ed8\u56fe\u5b57\u4f53\u8bbe\u7f6e\u6307\u5357<\/strong><\/h2>\n<p><p>Python\u4f5c\u4e3a\u4e00\u95e8\u5e7f\u6cdb\u5e94\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u7ed8\u56fe\u529f\u80fd\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\u5907\u53d7\u6b22\u8fce\u3002\u5728\u7ed8\u56fe\u8fc7\u7a0b\u4e2d\uff0c\u8bbe\u7f6e\u5408\u9002\u7684\u5b57\u4f53\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u5347\u56fe\u8868\u7684\u7f8e\u89c2\u5ea6\uff0c\u8fd8\u80fd\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u4e13\u4e1a\u6027\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Times New Roman\u5b57\u4f53\u8fdb\u884c\u7ed8\u56fe\uff0c\u4e3b\u8981\u4ee5Matplotlib\u5e93\u4e3a\u4f8b\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7ed8\u56fe\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684Python\u5e93\u3002\u901a\u5e38\uff0c\u4f7f\u7528Matplotlib\u8fdb\u884c\u7ed8\u56fe\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9009\u62e9\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<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u627e\u5230Times New Roman\u5b57\u4f53\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u8bbe\u7f6e\u5b57\u4f53\u4e4b\u524d\uff0c\u9700\u8981\u627e\u5230Times New Roman\u5b57\u4f53\u6587\u4ef6\u7684\u8def\u5f84\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5b57\u4f53\u6587\u4ef6\u7684\u8def\u5f84\u53ef\u80fd\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5728Windows\u7cfb\u7edf\u4e2d<\/h3>\n<\/p>\n<p><p>Times New Roman\u5b57\u4f53\u6587\u4ef6\u901a\u5e38\u4f4d\u4e8e\u4ee5\u4e0b\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code>C:\\Windows\\Fonts\\Times New Roman.ttf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5728macOS\u7cfb\u7edf\u4e2d<\/h3>\n<\/p>\n<p><p>Times New Roman\u5b57\u4f53\u6587\u4ef6\u901a\u5e38\u4f4d\u4e8e\u4ee5\u4e0b\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code>\/Library\/Fonts\/Times New Roman.ttf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5728Linux\u7cfb\u7edf\u4e2d<\/h3>\n<\/p>\n<p><p>Times New Roman\u5b57\u4f53\u6587\u4ef6\u53ef\u80fd\u9700\u8981\u624b\u52a8\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install ttf-mscorefonts-installer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u540e\uff0c\u5b57\u4f53\u6587\u4ef6\u901a\u5e38\u4f4d\u4e8e\u4ee5\u4e0b\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code>\/usr\/share\/fonts\/truetype\/msttcorefonts\/Times_New_Roman.ttf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u52a0\u8f7dTimes New Roman\u5b57\u4f53<\/h2>\n<\/p>\n<p><p>\u627e\u5230\u5b57\u4f53\u6587\u4ef6\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5176\u52a0\u8f7d\u5230Matplotlib\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684\u5b57\u4f53\u7ba1\u7406\u5668\uff08font_manager\uff09\u6765\u52a0\u8f7d\u5b57\u4f53\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u52a0\u8f7d\u5b57\u4f53\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6307\u5b9a\u5b57\u4f53\u6587\u4ef6\u8def\u5f84<\/p>\n<p>font_path = &#39;\/usr\/share\/fonts\/truetype\/msttcorefonts\/Times_New_Roman.ttf&#39;  # \u8bf7\u6839\u636e\u5b9e\u9645\u8def\u5f84\u8fdb\u884c\u4fee\u6539<\/p>\n<h2><strong>\u521b\u5efa\u5b57\u4f53\u5bf9\u8c61<\/strong><\/h2>\n<p>prop = fm.FontProperties(fname=font_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u5728\u6240\u6709\u7ed8\u56fe\u4e2d\u90fd\u4f7f\u7528Times New Roman\u5b57\u4f53\uff0c\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7684rcParams\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.rcParams[&#39;font.family&#39;] = prop.get_name()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5728\u7ed8\u56fe\u65f6\u6307\u5b9a\u5b57\u4f53<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u5e0c\u671b\u5728\u7279\u5b9a\u7ed8\u56fe\u4e2d\u4f7f\u7528Times New Roman\u5b57\u4f53\uff0c\u53ef\u4ee5\u5728\u7ed8\u56fe\u65f6\u6307\u5b9a\u5b57\u4f53\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p>ax.set_title(&#39;Sample Plot&#39;, fontproperties=prop)<\/p>\n<p>ax.set_xlabel(&#39;X-axis&#39;, fontproperties=prop)<\/p>\n<p>ax.set_ylabel(&#39;Y-axis&#39;, fontproperties=prop)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u7ed8\u5236\u793a\u4f8b\u56fe\u8868<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Times New Roman\u5b57\u4f53\u7ed8\u5236\u56fe\u8868\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<p>import numpy as np<\/p>\n<h2><strong>\u6307\u5b9a\u5b57\u4f53\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>font_path = &#39;\/usr\/share\/fonts\/truetype\/msttcorefonts\/Times_New_Roman.ttf&#39;  # \u8bf7\u6839\u636e\u5b9e\u9645\u8def\u5f84\u8fdb\u884c\u4fee\u6539<\/p>\n<h2><strong>\u521b\u5efa\u5b57\u4f53\u5bf9\u8c61<\/strong><\/h2>\n<p>prop = fm.FontProperties(fname=font_path)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027<\/strong><\/h2>\n<p>plt.rcParams[&#39;font.family&#39;] = prop.get_name()<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\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\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6807\u9898\u548c\u8f74\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_title(&#39;Sample Plot&#39;, fontproperties=prop)<\/p>\n<p>ax.set_xlabel(&#39;X-axis&#39;, fontproperties=prop)<\/p>\n<p>ax.set_ylabel(&#39;Y-axis&#39;, fontproperties=prop)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4fdd\u5b58\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u7ed8\u5236\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u5b57\u4f53\u8bbe\u7f6e\u4f9d\u7136\u751f\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>fig.savefig(&#39;sample_plot.png&#39;, dpi=300, bbox_inches=&#39;tight&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><h3>1\u3001\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u6b63\u786e<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Times New Roman\u5b57\u4f53\u65f6\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u6b63\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u5b57\u4f53\u6587\u4ef6\u5939\u6765\u627e\u5230\u6b63\u786e\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5b57\u4f53\u52a0\u8f7d\u548c\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7Matplotlib\u7684\u5b57\u4f53\u7ba1\u7406\u5668\u52a0\u8f7d\u5b57\u4f53\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027\u6216\u5728\u7ed8\u56fe\u65f6\u6307\u5b9a\u5b57\u4f53\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u5728\u663e\u793a\u6216\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u786e\u4fdd\u5b57\u4f53\u8bbe\u7f6e\u751f\u6548\uff0c\u4ee5\u786e\u4fdd\u56fe\u8868\u7684\u7f8e\u89c2\u5ea6\u548c\u4e13\u4e1a\u6027\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u5176\u4ed6\u5b57\u4f53\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Times New Roman\u5b57\u4f53\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5b57\u4f53\u6765\u63d0\u5347\u56fe\u8868\u7684\u7f8e\u89c2\u5ea6\u548c\u53ef\u8bfb\u6027\u3002\u901a\u8fc7Matplotlib\u7684\u5b57\u4f53\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u52a0\u8f7d\u548c\u8bbe\u7f6e\u5404\u79cd\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><h3>5\u3001\u5b57\u4f53\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u548c\u5b57\u4f53\u540d\u79f0\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u5728\u8de8\u5e73\u53f0\u4f7f\u7528\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5b57\u4f53\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>6\u3001\u5b57\u4f53\u5927\u5c0f\u548c\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u8bbe\u7f6e\u5b57\u4f53\u65f6\uff0c\u8fd8\u53ef\u4ee5\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u548c\u6837\u5f0f\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u7ed8\u56fe\u9700\u6c42\u3002\u901a\u8fc7Matplotlib\u7684\u5b57\u4f53\u5c5e\u6027\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u3001\u7c97\u7ec6\u3001\u659c\u4f53\u7b49\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5176\u4ed6\u7ed8\u56fe\u5e93\u5b57\u4f53\u8bbe\u7f6e<\/h2>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\u5916\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u7ed8\u56fe\u5e93\uff0c\u5982Seaborn\u3001Plotly\u7b49\uff0c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u5b57\u4f53\u8bbe\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7ed8\u56fe\u5e93\u7684\u5b57\u4f53\u8bbe\u7f6e\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1\u3001Seaborn<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u7528\u7684\u7ed8\u56fe\u63a5\u53e3\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eMatplotlib\u7684\u5168\u5c40\u5b57\u4f53\u5c5e\u6027\u6765\u5f71\u54cdSeaborn\u7684\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027<\/strong><\/h2>\n<p>plt.rcParams[&#39;font.family&#39;] = prop.get_name()<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randn(100)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.histplot(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001Plotly<\/h3>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u652f\u6301\u591a\u79cd\u4ea4\u4e92\u529f\u80fd\u548c\u56fe\u8868\u7c7b\u578b\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6ePlotly\u7684\u5b57\u4f53\u5c5e\u6027\u6765\u4f7f\u7528Times New Roman\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = go.Figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13]))<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027<\/strong><\/h2>\n<p>fig.update_layout(<\/p>\n<p>    title=&#39;Sample Plot&#39;,<\/p>\n<p>    font=dict(<\/p>\n<p>        family=&#39;Times New Roman, serif&#39;,<\/p>\n<p>        size=18,<\/p>\n<p>        color=&#39;black&#39;<\/p>\n<p>    )<\/p>\n<p>)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001\u627e\u4e0d\u5230\u5b57\u4f53\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u53ef\u80fd\u4e0d\u540c\u3002\u5982\u679c\u627e\u4e0d\u5230\u5b57\u4f53\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u5b57\u4f53\u6587\u4ef6\u5939\u6765\u627e\u5230\u6b63\u786e\u7684\u8def\u5f84\uff0c\u6216\u624b\u52a8\u5b89\u88c5\u6240\u9700\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5b57\u4f53\u8bbe\u7f6e\u65e0\u6548<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5728\u7ed8\u56fe\u65f6\u5b57\u4f53\u8bbe\u7f6e\u65e0\u6548\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u5b57\u4f53\u6587\u4ef6\u52a0\u8f7d\u5931\u8d25\u6216\u5b57\u4f53\u5c5e\u6027\u8bbe\u7f6e\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u548c\u5b57\u4f53\u5c5e\u6027\u8bbe\u7f6e\u6765\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u8de8\u5e73\u53f0\u5b57\u4f53\u517c\u5bb9\u6027\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u8de8\u5e73\u53f0\u4f7f\u7528\u65f6\uff0c\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u548c\u5b57\u4f53\u540d\u79f0\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u6765\u8bbe\u7f6e\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u7684\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u548c\u5b57\u4f53\u5c5e\u6027\uff0c\u786e\u4fdd\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>if os.name == &#39;nt&#39;:  # Windows<\/p>\n<p>    font_path = &#39;C:\\\\Windows\\\\Fonts\\\\Times New Roman.ttf&#39;<\/p>\n<p>elif os.name == &#39;posix&#39;:  # macOS or Linux<\/p>\n<p>    font_path = &#39;\/Library\/Fonts\/Times New Roman.ttf&#39;  # macOS<\/p>\n<p>    if not os.path.exists(font_path):<\/p>\n<p>        font_path = &#39;\/usr\/share\/fonts\/truetype\/msttcorefonts\/Times_New_Roman.ttf&#39;  # Linux<\/p>\n<p>prop = fm.FontProperties(fname=font_path)<\/p>\n<p>plt.rcParams[&#39;font.family&#39;] = prop.get_name()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u5b57\u4f53\u5927\u5c0f\u548c\u6837\u5f0f\u8c03\u6574<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u548c\u6837\u5f0f\u6765\u63d0\u5347\u56fe\u8868\u7684\u7f8e\u89c2\u5ea6\u548c\u53ef\u8bfb\u6027\u3002\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7684\u5b57\u4f53\u5c5e\u6027\u6765\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u3001\u7c97\u7ec6\u3001\u659c\u4f53\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u6807\u9898\u548c\u8f74\u6807\u7b7e\u5b57\u4f53\u5c5e\u6027<\/p>\n<p>ax.set_title(&#39;Sample Plot&#39;, fontproperties=prop, fontsize=16, fontweight=&#39;bold&#39;, fontstyle=&#39;italic&#39;)<\/p>\n<p>ax.set_xlabel(&#39;X-axis&#39;, fontproperties=prop, fontsize=14)<\/p>\n<p>ax.set_ylabel(&#39;Y-axis&#39;, fontproperties=prop, fontsize=14)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86\u5982\u4f55\u5728Python\u7ed8\u56fe\u4e2d\u4f7f\u7528Times New Roman\u5b57\u4f53\u3002\u4e3b\u8981\u5185\u5bb9\u5305\u62ec\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u627e\u5230\u5b57\u4f53\u6587\u4ef6\u3001\u52a0\u8f7d\u5b57\u4f53\u3001\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u3001\u7ed8\u5236\u793a\u4f8b\u56fe\u8868\u3001\u4fdd\u5b58\u56fe\u50cf\uff0c\u4ee5\u53ca\u5176\u4ed6\u7ed8\u56fe\u5e93\u7684\u5b57\u4f53\u8bbe\u7f6e\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5\u548c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u95ee\u9898\u7684\u5904\u7406\u65b9\u6cd5\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u5728Python\u7ed8\u56fe\u4e2d\u66f4\u597d\u5730\u4f7f\u7528Times New Roman\u5b57\u4f53\uff0c\u63d0\u5347\u56fe\u8868\u7684\u7f8e\u89c2\u5ea6\u548c\u4e13\u4e1a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7ed8\u56fe\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u4e3a\u65b0\u7f57\u9a6c\uff1f<\/strong><br \/>\u8981\u5728Python\u7ed8\u56fe\u4e2d\u4f7f\u7528\u65b0\u7f57\u9a6c\u5b57\u4f53\uff0c\u60a8\u9700\u8981\u5728\u7ed8\u56fe\u65f6\u6307\u5b9a\u5b57\u4f53\u5c5e\u6027\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7Matplotlib\u5e93\u8f7b\u677e\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u5728\u521b\u5efa\u56fe\u5f62\u65f6\u4f7f\u7528<code>fontdict<\/code>\u53c2\u6570\uff0c\u6216\u5728\u5168\u5c40\u8bbe\u7f6e\u4e2d\u66f4\u6539\u5b57\u4f53\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nplt.rcParams[&#39;font.family&#39;] = &#39;Times New Roman&#39;  # \u5168\u5c40\u8bbe\u7f6e\nplt.plot([1, 2, 3], [1, 4, 9])\nplt.title(&#39;\u793a\u4f8b\u56fe\u6807\u9898&#39;, fontdict={&#39;fontsize&#39;: 14, &#39;fontweight&#39;: &#39;bold&#39;})\nplt.show()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u73af\u5883\u4e2d\u5df2\u5b89\u88c5\u65b0\u7f57\u9a6c\u5b57\u4f53\uff0c\u6216\u8005\u4f7f\u7528\u652f\u6301\u7684\u5b57\u4f53\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u7ed8\u56fe\u65f6\uff0c\u5982\u4f55\u68c0\u67e5\u5df2\u5b89\u88c5\u7684\u5b57\u4f53\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>matplotlib.font_manager<\/code>\u6a21\u5757\u6765\u68c0\u67e5\u5df2\u5b89\u88c5\u7684\u5b57\u4f53\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.font_manager\n\n# \u83b7\u53d6\u6240\u6709\u53ef\u7528\u5b57\u4f53\nfonts = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext=&#39;ttf&#39;)\nfor font in fonts:\n    if &#39;Times New Roman&#39; in font:\n        print(font)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u5217\u51fa\u7cfb\u7edf\u4e2d\u6240\u6709\u4e0e\u65b0\u7f57\u9a6c\u5b57\u4f53\u76f8\u5173\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u7ed8\u56fe\u4e2d\u66f4\u6539\u5b57\u4f53\u5927\u5c0f\u548c\u6837\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u7ed8\u56fe\u51fd\u6570\u4e2d\u8bbe\u7f6e<code>fontsize<\/code>\u548c<code>fontweight<\/code>\u7b49\u53c2\u6570\u6765\u66f4\u6539\u5b57\u4f53\u5927\u5c0f\u548c\u6837\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">plt.plot([1, 2, 3], [1, 4, 9])\nplt.title(&#39;\u81ea\u5b9a\u4e49\u6807\u9898&#39;, fontsize=16, fontweight=&#39;bold&#39;, fontname=&#39;Times New Roman&#39;)\nplt.xlabel(&#39;X\u8f74\u6807\u7b7e&#39;, fontsize=12, fontname=&#39;Times New Roman&#39;)\nplt.ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;, fontsize=12, fontname=&#39;Times New Roman&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u540c\u65f6\u8c03\u6574\u6807\u9898\u548c\u5750\u6807\u8f74\u6807\u7b7e\u7684\u5b57\u4f53\u6837\u5f0f\u548c\u5927\u5c0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u56fe\u65f6\uff0c\u8bbe\u7f6e\u5b57\u4f53\u4e3a\u65b0\u7f57\u9a6c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Matplotlib\u3001\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u3001\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u6b63 [&hellip;]","protected":false},"author":3,"featured_media":1168982,"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\/1168973"}],"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=1168973"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168973\/revisions"}],"predecessor-version":[{"id":1168985,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168973\/revisions\/1168985"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168982"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}