{"id":1156919,"date":"2025-01-13T18:22:04","date_gmt":"2025-01-13T10:22:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156919.html"},"modified":"2025-01-13T18:22:07","modified_gmt":"2025-01-13T10:22:07","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%ad%97%e4%bd%93%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1156919.html","title":{"rendered":"Python\u5982\u4f55\u8bbe\u7f6e\u5b57\u4f53\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195245\/05b06b0b-c3d2-4e5a-a055-40c09710520c.webp\" alt=\"Python\u5982\u4f55\u8bbe\u7f6e\u5b57\u4f53\u4ee3\u7801\" \/><\/p>\n<p><p> Python\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u7684\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c<strong>\u5176\u4e2d\u8f83\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Tkinter\u3001Pygame\u548cMatplotlib<\/strong>\u3002\u8fd9\u4e09\u79cd\u65b9\u6cd5\u5206\u522b\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528Tkinter\u6765\u8bbe\u7f6e\u5b57\u4f53\uff0c\u5e76\u7ed9\u51fa\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Tkinter\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\u3002\u901a\u8fc7Tkinter\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u521b\u5efa\u56fe\u5f62\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u4e14\u53ef\u4ee5\u5bf9\u754c\u9762\u4e2d\u7684\u5404\u79cd\u5143\u7d20\u8fdb\u884c\u81ea\u5b9a\u4e49\uff0c\u5305\u62ec\u8bbe\u7f6e\u5b57\u4f53\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Tkinter\u7684<code>font<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import font<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Python Font Example&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b57\u4f53<\/strong><\/h2>\n<p>custom_font = font.Font(family=&quot;Helvetica&quot;, size=12, weight=&quot;bold&quot;, slant=&quot;italic&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6807\u7b7e\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;Hello, Tkinter!&quot;, font=custom_font)<\/p>\n<p>label.pack()<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Tkinter\u6a21\u5757\u53ca\u5176font\u5b50\u6a21\u5757\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aTkinter\u7a97\u53e3\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b57\u4f53\uff08Helvetica\uff0c12\u53f7\uff0c\u7c97\u4f53\uff0c\u659c\u4f53\uff09\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6807\u7b7e\uff0c\u5e76\u5c06\u5176\u5b57\u4f53\u8bbe\u7f6e\u4e3a\u6211\u4eec\u5b9a\u4e49\u7684\u81ea\u5b9a\u4e49\u5b57\u4f53\u3002\u6700\u540e\uff0c\u8fdb\u5165Tkinter\u7684\u4e3b\u5faa\u73af\uff0c\u4f7f\u5f97\u7a97\u53e3\u4fdd\u6301\u663e\u793a\u72b6\u6001\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001Tkinter\u4e2d\u7684\u5b57\u4f53\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u5b57\u4f53\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u5728Tkinter\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>font.Font<\/code>\u7c7b\u6765\u8bbe\u7f6e\u5b57\u4f53\u7684\u5404\u79cd\u5c5e\u6027\uff0c\u5305\u62ec\u5b57\u4f53\u65cf\uff08family\uff09\u3001\u5927\u5c0f\uff08size\uff09\u3001\u6837\u5f0f\uff08weight\u3001slant\uff09\u7b49\u3002\u8fd9\u91cc\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>family<\/strong>\uff1a\u5b57\u4f53\u65cf\uff0c\u5982&quot;Helvetica&quot;\u3001&quot;Arial&quot;\u7b49\u3002<\/li>\n<li><strong>size<\/strong>\uff1a\u5b57\u4f53\u5927\u5c0f\uff0c\u53ef\u4ee5\u662f\u6b63\u6574\u6570\uff08\u8868\u793a\u70b9\u6570\uff09\u6216\u8d1f\u6574\u6570\uff08\u8868\u793a\u50cf\u7d20\uff09\u3002<\/li>\n<li><strong>weight<\/strong>\uff1a\u5b57\u4f53\u91cd\u91cf\uff0c\u53ef\u4ee5\u662f&quot;normal&quot;\u6216&quot;bold&quot;\u3002<\/li>\n<li><strong>slant<\/strong>\uff1a\u5b57\u4f53\u6837\u5f0f\uff0c\u53ef\u4ee5\u662f&quot;roman&quot;\u6216&quot;italic&quot;\u3002<\/li>\n<li><strong>underline<\/strong>\uff1a\u662f\u5426\u52a0\u4e0b\u5212\u7ebf\uff0c\u5e03\u5c14\u503c\u3002<\/li>\n<li><strong>overstrike<\/strong>\uff1a\u662f\u5426\u52a0\u5220\u9664\u7ebf\uff0c\u5e03\u5c14\u503c\u3002<\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u8bbe\u7f6e\u8fd9\u4e9b\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import font<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Detailed Font Example&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u79cd\u81ea\u5b9a\u4e49\u5b57\u4f53<\/strong><\/h2>\n<p>font1 = font.Font(family=&quot;Arial&quot;, size=14, weight=&quot;bold&quot;, slant=&quot;italic&quot;)<\/p>\n<p>font2 = font.Font(family=&quot;Times New Roman&quot;, size=16, underline=True)<\/p>\n<p>font3 = font.Font(family=&quot;Courier&quot;, size=-20, overstrike=True)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u5e76\u5e94\u7528\u4e0d\u540c\u7684\u5b57\u4f53<\/strong><\/h2>\n<p>label1 = tk.Label(root, text=&quot;Bold Italic Arial&quot;, font=font1)<\/p>\n<p>label1.pack()<\/p>\n<p>label2 = tk.Label(root, text=&quot;Underlined Times New Roman&quot;, font=font2)<\/p>\n<p>label2.pack()<\/p>\n<p>label3 = tk.Label(root, text=&quot;Strikethrough Courier&quot;, font=font3)<\/p>\n<p>label3.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u79cd\u4e0d\u540c\u7684\u5b57\u4f53\uff0c\u5e76\u5c06\u5b83\u4eec\u5206\u522b\u5e94\u7528\u5230\u4e09\u4e2a\u6807\u7b7e\u4e0a\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5404\u79cd\u5b57\u4f53\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u52a8\u6001\u4fee\u6539\u5b57\u4f53<\/h4>\n<\/p>\n<p><p>\u5728Tkinter\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u52a8\u6001\u4fee\u6539\u5b57\u4f53\u3002\u901a\u8fc7\u4fee\u6539<code>font.Font<\/code>\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u5b9e\u65f6\u66f4\u65b0\u754c\u9762\u5143\u7d20\u7684\u5b57\u4f53\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u52a8\u6001\u4fee\u6539\u5b57\u4f53\u5927\u5c0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import font<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Dynamic Font Example&quot;)<\/p>\n<p>def increase_font_size():<\/p>\n<p>    current_size = custom_font.cget(&quot;size&quot;)<\/p>\n<p>    custom_font.configure(size=current_size + 2)<\/p>\n<p>def decrease_font_size():<\/p>\n<p>    current_size = custom_font.cget(&quot;size&quot;)<\/p>\n<p>    custom_font.configure(size=current_size - 2)<\/p>\n<p>custom_font = font.Font(family=&quot;Helvetica&quot;, size=12)<\/p>\n<p>label = tk.Label(root, text=&quot;Dynamic Font Size&quot;, font=custom_font)<\/p>\n<p>label.pack()<\/p>\n<p>increase_button = tk.Button(root, text=&quot;Increase Font Size&quot;, command=increase_font_size)<\/p>\n<p>increase_button.pack()<\/p>\n<p>decrease_button = tk.Button(root, text=&quot;Decrease Font Size&quot;, command=decrease_font_size)<\/p>\n<p>decrease_button.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u6309\u94ae\uff0c\u7528\u4e8e\u589e\u52a0\u548c\u51cf\u5c11\u5b57\u4f53\u5927\u5c0f\u3002\u901a\u8fc7\u8c03\u7528<code>custom_font.cget(&quot;size&quot;)<\/code>\u83b7\u53d6\u5f53\u524d\u5b57\u4f53\u5927\u5c0f\uff0c\u5e76\u4f7f\u7528<code>custom_font.configure(size=new_size)<\/code>\u66f4\u65b0\u5b57\u4f53\u5927\u5c0f\uff0c\u4ece\u800c\u5b9e\u73b0\u52a8\u6001\u4fee\u6539\u5b57\u4f53\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e8c\u3001Pygame\u4e2d\u7684\u5b57\u4f53\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>Pygame\u662f\u4e00\u4e2a\u7528\u4e8e\u5f00\u53d12D\u6e38\u620f\u7684Python\u5e93\u3002\u5728Pygame\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>pygame.font<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u5b57\u4f53\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Pygame\u4e2d\u8bbe\u7f6e\u548c\u663e\u793a\u5b57\u4f53\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u5b57\u4f53\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<h2><strong>\u521d\u59cb\u5316Pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>screen = pygame.display.set_mode((640, 480))<\/p>\n<p>pygame.display.set_caption(&quot;Pygame Font Example&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u5b57\u4f53<\/strong><\/h2>\n<p>font = pygame.font.SysFont(&quot;Arial&quot;, 48)<\/p>\n<h2><strong>\u6e32\u67d3\u6587\u672c<\/strong><\/h2>\n<p>text_surface = font.render(&quot;Hello, Pygame!&quot;, True, (255, 255, 255))<\/p>\n<h2><strong>\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            pygame.quit()<\/p>\n<p>            sys.exit()<\/p>\n<p>    # \u6e05\u5c4f<\/p>\n<p>    screen.fill((0, 0, 0))<\/p>\n<p>    # \u7ed8\u5236\u6587\u672c<\/p>\n<p>    screen.blit(text_surface, (50, 200))<\/p>\n<p>    # \u66f4\u65b0\u663e\u793a<\/p>\n<p>    pygame.display.flip()<\/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\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>pygame.font.SysFont<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u4f53\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>font.render<\/code>\u65b9\u6cd5\u5c06\u6587\u672c\u6e32\u67d3\u4e3a\u4e00\u4e2a\u8868\u9762\u5bf9\u8c61\u3002\u6700\u540e\uff0c\u5728\u4e3b\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u8868\u9762\u5bf9\u8c61\u7ed8\u5236\u5230\u7a97\u53e3\u4e2d\uff0c\u5e76\u4e0d\u65ad\u66f4\u65b0\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u81ea\u5b9a\u4e49\u5b57\u4f53<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u7cfb\u7edf\u5b57\u4f53\u5916\uff0cPygame\u8fd8\u652f\u6301\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>pygame.font.Font<\/code>\u65b9\u6cd5\u52a0\u8f7dTTF\u5b57\u4f53\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<h2><strong>\u521d\u59cb\u5316Pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>screen = pygame.display.set_mode((640, 480))<\/p>\n<p>pygame.display.set_caption(&quot;Pygame Custom Font Example&quot;)<\/p>\n<h2><strong>\u52a0\u8f7d\u81ea\u5b9a\u4e49\u5b57\u4f53<\/strong><\/h2>\n<p>font = pygame.font.Font(&quot;path\/to\/custom_font.ttf&quot;, 48)<\/p>\n<h2><strong>\u6e32\u67d3\u6587\u672c<\/strong><\/h2>\n<p>text_surface = font.render(&quot;Custom Font!&quot;, True, (255, 255, 255))<\/p>\n<h2><strong>\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            pygame.quit()<\/p>\n<p>            sys.exit()<\/p>\n<p>    # \u6e05\u5c4f<\/p>\n<p>    screen.fill((0, 0, 0))<\/p>\n<p>    # \u7ed8\u5236\u6587\u672c<\/p>\n<p>    screen.blit(text_surface, (50, 200))<\/p>\n<p>    # \u66f4\u65b0\u663e\u793a<\/p>\n<p>    pygame.display.flip()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pygame.font.Font<\/code>\u65b9\u6cd5\u52a0\u8f7d\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684TTF\u5b57\u4f53\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u5230\u6587\u672c\u6e32\u67d3\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e09\u3001Matplotlib\u4e2d\u7684\u5b57\u4f53\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u7684Python\u5e93\u3002\u5728Matplotlib\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u5b57\u4f53\uff0c\u5305\u62ec\u5728\u5168\u5c40\u914d\u7f6e\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u4ee5\u53ca\u5728\u5177\u4f53\u7ed8\u56fe\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5168\u5c40\u5b57\u4f53\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539Matplotlib\u7684rc\u53c2\u6570\u6765\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027\u3002\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u8bbe\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539Matplotlib\u7684\u914d\u7f6e\u6587\u4ef6<code>matplotlibrc<\/code>\u6765\u8bbe\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u811a\u672c\u4e2d\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027<\/strong><\/h2>\n<p>plt.rcParams[&quot;font.family&quot;] = &quot;serif&quot;<\/p>\n<p>plt.rcParams[&quot;font.serif&quot;] = [&quot;Times New Roman&quot;]<\/p>\n<p>plt.rcParams[&quot;font.size&quot;] = 14<\/p>\n<p>plt.rcParams[&quot;axes.titlesize&quot;] = 18<\/p>\n<p>plt.rcParams[&quot;axes.labelsize&quot;] = 16<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u8868<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Sample Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u4fee\u6539<code>plt.rcParams<\/code>\u5b57\u5178\u6765\u8bbe\u7f6e\u5168\u5c40\u5b57\u4f53\u5c5e\u6027\u3002\u8fd9\u4e9b\u8bbe\u7f6e\u5c06\u5e94\u7528\u5230\u6240\u6709\u540e\u7eed\u7684\u7ed8\u56fe\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5c40\u90e8\u5b57\u4f53\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5168\u5c40\u8bbe\u7f6e\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u5177\u4f53\u7684\u7ed8\u56fe\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5177\u4f53\u7ed8\u56fe\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u8868<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Sample Plot&quot;, fontsize=18, fontweight=&quot;bold&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;, fontsize=14)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;, fontsize=14)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u672c\u5b57\u4f53<\/strong><\/h2>\n<p>plt.text(2, 10, &quot;Sample Text&quot;, fontsize=12, family=&quot;serif&quot;, style=&quot;italic&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5728<code>plt.title<\/code>\u3001<code>plt.xlabel<\/code>\u3001<code>plt.ylabel<\/code>\u7b49\u65b9\u6cd5\u4e2d\u4f20\u9012\u5b57\u4f53\u5c5e\u6027\u53c2\u6570\u6765\u8bbe\u7f6e\u5b57\u4f53\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u4f7f\u7528<code>plt.text<\/code>\u65b9\u6cd5\u5728\u56fe\u8868\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6587\u672c\uff0c\u5e76\u8bbe\u7f6e\u4e86\u5176\u5b57\u4f53\u5c5e\u6027\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u65b9\u4fbf\u5730\u8bbe\u7f6e\u5b57\u4f53\uff0c\u6ee1\u8db3\u4e0d\u540c\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<strong>\u4f7f\u7528Tkinter\u8bbe\u7f6eGUI\u5e94\u7528\u7a0b\u5e8f\u7684\u5b57\u4f53\u3001\u4f7f\u7528Pygame\u8bbe\u7f6e\u6e38\u620f\u4e2d\u7684\u5b57\u4f53\u3001\u4f7f\u7528Matplotlib\u8bbe\u7f6e\u56fe\u8868\u4e2d\u7684\u5b57\u4f53<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u7279\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8bbe\u7f6e\u5b57\u4f53\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u7a0b\u5e8f\u7684\u7528\u6237\u4f53\u9a8c\u548c\u89c6\u89c9\u6548\u679c\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u4e3a\u4f60\u7684\u9879\u76ee\u589e\u8272\u4e0d\u5c11\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u5b57\u4f53\u4ee5\u7528\u4e8e\u56fe\u5f62\u754c\u9762\u6216\u56fe\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u901a\u5e38\u4f9d\u8d56\u4e8e\u6240\u4f7f\u7528\u7684\u5e93\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>fontdict<\/code>\u53c2\u6570\u6216<code>rcParams<\/code>\u8bbe\u7f6e\u5b57\u4f53\u3002\u5bf9\u4e8eTkinter\u7b49\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>font<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u5b57\u4f53\u6837\u5f0f\u3001\u5927\u5c0f\u548c\u7c97\u7ec6\u3002\u786e\u4fdd\u5728\u8bbe\u7f6e\u5b57\u4f53\u4e4b\u524d\u5b89\u88c5\u76f8\u5173\u5b57\u4f53\u5e93\uff0c\u4ee5\u4fbf\u80fd\u591f\u4f7f\u7528\u6240\u9700\u7684\u5b57\u4f53\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Matplotlib\u4e2d\u66f4\u6539\u9ed8\u8ba4\u5b57\u4f53\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>rcParams<\/code>\u5b57\u5178\u6765\u66f4\u6539\u9ed8\u8ba4\u5b57\u4f53\u3002\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u5f00\u5934\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nplt.rcParams[&#39;font.family&#39;] = &#39;YourFontName&#39;\n<\/code><\/pre>\n<p>\u66ff\u6362<code>YourFontName<\/code>\u4e3a\u6240\u9700\u7684\u5b57\u4f53\u540d\u79f0\u3002\u8fd9\u4e00\u8bbe\u7f6e\u5c06\u5f71\u54cd\u6240\u6709\u540e\u7eed\u751f\u6210\u7684\u56fe\u8868\uff0c\u4f7f\u5176\u4f7f\u7528\u6307\u5b9a\u7684\u5b57\u4f53\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u53ef\u4ee5\u5c06\u81ea\u5b9a\u4e49\u5b57\u4f53\u6587\u4ef6\uff08\u5982.ttf\u6216.otf\u683c\u5f0f\uff09\u653e\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u5e93\u52a0\u8f7d\u5b83\u3002\u4f8b\u5982\uff0c\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>FontProperties<\/code>\u7c7b\u6765\u52a0\u8f7d\u81ea\u5b9a\u4e49\u5b57\u4f53\uff0c\u5e76\u5728\u7ed8\u56fe\u65f6\u5e94\u7528\u3002\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u4ee5\u4fbfPython\u80fd\u591f\u627e\u5230\u5e76\u4f7f\u7528\u8be5\u5b57\u4f53\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u7684\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u8f83\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Tkinter\u3001Pygame\u548cMat [&hellip;]","protected":false},"author":3,"featured_media":1156925,"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\/1156919"}],"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=1156919"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156919\/revisions"}],"predecessor-version":[{"id":1156926,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156919\/revisions\/1156926"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156925"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}