{"id":1053316,"date":"2024-12-31T14:32:50","date_gmt":"2024-12-31T06:32:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053316.html"},"modified":"2024-12-31T14:32:54","modified_gmt":"2024-12-31T06:32:54","slug":"python%e5%a6%82%e4%bd%95%e5%8a%a0%e8%bd%bd%e4%b8%ad%e6%96%87%e5%ad%97%e4%bd%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1053316.html","title":{"rendered":"python\u5982\u4f55\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/05263417-16ed-4263-9c85-548f7f2fc013.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\" \/><\/p>\n<p><p> <strong>Python\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528matplotlib\u3001PIL\u3001pygame\u7b49\u5e93\u6765\u52a0\u8f7d\u548c\u663e\u793a\u4e2d\u6587\u5b57\u4f53\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u5728matplotlib\u4e2d\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u4ee5\u4e0b\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528matplotlib\u6765\u52a0\u8f7d\u548c\u663e\u793a\u4e2d\u6587\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5matplotlib<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528matplotlib\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u8bbe\u7f6ematplotlib\u7684\u4e2d\u6587\u5b57\u4f53\uff1a<\/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>\u627e\u5230\u7cfb\u7edf\u4e2d\u7684\u4e2d\u6587\u5b57\u4f53\u8def\u5f84<\/strong><\/h2>\n<p>zh_font_path = &#39;\/usr\/share\/fonts\/truetype\/wqy\/wqy-microhei.ttc&#39;  # \u6839\u636e\u7cfb\u7edf\u5b9e\u9645\u8def\u5f84\u8c03\u6574<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/h2>\n<p>my_font = fm.FontProperties(fname=zh_font_path)<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u56fe\u8868<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;, fontproperties=my_font)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;, fontproperties=my_font)<\/p>\n<p>plt.title(&#39;\u4e2d\u6587\u6807\u9898&#39;, fontproperties=my_font)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u627e\u5230\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u3002\u5bf9\u4e8eLinux\u7cfb\u7edf\uff0c\u5e38\u89c1\u7684\u4e2d\u6587\u5b57\u4f53\u5982\u6587\u6cc9\u9a7f\u5fae\u7c73\u9ed1\uff08wqy-microhei.ttc\uff09\u901a\u5e38\u5b89\u88c5\u5728<code>\/usr\/share\/fonts\/truetype\/wqy\/<\/code>\u8def\u5f84\u4e0b\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u5b57\u4f53\u8def\u5f84\u3002\u63a5\u7740\uff0c\u4f7f\u7528matplotlib.font_manager\u4e2d\u7684FontProperties\u7c7b\u52a0\u8f7d\u6307\u5b9a\u7684\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\uff0c\u5e76\u5728\u521b\u5efa\u56fe\u8868\u65f6\u901a\u8fc7<code>fontproperties<\/code>\u53c2\u6570\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001PIL\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5PIL<\/strong><\/li>\n<\/ol>\n<p><p>PIL\uff08Pillow\uff09\u662fPython Imaging Library\u7684\u7ee7\u627f\u7248\u672c\uff0c\u53ef\u4ee5\u5904\u7406\u56fe\u50cf\u548c\u6587\u5b57\u7ed8\u5236\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528PIL\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u65f6\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u80cc\u666f\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (200, 100), (255, 255, 255))<\/p>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/h2>\n<p>font = ImageFont.truetype(&#39;\/usr\/share\/fonts\/truetype\/wqy\/wqy-microhei.ttc&#39;, 20)<\/p>\n<h2><strong>\u7ed8\u5236\u6587\u5b57<\/strong><\/h2>\n<p>draw.text((10, 40), &quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;, font=font, fill=(0, 0, 0))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u7684\u56fe\u7247\uff0c\u7136\u540e\u4f7f\u7528ImageDraw\u5bf9\u8c61\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u6587\u5b57\u3002\u901a\u8fc7ImageFont.truetype\u65b9\u6cd5\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\uff0c\u5e76\u5728draw.text\u65b9\u6cd5\u4e2d\u6307\u5b9a\u5b57\u4f53\u548c\u6587\u5b57\u5185\u5bb9\u3002\u6700\u540e\uff0c\u4f7f\u7528image.show\u65b9\u6cd5\u663e\u793a\u751f\u6210\u7684\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001PYGAME\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5pygame<\/strong><\/li>\n<\/ol>\n<p><p>pygame\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u7528\u4e8e\u7f16\u5199\u89c6\u9891\u6e38\u620f\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528pygame\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u65f6\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<h2><strong>\u521d\u59cb\u5316pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>screen = pygame.display.set_mode((400, 300))<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<\/strong><\/h2>\n<p>pygame.display.set_caption(&quot;\u4e2d\u6587\u5b57\u4f53\u663e\u793a&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/h2>\n<p>font = pygame.font.Font(&#39;\/usr\/share\/fonts\/truetype\/wqy\/wqy-microhei.ttc&#39;, 36)<\/p>\n<h2><strong>\u6e32\u67d3\u6587\u5b57<\/strong><\/h2>\n<p>text_surface = font.render(&quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;, True, (0, 0, 0))<\/p>\n<h2><strong>\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>running = True<\/p>\n<p>while running:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            running = False<\/p>\n<p>    # \u7ed8\u5236\u6587\u5b57<\/p>\n<p>    screen.fill((255, 255, 255))<\/p>\n<p>    screen.blit(text_surface, (50, 100))<\/p>\n<p>    # \u66f4\u65b0\u663e\u793a<\/p>\n<p>    pygame.display.flip()<\/p>\n<h2><strong>\u9000\u51fapygame<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e86pygame\u5e76\u8bbe\u7f6e\u4e86\u7a97\u53e3\u5927\u5c0f\u548c\u6807\u9898\u3002\u4f7f\u7528pygame.font.Font\u65b9\u6cd5\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7font.render\u65b9\u6cd5\u6e32\u67d3\u6587\u5b57\u3002\u901a\u8fc7\u4e3b\u5faa\u73af\u4e0d\u65ad\u66f4\u65b0\u663e\u793a\u5185\u5bb9\uff0c\u5e76\u5728\u7a97\u53e3\u4e2d\u663e\u793a\u6e32\u67d3\u7684\u6587\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>Python\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528matplotlib\u3001PIL\u3001pygame\u7b49\u5e93<\/strong>\u3002\u5176\u4e2d\uff0cmatplotlib\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u7684\u65b9\u6cd5\u6700\u4e3a\u5e38\u7528\uff0c\u901a\u8fc7\u8bbe\u7f6efontproperties\u53c2\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4e2d\u6587\u5b57\u4f53\u7684\u663e\u793a\u3002PIL\u548cpygame\u4e5f\u53ef\u4ee5\u7528\u4e8e\u52a0\u8f7d\u548c\u663e\u793a\u4e2d\u6587\u5b57\u4f53\uff0c\u5206\u522b\u9002\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u6e38\u620f\u5f00\u53d1\u573a\u666f\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u627e\u5230\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u5e93\u65b9\u6cd5\u52a0\u8f7d\u548c\u8bbe\u7f6e\u5b57\u4f53\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728Python\u4e2d\u52a0\u8f7d\u548c\u663e\u793a\u4e2d\u6587\u5b57\u4f53\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u4f53\u8fdb\u884c\u6587\u672c\u663e\u793a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u4e2d\u6587\u5b57\u4f53\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u5b9e\u73b0\uff0c\u4f8b\u5982Matplotlib\u548cPillow\u3002\u4ee5Matplotlib\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\u6765\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\uff0c\u786e\u4fdd\u5728\u7ed8\u56fe\u65f6\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u6b63\u786e\u663e\u793a\u3002\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.font_manager<\/code>\u6a21\u5757\u6765\u6307\u5b9a\u5b57\u4f53\u6587\u4ef6\u7684\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u4e2d\u6587\u7684\u6b63\u5e38\u663e\u793a\u3002<\/p>\n<p><strong>\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u65f6\uff0c\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u7684\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e14\u8be5\u5b57\u4f53\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002\u5982\u679c\u5b57\u4f53\u6587\u4ef6\u635f\u574f\u6216\u8005\u4e0d\u652f\u6301\u4e2d\u6587\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6587\u672c\u663e\u793a\u4e3a\u4e71\u7801\u3002\u6b64\u5916\uff0c\u68c0\u67e5Python\u73af\u5883\u548c\u6240\u4f7f\u7528\u7684\u5e93\u7248\u672c\u4e5f\u662f\u5fc5\u8981\u7684\uff0c\u4ee5\u786e\u4fdd\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5982\u4f55\u5904\u7406\u4e2d\u6587\u5b57\u4f53\u52a0\u8f7d\u95ee\u9898\uff1f<\/strong><br \/>\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\uff08\u5982Windows\u3001macOS\u548cLinux\uff09\u5bf9\u5b57\u4f53\u7684\u7ba1\u7406\u6709\u6240\u4e0d\u540c\u3002\u5728Windows\u4e0a\uff0c\u5b57\u4f53\u6587\u4ef6\u901a\u5e38\u5b58\u50a8\u5728C:\\Windows\\Fonts\u76ee\u5f55\u4e2d\uff0c\u800c\u5728macOS\u4e0a\uff0c\u5b57\u4f53\u5219\u4f4d\u4e8e\/Library\/Fonts\u6216~\/Library\/Fonts\u3002Linux\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u5b57\u4f53\u5305\u6765\u83b7\u53d6\u4e2d\u6587\u5b57\u4f53\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u8def\u5f84\u6765\u5f15\u7528\u76f8\u5e94\u7684\u5b57\u4f53\u6587\u4ef6\uff0c\u4ece\u800c\u786e\u4fdd\u8de8\u5e73\u53f0\u7684\u5b57\u4f53\u52a0\u8f7d\u517c\u5bb9\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528matplotlib\u3001PIL\u3001pygame\u7b49\u5e93\u6765\u52a0\u8f7d\u548c\u663e\u793a\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1053326,"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\/1053316"}],"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=1053316"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053316\/revisions"}],"predecessor-version":[{"id":1053328,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053316\/revisions\/1053328"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053326"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}