{"id":173592,"date":"2024-05-08T18:14:33","date_gmt":"2024-05-08T10:14:33","guid":{"rendered":""},"modified":"2024-05-08T18:14:38","modified_gmt":"2024-05-08T10:14:38","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e5%8a%a8%e6%80%81%e5%9b%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173592.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u52a8\u6001\u56fe\u8868"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27045534\/c5a74c46-0834-4f20-ade8-6954b4552ecb.webp\" alt=\"\u5982\u4f55\u7528python\u753b\u52a8\u6001\u56fe\u8868\" \/><\/p>\n<p><p>\u786e\u5b9e\u5730\uff0c\u4f7f\u7528Python\u53ef\u4ee5\u6709\u6548\u5730\u521b\u5efa\u52a8\u6001\u56fe\u8868\uff0c\u672c\u8d28\u4e0a\u662f\u4e00\u7cfb\u5217\u56fe\u8868\u8fde\u7eed\u5c55\u793a\u4ee5\u6a21\u62df\u52a8\u6001\u6548\u679c\u3002\u8fd9\u6837\u7684\u56fe\u8868\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u3001\u8d22\u7ecf\u5e02\u573a\u5206\u6790\u7b49\u9886\u57df\u3002\u5176\u5b9e\u73b0\u7684\u57fa\u672c\u9014\u5f84\u6709\uff1a\u4f7f\u7528matplotlib\u5e93\u7ed3\u5408FuncAnimation\u51fd\u6570\u3001\u4f7f\u7528Plotly\u5e93\u8fdb\u884c\u52a8\u6001\u4ea4\u4e92\u5f0f\u6570\u636e\u53ef\u89c6\u5316\u3001\u5229\u7528Bokeh\u5e93\u5b9e\u73b0\u590d\u6742\u7684\u52a8\u6001\u56fe\u8868\u3002Python\u4e2d\u6700\u5e38\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u5e93\u4e4b\u4e00matplotlib\u7684FuncAnimation\u51fd\u6570\u53ef\u4ee5\u521b\u9020\u52a8\u6001\u56fe\u8868\uff0c\u5b83\u901a\u8fc7\u4e0d\u65ad\u66f4\u65b0\u56fe\u8868\u4e2d\u7684\u6570\u636e\u70b9\u5e76\u91cd\u65b0\u7ed8\u5236\u6765\u5b9e\u73b0\u52a8\u753b\u6548\u679c\u3002<\/p>\n<p>\u5728matplotlib\u4e2d\uff0c\u9996\u5148\u9700\u8981\u8bbe\u7f6e\u597d\u56fe\u8868\u7684\u57fa\u672c\u7ed3\u6784\u548c\u6837\u5f0f\uff0c\u7136\u540e\u901a\u8fc7\u66f4\u65b0\u51fd\u6570\u6765\u8fde\u7eed\u6539\u53d8\u56fe\u8868\u4e2d\u7684\u6570\u636e\u70b9\uff0c\u5e76\u4f7f\u7528FuncAnimation\u51fd\u6570\u6765\u5b9a\u671f\u8c03\u7528\u8fd9\u4e2a\u66f4\u65b0\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c\u4e0e\u57fa\u7840\u8bbe\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u753b\u52a8\u6001\u56fe\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5bfc\u5165\u4e86\u6b63\u786e\u7684Python\u5e93\u3002matplotlib\u662f\u6700\u57fa\u7840\u7684\u7ed8\u56fe\u5e93\uff0c\u800c\u5b83\u7684\u5b50\u5e93matplotlib.animation\u6b63\u662f\u7528\u4e8e\u521b\u5efa\u52a8\u6001\u56fe\u7684\u91cd\u8981\u6a21\u5757\u3002\u540c\u65f6\uff0c\u9700\u8981\u5bfc\u5165NumPy\u5e93\u4ee5\u4fbf\u4e8e\u5904\u7406\u548c\u751f\u6210\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.animation import FuncAnimation<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9996\u5148\u8bbe\u5b9a\u753b\u5e03\u548c\u8f74\u57df\uff0c\u4e3a\u540e\u7eed\u7684\u52a8\u753b\u7ed8\u5236\u505a\u597d\u51c6\u5907\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e9b\u521d\u59cb\u53c2\u6570\uff0c\u4f8b\u5982\u56fe\u7684\u5927\u5c0f\u3001\u8f74\u7684\u9650\u5236\u4ee5\u53ca\u9700\u8981\u7528\u5230\u7684\u6837\u5f0f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u56fe\u8868\u7684\u5927\u5c0f<\/p>\n<p>fig, ax = plt.subplots(figsize=(8, 5))<\/p>\n<h2><strong>\u5b9a\u4e49\u8f74\u7684\u9650\u5236<\/strong><\/h2>\n<p>ax.set_xlim(0, 10)<\/p>\n<p>ax.set_ylim(-1, 1)<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u6761\u7ebf<\/strong><\/h2>\n<p>line, = ax.plot([], [], lw=3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u5b9a\u4e49\u521d\u59cb\u5316\u51fd\u6570\u4e0e\u66f4\u65b0\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u52a8\u6001\u56fe\u7684\u5c55\u793a\u66f4\u52a0\u5e73\u6ed1\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u521d\u59cb\u5316\u51fd\u6570\uff0c\u7528\u6765\u8bbe\u7f6e\u57fa\u7ebf\uff0c\u786e\u4fdd\u5728\u52a8\u753b\u5f00\u59cb\u65f6\u56fe\u8868\u662f\u5e72\u51c0\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def init():<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u6570\u636e\u96c6<\/p>\n<p>    line.set_data([], [])<\/p>\n<p>    return line,<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u66f4\u65b0\u51fd\u6570\u662f\u52a8\u6001\u56fe\u4e2d\u6700\u6838\u5fc3\u7684\u90e8\u5206\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u8ba1\u7b97\u5e76\u8bbe\u7f6e\u65b0\u7684\u6570\u636e\u70b9\uff0c\u7136\u540eMatplotlib\u4f1a\u91cd\u65b0\u7ed8\u5236\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def update(frame):<\/p>\n<p>    # x\u6570\u636e\u4ee3\u8868\u65f6\u95f4\u6216\u8005\u5176\u4ed6\u8fde\u7eed\u7684\u53d8\u91cf<\/p>\n<p>    x_data = np.linspace(0, 10, 1000)<\/p>\n<p>    # y\u6570\u636e\u662f\u6570\u636e\u70b9\u7684\u503c\uff0c\u8fd9\u91cc\u5047\u8bbe\u662f\u67d0\u79cd\u6b63\u5f26\u6ce2\u5f62\u7684\u53d8\u5316<\/p>\n<p>    y_data = np.sin(2 * np.pi * (x_data - 0.01 * frame))<\/p>\n<p>    # \u8bbe\u7f6e\u65b0\u7684\u6570\u636e\u70b9<\/p>\n<p>    line.set_data(x_data, y_data)<\/p>\n<p>    return line,<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u521b\u5efa\u52a8\u6001\u56fe\u8868\u5e76\u5c55\u793a<\/strong><\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u53ea\u9700\u8c03\u7528FuncAnimation\u51fd\u6570\uff0c\u5e76\u63d0\u4f9b\u753b\u5e03fig\u3001\u66f4\u65b0\u51fd\u6570update\u548c\u521d\u59cb\u5316\u51fd\u6570init\uff0c\u4ee5\u53ca\u5e27\u6570\u548c\u66f4\u65b0\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">anim = FuncAnimation(fig, update, init_func=init,<\/p>\n<p>                     frames=200, interval=20, blit=True)<\/p>\n<h2><strong>\u5c55\u793a\u52a8\u6001\u56fe<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728frames\u53c2\u6570\u4e2d\u8bbe\u7f6e\u5e27\u6570\uff0c\u610f\u5473\u7740\u52a8\u753b\u5c06\u8fdb\u884c\u591a\u5c11\u6b21\u66f4\u65b0\uff0c\u800cinterval\u53c2\u6570\u5219\u8bbe\u7f6e\u4e86\u6bcf\u6b21\u66f4\u65b0\u7684\u65f6\u95f4\u95f4\u9694\uff08\u5355\u4f4d\u4e3a\u6beb\u79d2\uff09\u3002\u8bbe\u7f6eblit=True\u5c06\u53ea\u91cd\u65b0\u7ed8\u5236\u53d8\u5316\u7684\u90e8\u5206\uff0c\u8fd9\u5c06\u663e\u8457\u63d0\u9ad8\u52a8\u753b\u7684\u7ed8\u5236\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u57fa\u672c\u7684Python\u52a8\u6001\u56fe\u8868\u3002\u8fd9\u53ea\u662f\u4e00\u4e2a\u8d77\u70b9\uff0c\u6839\u636e\u5b9e\u9645\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u52a8\u753b\u8fdb\u884c\u591a\u65b9\u9762\u7684\u6269\u5c55\u548c\u81ea\u5b9a\u4e49\uff0c\u6bd4\u5982\u6dfb\u52a0\u6ce8\u91ca\u3001\u81ea\u5b9a\u4e49\u6837\u5f0f\u3001\u5e94\u7528\u4e0d\u540c\u7684\u6570\u5b66\u6a21\u578b\u6765\u751f\u6210\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u62d3\u5c55\u4e0e\u81ea\u5b9a\u4e49<\/strong><\/p>\n<\/p>\n<p><p>\u52a8\u6001\u56fe\u4e0d\u5c40\u9650\u4e8e\u57fa\u672c\u7ebf\u6761\u7684\u52a8\u6001\u66f4\u65b0\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u7528\u6237\u53ef\u4ee5\u5bf9\u52a8\u753b\u8fdb\u884c\u591a\u79cd\u62d3\u5c55\u548c\u81ea\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u7684\u51e0\u4f55\u56fe\u5f62\uff08\u5982\u6563\u70b9\u3001\u6761\u5f62\u56fe\u7b49\uff09\u5230\u52a8\u753b\u4e2d\uff0c\u5e76\u540c\u65f6\u66f4\u65b0\u5b83\u4eec\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u53ef\u4ee5\u901a\u8fc7\u5f15\u5165\u4ea4\u4e92\u529f\u80fd\uff0c\u5982\u6309\u94ae\u548c\u6ed1\u52a8\u6761\uff0c\u6765\u8c03\u6574\u5c55\u793a\u7684\u6570\u636e\u96c6\u3002\u8fd9\u4e9b\u90fd\u53ef\u4ee5\u501f\u52a9matplotlib\u7684\u5176\u4ed6\u5b50\u5e93\u548c\u76f8\u5173\u5de5\u5177\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u52a8\u6001\u56fe\u7684\u7f8e\u5316\u4e5f\u5341\u5206\u91cd\u8981\u3002\u8c03\u6574\u56fe\u4f8b\u3001\u6807\u9898\u3001\u8f74\u6807\u7b7e\u4e0e\u6837\u5f0f\u5c06\u4f7f\u52a8\u753b\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u5438\u5f15\u529b\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u901a\u8fc7\u8c03\u6574\u52a8\u753b\u7684\u901f\u5ea6\u3001\u5e27\u6570\u548c\u5176\u4ed6\u53c2\u6570\uff0c\u53ef\u4ee5\u9488\u5bf9\u4e0d\u540c\u7684\u5c55\u793a\u573a\u5408\u5b9a\u5236\u5316\u52a8\u753b\u7684\u5c55\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u52a8\u6001\u56fe\u8868\uff1f<\/strong><\/p>\n<p>Python \u6709\u591a\u79cd\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u52a8\u6001\u56fe\u8868\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f Matplotlib \u548c Plotly\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u6765\u4f7f\u7528\u8fd9\u4e9b\u5e93\u7ed8\u5236\u52a8\u6001\u56fe\u8868\uff1a<\/p>\n<ol>\n<li>\u5b89\u88c5 Matplotlib \u6216 Plotly\uff1a\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u4f7f\u7528\u5305\u7ba1\u7406\u5668\uff08\u6bd4\u5982 pip\uff09\u5b89\u88c5\u8fd9\u4e9b\u5e93\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5 Matplotlib\uff1a<\/li>\n<\/ol>\n<pre><code>pip install matplotlib\n<\/code><\/pre>\n<p>\u6216\u8005\u53ef\u4ee5\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5 Plotly\uff1a<\/p>\n<pre><code>pip install plotly\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff1a\u5728 Python \u811a\u672c\u4e2d\u5bfc\u5165 Matplotlib \u6216 Plotly\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165 Matplotlib\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n<\/code><\/pre>\n<p>\u6216\u8005\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165 Plotly\uff1a<\/p>\n<pre><code class=\"language-python\">import plotly.graph_objects as go\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u521b\u5efa\u52a8\u6001\u56fe\u8868\uff1a\u4f7f\u7528 Matplotlib \u6216 Plotly \u7684\u51fd\u6570\u548c\u65b9\u6cd5\u521b\u5efa\u52a8\u6001\u56fe\u8868\u3002\u5bf9\u4e8e Matplotlib\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>FuncAnimation<\/code>\u51fd\u6570\u6765\u52a8\u6001\u66f4\u65b0\u56fe\u8868\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Matplotlib \u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport matplotlib.animation as animation\nimport numpy as np\n\n# \u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u51fd\u6570\ndef update_graph(frame):\n    x = np.linspace(0, 2 * np.pi, 100)\n    y = np.sin(x + frame)\n    plt.cla()\n    plt.plot(x, y)\n\n# \u521b\u5efa\u7a7a\u767d\u56fe\u8868\nfig = plt.figure()\n\n# \u521b\u5efa\u52a8\u753b\nani = animation.FuncAnimation(fig, update_graph, frames=100, interval=50)\n\n# \u663e\u793a\u52a8\u6001\u56fe\u8868\nplt.show()\n<\/code><\/pre>\n<p>\u5bf9\u4e8e Plotly\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>update_traces<\/code>\u65b9\u6cd5\u6765\u5b9e\u65f6\u66f4\u65b0\u56fe\u8868\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Plotly \u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import plotly.graph_objects as go\nimport numpy as np\n\n# \u521b\u5efa\u56fe\u8868\u548c\u521d\u59cb\u6570\u636e\nx = np.linspace(0, 2 * np.pi, 100)\ny = np.sin(x)\nfig = go.Figure(data=go.Scatter(x=x, y=y))\n\n# \u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u51fd\u6570\ndef update_graph(frame):\n    y = np.sin(x + frame\/10)\n    fig.update_traces(y=y)\n\n# \u521b\u5efa\u52a8\u753b\nani = animation.FuncAnimation(fig, update_graph, frames=100)\n\n# \u663e\u793a\u52a8\u6001\u56fe\u8868\nfig.show()\n<\/code><\/pre>\n<p>\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u80fd\u5e2e\u52a9\u60a8\u4f7f\u7528 Python \u7ed8\u5236\u52a8\u6001\u56fe\u8868\uff01<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5236\u4f5c\u5e26\u6709\u52a8\u6001\u6548\u679c\u7684\u56fe\u8868\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u548cPlotly\u7b49\u5e93\u5236\u4f5c\u5e26\u6709\u52a8\u6001\u6548\u679c\u7684\u56fe\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\u6765\u521b\u5efa\u8fd9\u6837\u7684\u56fe\u8868\uff1a<\/p>\n<ol>\n<li>\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff1a\u60a8\u9700\u8981\u5728\u60a8\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5Matplotlib\u548cPlotly\u5e93\u3002\u4f7f\u7528\u5305\u7ba1\u7406\u5668\uff08\u5982pip\uff09\u6765\u5b89\u88c5\u5b83\u4eec\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528pip\u5b89\u88c5\u7684\u793a\u4f8b\u547d\u4ee4\uff1a<\/li>\n<\/ol>\n<pre><code>pip install matplotlib\npip install plotly\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5bfc\u5165\u5e93\uff1a\u5728Python\u811a\u672c\u7684\u5f00\u5934\u5bfc\u5165Matplotlib\u6216Plotly\u5e93\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Matplotlib\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Plotly\uff1a<\/p>\n<pre><code class=\"language-python\">import plotly.graph_objects as go\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u521b\u5efa\u52a8\u6001\u56fe\u8868\uff1a\u4f7f\u7528Matplotlib\u7684<code>FuncAnimation<\/code>\u51fd\u6570\u6216Plotly\u7684<code>update_traces<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u52a8\u6001\u56fe\u8868\u3002\u8fd9\u4e9b\u51fd\u6570\u548c\u65b9\u6cd5\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5e27\uff08frame\uff09\u4e4b\u95f4\u66f4\u65b0\u56fe\u8868\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u548cPlotly\u5206\u522b\u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<p>\u4f7f\u7528Matplotlib\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport matplotlib.animation as animation\nimport numpy as np\n\n# \u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u51fd\u6570\ndef update_graph(frame):\n    x = np.linspace(0, 2 * np.pi, 100)\n    y = np.sin(x + frame)\n    plt.cla()\n    plt.plot(x, y)\n\n# \u521b\u5efa\u7a7a\u767d\u56fe\u8868\nfig = plt.figure()\n\n# \u521b\u5efa\u52a8\u753b\nani = animation.FuncAnimation(fig, update_graph, frames=100, interval=50)\n\n# \u663e\u793a\u52a8\u6001\u56fe\u8868\nplt.show()\n<\/code><\/pre>\n<p>\u4f7f\u7528Plotly\uff1a<\/p>\n<pre><code class=\"language-python\">import plotly.graph_objects as go\nimport numpy as np\n\n# \u521b\u5efa\u56fe\u8868\u548c\u521d\u59cb\u6570\u636e\nx = np.linspace(0, 2 * np.pi, 100)\ny = np.sin(x)\nfig = go.Figure(data=go.Scatter(x=x, y=y))\n\n# \u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u51fd\u6570\ndef update_graph(frame):\n    y = np.sin(x + frame\/10)\n    fig.update_traces(y=y)\n\n# \u521b\u5efa\u52a8\u753b\nani = animation.FuncAnimation(fig, update_graph, frames=100)\n\n# \u663e\u793a\u52a8\u6001\u56fe\u8868\nfig.show()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u5f00\u59cb\u5236\u4f5c\u5e26\u6709\u52a8\u6001\u6548\u679c\u7684\u56fe\u8868\uff0c\u5c1d\u8bd5\u4e0d\u540c\u7684\u6570\u636e\u548c\u52a8\u753b\u6548\u679c\uff0c\u4f7f\u56fe\u8868\u66f4\u52a0\u751f\u52a8\u548c\u6709\u8da3\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u52a8\u6001\u56fe\u8868\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u52a8\u753b\u6587\u4ef6\uff1f<\/strong><\/p>\n<p>\u8981\u4f7f\u7528Python\u7ed8\u5236\u52a8\u6001\u56fe\u8868\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u52a8\u753b\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Plotly\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\u6765\u5b8c\u6210\u6b64\u4efb\u52a1\uff1a<\/p>\n<ol>\n<li>\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff1a\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u4e2d\u5df2\u5b89\u88c5Matplotlib\u548cPlotly\u5e93\u3002\u4f7f\u7528\u5305\u7ba1\u7406\u5668\uff08\u5982pip\uff09\u8fdb\u884c\u5b89\u88c5\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528pip\u5b89\u88c5\u7684\u793a\u4f8b\u547d\u4ee4\uff1a<\/li>\n<\/ol>\n<pre><code>pip install matplotlib\npip install plotly\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff1a\u5728Python\u811a\u672c\u7684\u5f00\u5934\u5bfc\u5165Matplotlib\u6216Plotly\u5e93\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Matplotlib\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Plotly\uff1a<\/p>\n<pre><code class=\"language-python\">import plotly.graph_objects as go\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u521b\u5efa\u52a8\u6001\u56fe\u8868\uff1a\u4f7f\u7528Matplotlib\u7684<code>FuncAnimation<\/code>\u51fd\u6570\u6216Plotly\u7684<code>update_traces<\/code>\u65b9\u6cd5\u521b\u5efa\u52a8\u6001\u56fe\u8868\uff0c\u5e76\u786e\u4fdd\u8bbe\u7f6e\u52a8\u753b\u7684\u4fdd\u5b58\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u548cPlotly\u5206\u522b\u521b\u5efa\u52a8\u6001\u56fe\u8868\u5e76\u4fdd\u5b58\u4e3a\u52a8\u753b\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<p>\u4f7f\u7528Matplotlib\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport matplotlib.animation as animation\nimport numpy as np\n\n# \u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u51fd\u6570\ndef update_graph(frame):\n    x = np.linspace(0, 2 * np.pi, 100)\n    y = np.sin(x + frame)\n    plt.cla()\n    plt.plot(x, y)\n\n# \u521b\u5efa\u7a7a\u767d\u56fe\u8868\nfig = plt.figure()\n\n# \u521b\u5efa\u52a8\u753b\nani = animation.FuncAnimation(fig, update_graph, frames=100, interval=50)\n\n# \u4fdd\u5b58\u52a8\u753b\u6587\u4ef6\nani.save(&#039;animation.mp4&#039;, writer=&#039;ffmpeg&#039;)\n\n# \u663e\u793a\u52a8\u6001\u56fe\u8868\nplt.show()\n<\/code><\/pre>\n<p>\u4f7f\u7528Plotly\uff1a<\/p>\n<pre><code class=\"language-python\">import plotly.graph_objects as go\nimport numpy as np\n\n# \u521b\u5efa\u56fe\u8868\u548c\u521d\u59cb\u6570\u636e\nx = np.linspace(0, 2 * np.pi, 100)\ny = np.sin(x)\nfig = go.Figure(data=go.Scatter(x=x, y=y))\n\n# \u521b\u5efa\u52a8\u6001\u56fe\u8868\u7684\u51fd\u6570\ndef update_graph(frame):\n    y = np.sin(x + frame\/10)\n    fig.update_traces(y=y)\n\n# \u521b\u5efa\u52a8\u753b\nani = animation.FuncAnimation(fig, update_graph, frames=100)\n\n# \u4fdd\u5b58\u52a8\u753b\u6587\u4ef6\nani.save(&#039;animation.html&#039;)\n\n# \u663e\u793a\u52a8\u6001\u56fe\u8868\nfig.show()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u5c06\u52a8\u6001\u56fe\u8868\u4fdd\u5b58\u4e3a\u52a8\u753b\u6587\u4ef6\uff0c\u6587\u4ef6\u683c\u5f0f\u53ef\u4ee5\u662fMP4\uff08\u4f7f\u7528FFmpeg\u7f16\u7801\uff09\u6216HTML\u3002\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7684\u65f6\u5019\u64ad\u653e\u60a8\u7684\u52a8\u6001\u56fe\u8868\uff0c\u5e76\u4e0e\u4ed6\u4eba\u5206\u4eab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u786e\u5b9e\u5730\uff0c\u4f7f\u7528Python\u53ef\u4ee5\u6709\u6548\u5730\u521b\u5efa\u52a8\u6001\u56fe\u8868\uff0c\u672c\u8d28\u4e0a\u662f\u4e00\u7cfb\u5217\u56fe\u8868\u8fde\u7eed\u5c55\u793a\u4ee5\u6a21\u62df\u52a8\u6001\u6548\u679c\u3002\u8fd9\u6837\u7684\u56fe\u8868\u5e7f\u6cdb\u7528\u4e8e\u6570 [&hellip;]","protected":false},"author":3,"featured_media":173597,"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\/173592"}],"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=173592"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173592\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173597"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}