{"id":1090404,"date":"2025-01-08T14:02:25","date_gmt":"2025-01-08T06:02:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1090404.html"},"modified":"2025-01-08T14:02:27","modified_gmt":"2025-01-08T06:02:27","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%88%b6%e4%bd%9c%e6%95%b0%e6%8d%ae%e5%8a%a8%e5%9b%be-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1090404.html","title":{"rendered":"\u5982\u4f55\u7528python\u5236\u4f5c\u6570\u636e\u52a8\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203035\/8f01c5ac-2645-47d9-8a21-18998ccec13b.webp\" alt=\"\u5982\u4f55\u7528python\u5236\u4f5c\u6570\u636e\u52a8\u56fe\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u5236\u4f5c\u6570\u636e\u52a8\u56fe\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165\u76f8\u5173\u5e93\u3001\u51c6\u5907\u6570\u636e\u3001\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f\u3001\u5b9a\u4e49\u66f4\u65b0\u51fd\u6570\u3001\u8c03\u7528\u52a8\u753b\u51fd\u6570\u3001\u4fdd\u5b58\u52a8\u56fe\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528Matplotlib\u548cFuncAnimation\u6765\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u76f8\u5173\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u65f6\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u5fc5\u9700\u7684\u5e93\uff0c\u5982Matplotlib\u548cNumPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>from matplotlib.animation import FuncAnimation<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u800cNumPy\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u6570\u503c\u8ba1\u7b97\u3002FuncAnimation\u662fMatplotlib\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u52a8\u753b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u51c6\u5907\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5236\u4f5c\u52a8\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5c55\u793a\u6b63\u5f26\u6ce2\u548c\u4f59\u5f26\u6ce2\u7684\u53d8\u5316\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u4f7f\u7528NumPy\u7684<code>linspace<\/code>\u51fd\u6570\u751f\u62100\u52302\u03c0\u4e4b\u95f4\u7684100\u4e2a\u70b9\uff0c\u5e76\u8ba1\u7b97\u8fd9\u4e9b\u70b9\u5bf9\u5e94\u7684\u6b63\u5f26\u503c\u548c\u4f59\u5f26\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u8bbe\u7f6e\u56fe\u8868\u7684\u6837\u5f0f\uff0c\u5305\u62ec\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\u548c\u5b50\u56fe\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p>line1, = ax.plot(x, y1, label=&#39;Sine Wave&#39;)<\/p>\n<p>line2, = ax.plot(x, y2, label=&#39;Cosine Wave&#39;)<\/p>\n<p>ax.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u8868\u5bf9\u8c61<code>fig<\/code>\u548c\u4e00\u4e2a\u5b50\u56fe\u5bf9\u8c61<code>ax<\/code>\uff0c\u5e76\u7ed8\u5236\u4e86\u4e24\u6761\u66f2\u7ebf\uff0c\u5206\u522b\u4ee3\u8868\u6b63\u5f26\u6ce2\u548c\u4f59\u5f26\u6ce2\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b9a\u4e49\u66f4\u65b0\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u66f4\u65b0\u51fd\u6570\u662f\u52a8\u753b\u7684\u6838\u5fc3\uff0c\u7528\u4e8e\u66f4\u65b0\u6bcf\u4e00\u5e27\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def update(frame):<\/p>\n<p>    line1.set_ydata(np.sin(x + frame \/ 10.0))<\/p>\n<p>    line2.set_ydata(np.cos(x + frame \/ 10.0))<\/p>\n<p>    return line1, line2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u66f4\u65b0\u4e86\u6b63\u5f26\u6ce2\u548c\u4f59\u5f26\u6ce2\u7684y\u8f74\u6570\u636e\uff0c<code>frame<\/code>\u53c2\u6570\u4ee3\u8868\u5f53\u524d\u7684\u5e27\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8c03\u7528\u52a8\u753b\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528FuncAnimation\u7c7b\u521b\u5efa\u52a8\u753b\uff0c\u6307\u5b9a\u66f4\u65b0\u51fd\u6570\u548c\u5e27\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ani = FuncAnimation(fig, update, frames=100, blit=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aFuncAnimation\u5bf9\u8c61<code>ani<\/code>\uff0c\u5e76\u6307\u5b9a\u4e86\u66f4\u65b0\u51fd\u6570<code>update<\/code>\uff0c\u5e27\u6570\u4e3a100\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4fdd\u5b58\u52a8\u56fe<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4fdd\u5b58\u52a8\u56fe\u4e3aGIF\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ani.save(&#39;data_animation.gif&#39;, writer=&#39;imagemagick&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528<code>imagemagick<\/code>\u4f5c\u4e3aGIF\u7684\u5199\u5165\u5668\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>from matplotlib.animation import FuncAnimation<\/p>\n<h2><strong>\u51c6\u5907\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>line1, = ax.plot(x, y1, label=&#39;Sine Wave&#39;)<\/p>\n<p>line2, = ax.plot(x, y2, label=&#39;Cosine Wave&#39;)<\/p>\n<p>ax.legend()<\/p>\n<h2><strong>\u5b9a\u4e49\u66f4\u65b0\u51fd\u6570<\/strong><\/h2>\n<p>def update(frame):<\/p>\n<p>    line1.set_ydata(np.sin(x + frame \/ 10.0))<\/p>\n<p>    line2.set_ydata(np.cos(x + frame \/ 10.0))<\/p>\n<p>    return line1, line2<\/p>\n<h2><strong>\u8c03\u7528\u52a8\u753b\u51fd\u6570<\/strong><\/h2>\n<p>ani = FuncAnimation(fig, update, frames=100, blit=True)<\/p>\n<h2><strong>\u4fdd\u5b58\u52a8\u56fe<\/strong><\/h2>\n<p>ani.save(&#39;data_animation.gif&#39;, writer=&#39;imagemagick&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u4f7f\u7528Matplotlib\u548cFuncAnimation\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u7684\u57fa\u672c\u6b65\u9aa4\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5236\u4f5c\u51fa\u5c55\u793a\u6570\u636e\u52a8\u6001\u53d8\u5316\u7684\u52a8\u753b\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u4e2a\u6b65\u9aa4\u7684\u7ec6\u8282\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u76f8\u5173\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u548cFuncAnimation\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u65f6\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u8fd9\u4e9b\u5e93\u3002Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u8868\u7c7b\u578b\u548c\u52a8\u753b\u6548\u679c\u3002NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>from matplotlib.animation import FuncAnimation<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u8fd9\u4e9b\u5e93\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u51c6\u5907\u6570\u636e\u548c\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u51c6\u5907\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u662f\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u901a\u8fc7\u67d0\u4e9b\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u7684\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u751f\u62100\u52302\u03c0\u4e4b\u95f4\u7684100\u4e2a\u70b9\uff0c\u5e76\u8ba1\u7b97\u8fd9\u4e9b\u70b9\u5bf9\u5e94\u7684\u6b63\u5f26\u503c\u548c\u4f59\u5f26\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u6570\u636e\u5c06\u7528\u4e8e\u7ed8\u5236\u6b63\u5f26\u6ce2\u548c\u4f59\u5f26\u6ce2\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f<\/p>\n<\/p>\n<p><p>\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f\u5305\u62ec\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\u548c\u5b50\u56fe\u5bf9\u8c61\uff0c\u5e76\u7ed8\u5236\u6570\u636e\u66f2\u7ebf\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u8868\u5bf9\u8c61<code>fig<\/code>\u548c\u4e00\u4e2a\u5b50\u56fe\u5bf9\u8c61<code>ax<\/code>\uff0c\u5e76\u7ed8\u5236\u4e86\u4e24\u6761\u66f2\u7ebf\uff0c\u5206\u522b\u4ee3\u8868\u6b63\u5f26\u6ce2\u548c\u4f59\u5f26\u6ce2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p>line1, = ax.plot(x, y1, label=&#39;Sine Wave&#39;)<\/p>\n<p>line2, = ax.plot(x, y2, label=&#39;Cosine Wave&#39;)<\/p>\n<p>ax.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>ax.plot<\/code>\u51fd\u6570\u7ed8\u5236\u66f2\u7ebf\uff0c<code>label<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u56fe\u4f8b\u6807\u7b7e\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>ax.legend<\/code>\u51fd\u6570\u663e\u793a\u56fe\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b9a\u4e49\u66f4\u65b0\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u66f4\u65b0\u51fd\u6570\u662f\u52a8\u753b\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u7528\u4e8e\u66f4\u65b0\u6bcf\u4e00\u5e27\u7684\u6570\u636e\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>update<\/code>\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u53c2\u6570<code>frame<\/code>\uff0c\u4ee3\u8868\u5f53\u524d\u7684\u5e27\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def update(frame):<\/p>\n<p>    line1.set_ydata(np.sin(x + frame \/ 10.0))<\/p>\n<p>    line2.set_ydata(np.cos(x + frame \/ 10.0))<\/p>\n<p>    return line1, line2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u66f4\u65b0\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>line1.set_ydata<\/code>\u548c<code>line2.set_ydata<\/code>\u51fd\u6570\u66f4\u65b0\u6b63\u5f26\u6ce2\u548c\u4f59\u5f26\u6ce2\u7684y\u8f74\u6570\u636e\u3002\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06<code>frame<\/code>\u9664\u4ee510\u540e\u52a0\u5230x\u8f74\u6570\u636e\u4e0a\uff0c\u4ee5\u5b9e\u73b0\u6ce2\u5f62\u7684\u52a8\u6001\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8c03\u7528\u52a8\u753b\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528FuncAnimation\u7c7b\u521b\u5efa\u52a8\u753b\uff0c\u6307\u5b9a\u66f4\u65b0\u51fd\u6570\u548c\u5e27\u6570\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aFuncAnimation\u5bf9\u8c61<code>ani<\/code>\uff0c\u5e76\u6307\u5b9a\u4e86\u66f4\u65b0\u51fd\u6570<code>update<\/code>\u548c\u5e27\u6570100\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ani = FuncAnimation(fig, update, frames=100, blit=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>blit<\/code>\u53c2\u6570\u7528\u4e8e\u63d0\u9ad8\u52a8\u753b\u7684\u6548\u7387\uff0c\u901a\u5e38\u8bbe\u7f6e\u4e3a<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4fdd\u5b58\u52a8\u56fe<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>ani.save<\/code>\u51fd\u6570\u5c06\u52a8\u753b\u4fdd\u5b58\u4e3aGIF\u6587\u4ef6\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u52a8\u56fe\u4fdd\u5b58\u4e3a\u540d\u4e3a<code>data_animation.gif<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>imagemagick<\/code>\u4f5c\u4e3aGIF\u7684\u5199\u5165\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ani.save(&#39;data_animation.gif&#39;, writer=&#39;imagemagick&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4fdd\u5b58\u52a8\u56fe\u65f6\u9700\u8981\u5b89\u88c5<code>imagemagick<\/code>\u8f6f\u4ef6\uff0c\u5e76\u786e\u4fdd\u5176\u8def\u5f84\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>Python\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u4e0d\u4ec5\u9002\u7528\u4e8e\u6570\u5b66\u51fd\u6570\u7684\u52a8\u6001\u5c55\u793a\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4ee5\u4e0b\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u53ef\u89c6\u5316<\/strong>\uff1a\u5c55\u793a\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u53d8\u5316\u8d8b\u52bf\uff0c\u5982\u80a1\u7968\u4ef7\u683c\u3001\u6c14\u6e29\u53d8\u5316\u7b49\u3002<\/li>\n<li><strong>\u79d1\u5b66\u7814\u7a76<\/strong>\uff1a\u5c55\u793a\u5b9e\u9a8c\u6570\u636e\u7684\u52a8\u6001\u53d8\u5316\uff0c\u5982\u5206\u5b50\u8fd0\u52a8\u3001\u5929\u4f53\u8fd0\u884c\u8f68\u8ff9\u7b49\u3002<\/li>\n<li><strong>\u6559\u80b2\u6559\u5b66<\/strong>\uff1a\u751f\u52a8\u5c55\u793a\u6570\u5b66\u548c\u7269\u7406\u73b0\u8c61\u7684\u52a8\u6001\u8fc7\u7a0b\uff0c\u5982\u6ce2\u52a8\u3001\u632f\u52a8\u7b49\u3002<\/li>\n<\/ul>\n<p><h3>\u8fdb\u9636\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u66f4\u591a\u7684\u6570\u636e\u6e90\u548c\u590d\u6742\u7684\u56fe\u8868\u6837\u5f0f\u5236\u4f5c\u66f4\u4e3a\u590d\u6742\u7684\u6570\u636e\u52a8\u56fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8fdb\u9636\u5e94\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/strong>\uff1a\u4eceCSV\u6587\u4ef6\u6216\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u6839\u636e\u6570\u636e\u53d8\u5316\u5236\u4f5c\u52a8\u56fe\u3002<\/li>\n<li><strong>\u591a\u5b50\u56fe\u5c55\u793a<\/strong>\uff1a\u5728\u4e00\u4e2a\u56fe\u8868\u4e2d\u5c55\u793a\u591a\u4e2a\u5b50\u56fe\uff0c\u6bcf\u4e2a\u5b50\u56fe\u663e\u793a\u4e0d\u540c\u7684\u6570\u636e\u53d8\u5316\u60c5\u51b5\u3002<\/li>\n<li><strong>\u4ea4\u4e92\u5f0f\u52a8\u56fe<\/strong>\uff1a\u4f7f\u7528<code>matplotlib.widgets<\/code>\u6a21\u5757\u6dfb\u52a0\u4ea4\u4e92\u63a7\u4ef6\uff0c\u5982\u6ed1\u5757\u3001\u6309\u94ae\u7b49\uff0c\u4f7f\u7528\u6237\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\u52a8\u56fe\u53c2\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u5e76\u5236\u4f5c\u52a8\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<p>from matplotlib.animation import FuncAnimation<\/p>\n<h2><strong>\u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u51c6\u5907\u6570\u636e<\/strong><\/h2>\n<p>x = data[&#39;x&#39;]<\/p>\n<p>y1 = data[&#39;y1&#39;]<\/p>\n<p>y2 = data[&#39;y2&#39;]<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>line1, = ax.plot(x, y1, label=&#39;Data 1&#39;)<\/p>\n<p>line2, = ax.plot(x, y2, label=&#39;Data 2&#39;)<\/p>\n<p>ax.legend()<\/p>\n<h2><strong>\u5b9a\u4e49\u66f4\u65b0\u51fd\u6570<\/strong><\/h2>\n<p>def update(frame):<\/p>\n<p>    line1.set_ydata(data[&#39;y1&#39;] + frame)<\/p>\n<p>    line2.set_ydata(data[&#39;y2&#39;] + frame)<\/p>\n<p>    return line1, line2<\/p>\n<h2><strong>\u8c03\u7528\u52a8\u753b\u51fd\u6570<\/strong><\/h2>\n<p>ani = FuncAnimation(fig, update, frames=100, blit=True)<\/p>\n<h2><strong>\u4fdd\u5b58\u52a8\u56fe<\/strong><\/h2>\n<p>ani.save(&#39;data_animation_from_csv.gif&#39;, writer=&#39;imagemagick&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4ece\u540d\u4e3a<code>data.csv<\/code>\u7684\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u6839\u636e\u6570\u636e\u53d8\u5316\u5236\u4f5c\u52a8\u56fe\u3002\u52a8\u56fe\u5c55\u793a\u4e86\u6570\u636e\u968f\u65f6\u95f4\u7684\u53d8\u5316\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5c0f\u7ed3<\/h3>\n<\/p>\n<p><p>\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u662f\u6570\u636e\u53ef\u89c6\u5316\u7684\u91cd\u8981\u624b\u6bb5\uff0c\u53ef\u4ee5\u751f\u52a8\u5c55\u793a\u6570\u636e\u7684\u52a8\u6001\u53d8\u5316\u8fc7\u7a0b\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684Matplotlib\u5e93\u548cFuncAnimation\u7c7b\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u7684\u57fa\u672c\u6b65\u9aa4\u548c\u5e94\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5e94\u7528\u4e8e\u5b9e\u9645\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5236\u4f5c\u6570\u636e\u52a8\u56fe\uff1f<\/strong><br \/>\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u51e0\u4e2a\u6d41\u884c\u7684Python\u5e93\u3002Matplotlib\u662f\u4e00\u4e2a\u57fa\u7840\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u7b80\u5355\u7684\u52a8\u56fe\u3002\u4e3a\u4e86\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6548\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528Plotly\uff0c\u5b83\u63d0\u4f9b\u4e86\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684\u529f\u80fd\u3002\u53e6\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u662fManim\uff0c\u5b83\u9002\u5408\u5236\u4f5c\u6570\u5b66\u76f8\u5173\u7684\u52a8\u753b\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u9ad8\u6548\u7684\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u8003\u8651\u4f7f\u7528Pandas\u7ed3\u5408\u8fd9\u4e9b\u56fe\u5f62\u5e93\uff0c\u80fd\u591f\u5927\u5927\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5236\u4f5c\u6570\u636e\u52a8\u56fe\u901a\u5e38\u5305\u62ec\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u9700\u51c6\u5907\u548c\u6e05\u6d17\u6570\u636e\uff0c\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u5b8c\u6574\u6027\u3002\u63a5\u7740\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u5e76\u6839\u636e\u9700\u6c42\u7ed8\u5236\u9759\u6001\u56fe\u5f62\u3002\u7136\u540e\uff0c\u901a\u8fc7\u8bbe\u7f6e\u52a8\u753b\u53c2\u6570\u5982\u5e27\u6570\u548c\u6301\u7eed\u65f6\u95f4\uff0c\u5c06\u9759\u6001\u56fe\u8f6c\u5316\u4e3a\u52a8\u56fe\u3002\u6700\u540e\uff0c\u4f7f\u7528\u5e93\u4e2d\u7684\u4fdd\u5b58\u529f\u80fd\uff0c\u5c06\u52a8\u56fe\u5bfc\u51fa\u4e3aGIF\u6216MP4\u683c\u5f0f\uff0c\u4fbf\u4e8e\u5206\u4eab\u548c\u5c55\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u6570\u636e\u52a8\u56fe\u7684\u64ad\u653e\u6027\u80fd\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f18\u5316\u6570\u636e\u52a8\u56fe\u7684\u64ad\u653e\u6027\u80fd\uff0c\u53ef\u4ee5\u91c7\u53d6\u51e0\u79cd\u7b56\u7565\u3002\u9996\u5148\uff0c\u51cf\u5c11\u6bcf\u5e27\u7684\u56fe\u5f62\u590d\u6742\u5ea6\uff0c\u4f8b\u5982\u7b80\u5316\u56fe\u5f62\u5143\u7d20\u6216\u964d\u4f4e\u5206\u8fa8\u7387\u3002\u5176\u6b21\uff0c\u63a7\u5236\u52a8\u56fe\u7684\u5e27\u7387\uff0c\u8fc7\u9ad8\u7684\u5e27\u7387\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002\u4f7f\u7528\u5408\u9002\u7684\u6587\u4ef6\u683c\u5f0f\u4e5f\u5f88\u91cd\u8981\uff0cGIF\u6587\u4ef6\u901a\u5e38\u8f83\u5927\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528MP4\u683c\u5f0f\u4ee5\u51cf\u5c0f\u6587\u4ef6\u5927\u5c0f\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u6548\u7387\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u548c\u4e0d\u5fc5\u8981\u7684\u7ed8\u56fe\u64cd\u4f5c\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u52a8\u56fe\u7684\u751f\u6210\u901f\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u5236\u4f5c\u6570\u636e\u52a8\u56fe\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165\u76f8\u5173\u5e93\u3001\u51c6\u5907\u6570\u636e\u3001\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f\u3001\u5b9a\u4e49\u66f4\u65b0\u51fd\u6570\u3001\u8c03\u7528\u52a8\u753b\u51fd\u6570\u3001\u4fdd\u5b58 [&hellip;]","protected":false},"author":3,"featured_media":1090408,"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\/1090404"}],"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=1090404"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090404\/revisions"}],"predecessor-version":[{"id":1090410,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090404\/revisions\/1090410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1090408"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1090404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1090404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1090404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}