{"id":961761,"date":"2024-12-27T04:01:49","date_gmt":"2024-12-26T20:01:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/961761.html"},"modified":"2024-12-27T04:01:50","modified_gmt":"2024-12-26T20:01:50","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9%e5%8f%98ticks%e5%ad%97%e4%bd%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/961761.html","title":{"rendered":"python\u5982\u4f55\u6539\u53d8ticks\u5b57\u4f53"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103911\/3e88a1f1-a04f-46a8-96d2-cbce56c41c11.webp\" alt=\"python\u5982\u4f55\u6539\u53d8ticks\u5b57\u4f53\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u6539\u53d8\u56fe\u5f62\u7684\u523b\u5ea6\u5b57\u4f53\u3002\u901a\u8fc7\u8bbe\u7f6e<code>matplotlib.pyplot<\/code>\u6a21\u5757\u4e2d\u7684<code>xticks<\/code>\u548c<code>yticks<\/code>\u7684\u5b57\u4f53\u53c2\u6570\uff0c\u6216\u8005\u4f7f\u7528<code>rcParams<\/code>\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u6837\u5f0f\u548c\u5927\u5c0f\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>fontdict<\/code>\u53c2\u6570\u8bbe\u5b9a\u5b57\u4f53\u5c5e\u6027\u3001\u901a\u8fc7<code>rc<\/code>\u547d\u4ee4\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u3001\u5728\u7ed8\u56fe\u51fd\u6570\u4e2d\u76f4\u63a5\u8c03\u6574\u5b57\u4f53\u3002<\/strong>\u8be6\u7ec6\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>fontdict<\/code>\u53c2\u6570\u8bbe\u5b9a\u5b57\u4f53\u5c5e\u6027<\/p>\n<\/p>\n<p><p>\u5728Matplotlib\u4e2d\uff0c<code>xticks<\/code>\u548c<code>yticks<\/code>\u51fd\u6570\u90fd\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a<code>fontdict<\/code>\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u523b\u5ea6\u6807\u7b7e\u7684\u5b57\u4f53\u5c5e\u6027\u3002\u901a\u8fc7\u8fd9\u4e2a\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u5b57\u4f53\u7684\u7c7b\u578b\u3001\u5927\u5c0f\u3001\u989c\u8272\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<p>plt.xticks(fontsize=12, fontweight=&#39;bold&#39;, fontname=&#39;Times New Roman&#39;)<\/p>\n<p>plt.yticks(fontsize=12, fontweight=&#39;bold&#39;, fontname=&#39;Times New Roman&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>fontsize<\/code>\u6307\u5b9a\u5b57\u4f53\u5927\u5c0f\uff0c<code>fontweight<\/code>\u8bbe\u7f6e\u5b57\u4f53\u7684\u7c97\u7ec6\uff0c\u800c<code>fontname<\/code>\u7528\u6765\u6307\u5b9a\u5b57\u4f53\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7<code>rc<\/code>\u547d\u4ee4\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53<\/p>\n<\/p>\n<p><p>Matplotlib\u63d0\u4f9b\u4e86\u4e00\u79cd\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u7684\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7<code>rc<\/code>\u547d\u4ee4\u3002\u8fd9\u6837\u8bbe\u7f6e\u540e\uff0c\u6240\u6709\u7684\u56fe\u5f62\u90fd\u4f1a\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u4f53\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib as mpl<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>mpl.rc(&#39;xtick&#39;, labelsize=12, color=&#39;r&#39;, weight=&#39;bold&#39;)<\/p>\n<p>mpl.rc(&#39;ytick&#39;, labelsize=12, color=&#39;r&#39;, weight=&#39;bold&#39;)<\/p>\n<p>plt.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5168\u5c40\u5730\u63a7\u5236\u56fe\u5f62\u4e2d\u523b\u5ea6\u6807\u7b7e\u7684\u5b57\u4f53\u6837\u5f0f\uff0c\u8fd9\u5728\u9700\u8981\u7edf\u4e00\u98ce\u683c\u7684\u573a\u5408\u4e0b\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5728\u7ed8\u56fe\u51fd\u6570\u4e2d\u76f4\u63a5\u8c03\u6574\u5b57\u4f53<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>fontdict<\/code>\u548c<code>rc<\/code>\u547d\u4ee4\u5916\uff0c\u8fd8\u6709\u4e00\u79cd\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u5728\u7ed8\u56fe\u51fd\u6570\u4e2d\u4f7f\u7528<code>tick_params<\/code>\u6765\u8c03\u6574\u5b57\u4f53\u3002<code>tick_params<\/code>\u53ef\u4ee5\u63a7\u5236\u523b\u5ea6\u7ebf\u548c\u523b\u5ea6\u6807\u7b7e\u7684\u5916\u89c2\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<p>plt.tick_params(axis=&#39;x&#39;, labelsize=14, labelcolor=&#39;blue&#39;, labelrotation=45)<\/p>\n<p>plt.tick_params(axis=&#39;y&#39;, labelsize=14, labelcolor=&#39;blue&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>tick_params<\/code>\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>axis<\/code>\u53c2\u6570\u6765\u5206\u522b\u8c03\u6574x\u8f74\u548cy\u8f74\u7684\u523b\u5ea6\u5b57\u4f53\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u800c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7efc\u5408\u8fd0\u7528\u591a\u79cd\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f80\u5f80\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u5b57\u4f53\u8bbe\u7f6e\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u5728\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u7684\u57fa\u7840\u4e0a\uff0c\u5bf9\u67d0\u4e2a\u7279\u5b9a\u56fe\u5f62\u8fdb\u884c\u989d\u5916\u7684\u5b9a\u5236\u5316\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5168\u5c40\u8bbe\u7f6e<\/strong><\/h2>\n<p>plt.rc(&#39;xtick&#39;, labelsize=10, labelcolor=&#39;green&#39;)<\/p>\n<p>plt.rc(&#39;ytick&#39;, labelsize=10, labelcolor=&#39;green&#39;)<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<h2><strong>\u5bf9\u67d0\u4e2a\u7279\u5b9a\u56fe\u5f62\u8fdb\u884c\u5b9a\u5236\u5316\u8bbe\u7f6e<\/strong><\/h2>\n<p>ax.tick_params(axis=&#39;x&#39;, labelsize=12, labelcolor=&#39;red&#39;, labelrotation=30)<\/p>\n<p>ax.tick_params(axis=&#39;y&#39;, labelsize=12, labelcolor=&#39;red&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u7efc\u5408\u8fd0\u7528\u7684\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u4fdd\u6301\u6574\u4f53\u98ce\u683c\u7edf\u4e00\u7684\u540c\u65f6\uff0c\u7075\u6d3b\u5730\u5bf9\u5c40\u90e8\u8fdb\u884c\u7ec6\u8282\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u5b57\u4f53\u5e93<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u8bb8\u591a\u5b57\u4f53\u5e93\u53ef\u4ee5\u9009\u62e9\uff0c\u6bd4\u5982<code>DejaVu Sans<\/code>\u3001<code>Times New Roman<\/code>\u3001<code>Arial<\/code>\u7b49\uff0cMatplotlib\u53ef\u4ee5\u901a\u8fc7<code>font_manager<\/code>\u6a21\u5757\u6765\u52a0\u8f7d\u7cfb\u7edf\u4e2d\u7684\u5b57\u4f53\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>\u67e5\u770b\u7cfb\u7edf\u4e2d\u53ef\u7528\u7684\u5b57\u4f53<\/strong><\/h2>\n<p>for font in fm.findSystemFonts(fontpaths=None, fontext=&#39;ttf&#39;):<\/p>\n<p>    print(fm.FontProperties(fname=font).get_name())<\/p>\n<h2><strong>\u4f7f\u7528\u7279\u5b9a\u5b57\u4f53<\/strong><\/h2>\n<p>font_path = &#39;\/path\/to\/font.ttf&#39;  # \u66ff\u6362\u4e3a\u5b9e\u9645\u5b57\u4f53\u8def\u5f84<\/p>\n<p>prop = fm.FontProperties(fname=font_path)<\/p>\n<p>plt.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<p>plt.xticks(fontproperties=prop)<\/p>\n<p>plt.yticks(fontproperties=prop)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u5b57\u4f53\u6587\u4ef6\u6765\u7ed8\u5236Matplotlib\u56fe\u5f62\uff0c\u4ece\u800c\u6ee1\u8db3\u7279\u6b8a\u7684\u5c55\u793a\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u5173\u4e8e\u5982\u4f55\u5728Python\u4e2d\u6539\u53d8Matplotlib\u56fe\u5f62\u7684\u523b\u5ea6\u5b57\u4f53\u7684\u8be6\u7ec6\u4ecb\u7ecd\u3002\u901a\u8fc7\u4f7f\u7528<code>fontdict<\/code>\u3001<code>rc<\/code>\u547d\u4ee4\u3001<code>tick_params<\/code>\u4ee5\u53ca\u52a0\u8f7d\u4e0d\u540c\u7684\u5b57\u4f53\u5e93\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u8c03\u6574\u56fe\u5f62\u7684\u5916\u89c2\u4ee5\u6ee1\u8db3\u5404\u79cd\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u5b9a\u4e49Matplotlib\u56fe\u8868\u7684\u523b\u5ea6\u5b57\u4f53\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u8868\u65f6\uff0c\u60f3\u8981\u6539\u53d8\u523b\u5ea6\u5b57\u4f53\u53ef\u4ee5\u4f7f\u7528<code>tick_params<\/code>\u548c<code>set_xticklabels<\/code>\u3001<code>set_yticklabels<\/code>\u7b49\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u6307\u5b9a\u5b57\u4f53\u7684\u5927\u5c0f\u3001\u98ce\u683c\u548c\u989c\u8272\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.tick_params(axis=&#39;both&#39;, labelsize=14, labelcolor=&#39;red&#39;)<\/code>\u6765\u8c03\u6574x\u548cy\u8f74\u7684\u523b\u5ea6\u6807\u7b7e\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u6539\u53d8\u523b\u5ea6\u6807\u7b7e\u7684\u5b57\u4f53\u7c7b\u578b\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>fontdict<\/code>\u53c2\u6570\u6765\u4fee\u6539\u5b57\u4f53\u7c7b\u578b\u3002\u6bd4\u5982\u5728\u8bbe\u7f6e\u523b\u5ea6\u6807\u7b7e\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u6240\u9700\u7684\u5b57\u4f53\u5c5e\u6027\u3002\u4f8b\u5982\uff0c<code>ax.set_xticklabels([&#39;Label1&#39;, &#39;Label2&#39;], fontdict={&#39;fontsize&#39;: 12, &#39;fontweight&#39;: &#39;bold&#39;, &#39;family&#39;: &#39;serif&#39;})<\/code>\u53ef\u4ee5\u4f7f\u523b\u5ea6\u6807\u7b7e\u4f7f\u7528\u7279\u5b9a\u7684\u5b57\u4f53\u7c7b\u578b\u548c\u6837\u5f0f\u3002<\/p>\n<p><strong>\u5728Jupyter Notebook\u4e2d\u5982\u4f55\u66f4\u6539\u56fe\u5f62\u7684\u523b\u5ea6\u5b57\u4f53\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u4fee\u6539\u523b\u5ea6\u5b57\u4f53\u4e0e\u5728\u5176\u4ed6\u73af\u5883\u4e2d\u76f8\u4f3c\u3002\u7528\u6237\u53ef\u4ee5\u5728\u7ed8\u56fe\u4ee3\u7801\u4e2d\u52a0\u5165\u76f8\u5173\u8bbe\u7f6e\uff0c\u786e\u4fdd\u56fe\u5f62\u663e\u793a\u65f6\u523b\u5ea6\u5b57\u4f53\u5f97\u5230\u76f8\u5e94\u7684\u8c03\u6574\u3002\u53ef\u4ee5\u4f7f\u7528<code>plt.xticks(fontsize=10)<\/code>\u548c<code>plt.yticks(fontsize=10)<\/code>\u6765\u76f4\u63a5\u8c03\u6574\u523b\u5ea6\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n<p><strong>\u662f\u5426\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93\u6765\u66f4\u6539\u5b57\u4f53\uff1f<\/strong><br \/>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4fee\u6539\u523b\u5ea6\u5b57\u4f53\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93\uff0c\u56e0\u4e3aMatplotlib\u81ea\u5e26\u4e86\u5bf9\u5b57\u4f53\u7684\u652f\u6301\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u60f3\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u5b57\u4f53\u6837\u5f0f\u6216\u7279\u6b8a\u5b57\u7b26\uff0c\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u5b57\u4f53\u5e93\u5e76\u8fdb\u884c\u914d\u7f6e\u3002\u4f7f\u7528<code>matplotlib.font_manager<\/code>\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u7ba1\u7406\u548c\u52a0\u8f7d\u4e0d\u540c\u7684\u5b57\u4f53\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u6539\u53d8\u56fe\u5f62\u7684\u523b\u5ea6\u5b57\u4f53\u3002\u901a\u8fc7\u8bbe\u7f6ematplotlib.pyplo [&hellip;]","protected":false},"author":3,"featured_media":961767,"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\/961761"}],"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=961761"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961761\/revisions"}],"predecessor-version":[{"id":961768,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961761\/revisions\/961768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/961767"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=961761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=961761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=961761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}