{"id":1039054,"date":"2024-12-31T12:29:17","date_gmt":"2024-12-31T04:29:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1039054.html"},"modified":"2024-12-31T12:29:19","modified_gmt":"2024-12-31T04:29:19","slug":"python3-7-%e7%94%bb%e5%9b%be%e5%a6%82%e4%bd%95%e8%b7%b3%e5%87%ba%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1039054.html","title":{"rendered":"python3.7 \u753b\u56fe\u5982\u4f55\u8df3\u51fa\u6765"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/15ba8f00-3a22-4170-8de8-2137987c0909.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python3.7 \u753b\u56fe\u5982\u4f55\u8df3\u51fa\u6765\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python 3.7\u8fdb\u884c\u7ed8\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u4f7f\u56fe\u5f62\u7a97\u53e3\u5f39\u51fa\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u4f7f\u7528Tkinter\u5e93\u3001\u4f7f\u7528PyQt\u5e93\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93<\/strong><\/p>\n<\/p>\n<p><p>matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684\u63a5\u53e3\uff0c\u4f7f\u5f97\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528matplotlib\u5e93\u7ed8\u56fe\u5e76\u5f39\u51fa\u56fe\u5f62\u7a97\u53e3\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5matplotlib\u5e93<\/li>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/li>\n<li>\u521b\u5efa\u6570\u636e<\/li>\n<li>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/li>\n<li>\u7ed8\u5236\u56fe\u5f62<\/li>\n<li>\u663e\u793a\u56fe\u5f62\u7a97\u53e3<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5matplotlib\u5e93<\/p>\n<p>pip install matplotlib<\/p>\n<h2><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/strong><\/h2>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62\u7a97\u53e3<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528Tkinter\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u901a\u8fc7\u5c06Tkinter\u4e0ematplotlib\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5728GUI\u7a97\u53e3\u4e2d\u5d4c\u5165\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Tkinter\u5e93\u7ed8\u56fe\u5e76\u5f39\u51fa\u56fe\u5f62\u7a97\u53e3\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5matplotlib\u5e93<\/li>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/li>\n<li>\u521b\u5efa\u6570\u636e<\/li>\n<li>\u521b\u5efaTkinter\u7a97\u53e3<\/li>\n<li>\u5c06matplotlib\u56fe\u5f62\u5d4c\u5165Tkinter\u7a97\u53e3<\/li>\n<li>\u663e\u793a\u56fe\u5f62\u7a97\u53e3<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5matplotlib\u5e93<\/p>\n<p>pip install matplotlib<\/p>\n<h2><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/strong><\/h2>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg<\/p>\n<p>import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u521b\u5efaTkinter\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Matplotlib in Tkinter&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u5c06matplotlib\u56fe\u5f62\u5d4c\u5165Tkinter\u7a97\u53e3<\/strong><\/h2>\n<p>canvas = FigureCanvasTkAgg(fig, master=root)<\/p>\n<p>canvas.draw()<\/p>\n<p>canvas.get_tk_widget().pack()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62\u7a97\u53e3<\/strong><\/h2>\n<p>tk.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><strong>\u4e09\u3001\u4f7f\u7528PyQt\u5e93<\/strong><\/p>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python GUI\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u901a\u8fc7\u5c06PyQt\u4e0ematplotlib\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5728GUI\u7a97\u53e3\u4e2d\u5d4c\u5165\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyQt\u5e93\u7ed8\u56fe\u5e76\u5f39\u51fa\u56fe\u5f62\u7a97\u53e3\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5PyQt\u548cmatplotlib\u5e93<\/li>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/li>\n<li>\u521b\u5efa\u6570\u636e<\/li>\n<li>\u521b\u5efaPyQt\u7a97\u53e3<\/li>\n<li>\u5c06matplotlib\u56fe\u5f62\u5d4c\u5165PyQt\u7a97\u53e3<\/li>\n<li>\u663e\u793a\u56fe\u5f62\u7a97\u53e3<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5PyQt\u548cmatplotlib\u5e93<\/p>\n<p>pip install PyQt5 matplotlib<\/p>\n<h2><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/strong><\/h2>\n<p>import sys<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u521b\u5efaPyQt\u7a97\u53e3<\/strong><\/h2>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self, *args, kwargs):<\/p>\n<p>        super(MainWindow, self).__init__(*args, kwargs)<\/p>\n<p>        self.setWindowTitle(&quot;Matplotlib in PyQt&quot;)<\/p>\n<p>        # \u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/p>\n<p>        fig, ax = plt.subplots()<\/p>\n<p>        ax.plot(x, y)<\/p>\n<p>        # \u5c06matplotlib\u56fe\u5f62\u5d4c\u5165PyQt\u7a97\u53e3<\/p>\n<p>        canvas = FigureCanvas(fig)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        layout.addWidget(canvas)<\/p>\n<p>        container = QWidget()<\/p>\n<p>        container.setLayout(layout)<\/p>\n<p>        self.setCentralWidget(container)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62\u7a97\u53e3<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528Plotly\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\u548c\u53ef\u89c6\u5316\u3002\u5b83\u53ef\u4ee5\u5728Web\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\u56fe\u5f62\uff0c\u5e76\u652f\u6301\u591a\u79cd\u4ea4\u4e92\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Plotly\u5e93\u7ed8\u56fe\u5e76\u5f39\u51fa\u56fe\u5f62\u7a97\u53e3\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Plotly\u5e93<\/li>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/li>\n<li>\u521b\u5efa\u6570\u636e<\/li>\n<li>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/li>\n<li>\u663e\u793a\u56fe\u5f62<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5Plotly\u5e93<\/p>\n<p>pip install plotly<\/p>\n<h2><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/strong><\/h2>\n<p>import plotly.graph_objects as go<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = go.Figure(data=go.Scatter(x=x, y=y, mode=&#39;lines&#39;))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u4f7f\u7528Bokeh\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Bokeh\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u7684\u56fe\u5f62\u3002\u5b83\u53ef\u4ee5\u5728Web\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\u56fe\u5f62\uff0c\u5e76\u652f\u6301\u591a\u79cd\u4ea4\u4e92\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Bokeh\u5e93\u7ed8\u56fe\u5e76\u5f39\u51fa\u56fe\u5f62\u7a97\u53e3\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Bokeh\u5e93<\/li>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/li>\n<li>\u521b\u5efa\u6570\u636e<\/li>\n<li>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/li>\n<li>\u663e\u793a\u56fe\u5f62<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5Bokeh\u5e93<\/p>\n<p>pip install bokeh<\/p>\n<h2><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/strong><\/h2>\n<p>from bokeh.plotting import figure, show<\/p>\n<p>from bokeh.io import output_notebook<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>p = figure(title=&quot;Bokeh Line Plot&quot;, x_axis_label=&#39;X&#39;, y_axis_label=&#39;Y&#39;)<\/p>\n<p>p.line(x, y, legend_label=&#39;Line&#39;, line_width=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>output_notebook()<\/p>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u4f7f\u7528Python 3.7\u8fdb\u884c\u7ed8\u56fe\u5e76\u5f39\u51fa\u56fe\u5f62\u7a97\u53e3\u7684\u51e0\u79cd\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528matplotlib\u3001Tkinter\u3001PyQt\u3001Plotly\u548cBokeh\u7b49\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u5e76\u663e\u793a\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7ed8\u56fe\u6548\u7387\u548c\u56fe\u5f62\u7684\u4ea4\u4e92\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 3.7\u4e2d\u4f7f\u7528matplotlib\u7ed8\u5236\u56fe\u5f62\u5e76\u5173\u95ed\u7a97\u53e3\uff1f<\/strong><br \/>\u5728Python 3.7\u4e2d\uff0c\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u56fe\u5f62\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>plt.show()<\/code>\u547d\u4ee4\u663e\u793a\u56fe\u5f62\u7a97\u53e3\uff0c\u540c\u65f6\u53ef\u4ee5\u901a\u8fc7\u70b9\u51fb\u7a97\u53e3\u7684\u5173\u95ed\u6309\u94ae\u6765\u624b\u52a8\u5173\u95ed\u56fe\u5f62\u3002\u5982\u679c\u5e0c\u671b\u5728\u4ee3\u7801\u4e2d\u81ea\u52a8\u5173\u95ed\u56fe\u5f62\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.close()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5728Python 3.7\u4e2d\uff0c\u5982\u4f55\u5728\u7ed8\u56fe\u65f6\u907f\u514d\u963b\u585e\u7a0b\u5e8f\u8fd0\u884c\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u7ed8\u56fe\u65f6\u4e0d\u963b\u585e\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.show(block=False)<\/code>\u3002\u8fd9\u6837\uff0c\u56fe\u5f62\u4f1a\u5728\u540e\u53f0\u6253\u5f00\uff0c\u60a8\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801\uff0c\u800c\u4e0d\u5fc5\u7b49\u5f85\u56fe\u5f62\u7a97\u53e3\u5173\u95ed\u3002<\/p>\n<p><strong>\u4f7f\u7528Python 3.7\u7ed8\u56fe\u65f6\uff0c\u5982\u4f55\u8bbe\u7f6e\u56fe\u5f62\u7684\u4fdd\u5b58\u8def\u5f84\uff1f<\/strong><br \/>\u5728\u4f7f\u7528matplotlib\u7ed8\u56fe\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>plt.savefig(&#39;\u8def\u5f84\/\u6587\u4ef6\u540d.png&#39;)<\/code>\u6765\u4fdd\u5b58\u56fe\u5f62\u3002\u786e\u4fdd\u6307\u5b9a\u7684\u8def\u5f84\u5b58\u5728\uff0c\u5e76\u4e14\u6587\u4ef6\u540d\u4e2d\u5305\u542b\u5408\u9002\u7684\u6269\u5c55\u540d\uff08\u5982.png\u3001.jpg\u7b49\uff09\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u7ed8\u5236\u7684\u56fe\u5f62\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python 3.7\u8fdb\u884c\u7ed8\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u4f7f\u56fe\u5f62\u7a97\u53e3\u5f39\u51fa\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u4f7f\u7528Tki [&hellip;]","protected":false},"author":3,"featured_media":1039065,"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\/1039054"}],"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=1039054"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039054\/revisions"}],"predecessor-version":[{"id":1039069,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039054\/revisions\/1039069"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039065"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1039054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1039054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1039054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}