{"id":1157948,"date":"2025-01-13T18:34:47","date_gmt":"2025-01-13T10:34:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1157948.html"},"modified":"2025-01-13T18:34:49","modified_gmt":"2025-01-13T10:34:49","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%88%86%e6%94%af%e7%bb%93%e6%9e%84%e5%a6%82%e4%bd%95%e7%94%bb%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1157948.html","title":{"rendered":"\u5982\u4f55\u7528python\u5206\u652f\u7ed3\u6784\u5982\u4f55\u753b\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200036\/194c3d18-d1f9-4d83-93c4-5f1f184bbfc6.webp\" alt=\"\u5982\u4f55\u7528python\u5206\u652f\u7ed3\u6784\u5982\u4f55\u753b\u56fe\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5206\u652f\u7ed3\u6784\u753b\u56fe\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\uff1aif-else\u8bed\u53e5\u3001for\u5faa\u73af\u548cwhile\u5faa\u73af\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u63a7\u5236\u7ed8\u56fe\u3001\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u7ed8\u5236\u4e0d\u540c\u56fe\u5f62\u3001\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u53c2\u6570\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u63a7\u5236\u7ed8\u56fe\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528if-else\u8bed\u53e5\u6765\u63a7\u5236\u56fe\u5f62\u7684\u989c\u8272\u3001\u6837\u5f0f\u3001\u5f62\u72b6\u7b49\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5728Python\u4e2d\u5b9e\u73b0\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528if-else\u8bed\u53e5\u63a7\u5236\u7ed8\u56fe<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8fdb\u884c\u7ed8\u56fe\u65f6\uff0c\u53ef\u4ee5\u5229\u7528if-else\u8bed\u53e5\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u6765\u63a7\u5236\u56fe\u5f62\u7684\u7ed8\u5236\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7if-else\u8bed\u53e5\u6765\u51b3\u5b9a\u56fe\u5f62\u7684\u989c\u8272\u3001\u7ebf\u578b\u3001\u6807\u8bb0\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528if-else\u8bed\u53e5\u6765\u63a7\u5236\u7ebf\u6761\u7684\u989c\u8272\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<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u63a7\u5236\u7ed8\u56fe\u989c\u8272<\/strong><\/h2>\n<p>color = &#39;red&#39; if max(y) &gt; 0.5 else &#39;blue&#39;<\/p>\n<p>plt.plot(x, y, color=color)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Sin Function&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cif-else\u8bed\u53e5\u7528\u4e8e\u68c0\u67e5y\u7684\u6700\u5927\u503c\u662f\u5426\u5927\u4e8e0.5\uff0c\u5e76\u6839\u636e\u7ed3\u679c\u8bbe\u7f6e\u7ed8\u56fe\u7684\u989c\u8272\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u989c\u8272\u4e3a\u7ea2\u8272\uff1b\u5426\u5219\uff0c\u989c\u8272\u4e3a\u84dd\u8272\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528for\u5faa\u73af\u8fdb\u884c\u6279\u91cf\u7ed8\u56fe<\/p>\n<\/p>\n<p><p>for\u5faa\u73af\u53ef\u4ee5\u7528\u6765\u6279\u91cf\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u5e76\u6839\u636e\u6bcf\u4e2a\u6570\u636e\u70b9\u7684\u5c5e\u6027\u7ed8\u5236\u4e0d\u540c\u7684\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\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<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u6279\u91cf\u7ed8\u56fe<\/strong><\/h2>\n<p>for y in [y1, y2]:<\/p>\n<p>    plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Sin and Cos Functions&#39;)<\/p>\n<p>plt.legend([&#39;sin(x)&#39;, &#39;cos(x)&#39;])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u904d\u5386\u4e86\u4e00\u4e2a\u5305\u542by1\u548cy2\u7684\u5217\u8868\uff0c\u5e76\u5206\u522b\u7ed8\u5236\u4e86sin(x)\u548ccos(x)\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528while\u5faa\u73af\u8fdb\u884c\u52a8\u6001\u7ed8\u56fe<\/p>\n<\/p>\n<p><p>while\u5faa\u73af\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u52a8\u6001\u7ed8\u56fe\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5b9e\u65f6\u66f4\u65b0\u56fe\u5f62\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u4e0d\u65ad\u66f4\u65b0\u56fe\u5f62\u6570\u636e\uff0c\u4ee5\u521b\u5efa\u52a8\u753b\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528while\u5faa\u73af\u5b9e\u73b0\u52a8\u6001\u7ed8\u56fe\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>import time<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p>plt.ion()  # \u5f00\u542f\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>line, = ax.plot(x, y)<\/p>\n<h2><strong>\u52a8\u6001\u66f4\u65b0\u56fe\u5f62<\/strong><\/h2>\n<p>t = 0<\/p>\n<p>while t &lt; 10:<\/p>\n<p>    y = np.sin(x + t)<\/p>\n<p>    line.set_ydata(y)<\/p>\n<p>    fig.canvas.draw()<\/p>\n<p>    fig.canvas.flush_events()<\/p>\n<p>    time.sleep(0.1)<\/p>\n<p>    t += 0.1<\/p>\n<p>plt.ioff()  # \u5173\u95ed\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cwhile\u5faa\u73af\u7528\u4e8e\u4e0d\u65ad\u66f4\u65b0y\u7684\u6570\u636e\uff0c\u5e76\u4f7f\u7528set_ydata\u65b9\u6cd5\u66f4\u65b0\u56fe\u5f62\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u7ed8\u5236\u4e0d\u540c\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u7ed8\u5236\u4e0d\u540c\u7684\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u6839\u636e\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u9009\u62e9\u4e0d\u540c\u7684\u56fe\u5f62\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u6839\u636e\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u9009\u62e9\u4e0d\u540c\u7684\u56fe\u5f62\u7c7b\u578b\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<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randn(1000)<\/p>\n<h2><strong>\u6839\u636e\u6570\u636e\u5206\u5e03\u7ed8\u5236\u4e0d\u540c\u56fe\u5f62<\/strong><\/h2>\n<p>if np.mean(data) &gt; 0:<\/p>\n<p>    plt.hist(data, bins=30, color=&#39;blue&#39;)<\/p>\n<p>    plt.title(&#39;Histogram&#39;)<\/p>\n<p>else:<\/p>\n<p>    plt.boxplot(data)<\/p>\n<p>    plt.title(&#39;Boxplot&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cif-else\u8bed\u53e5\u7528\u4e8e\u68c0\u67e5\u6570\u636e\u7684\u5747\u503c\uff0c\u5e76\u6839\u636e\u7ed3\u679c\u9009\u62e9\u7ed8\u5236\u76f4\u65b9\u56fe\u6216\u7bb1\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u56fe\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u7279\u5f81\u8c03\u6574\u56fe\u5f62\u7684\u523b\u5ea6\u3001\u6807\u7b7e\u3001\u6807\u9898\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u7684\u53c2\u6570\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<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u53c2\u6570<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>if max(y) &gt; 0.5:<\/p>\n<p>    plt.title(&#39;Sin Function (High Peak)&#39;)<\/p>\n<p>    plt.ylim(-1, 1)<\/p>\n<p>else:<\/p>\n<p>    plt.title(&#39;Sin Function (Low Peak)&#39;)<\/p>\n<p>    plt.ylim(-0.5, 0.5)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cif-else\u8bed\u53e5\u7528\u4e8e\u68c0\u67e5y\u7684\u6700\u5927\u503c\uff0c\u5e76\u6839\u636e\u7ed3\u679c\u8c03\u6574\u56fe\u5f62\u7684\u6807\u9898\u548cy\u8f74\u7684\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u7ed3\u5408\u591a\u79cd\u5206\u652f\u7ed3\u6784\u8fdb\u884c\u590d\u6742\u7ed8\u56fe<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u5206\u652f\u7ed3\u6784\u6765\u5b9e\u73b0\u590d\u6742\u7684\u7ed8\u56fe\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528if-else\u8bed\u53e5\u548cfor\u5faa\u73af\uff0c\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408\u591a\u79cd\u5206\u652f\u7ed3\u6784\u8fdb\u884c\u590d\u6742\u7ed8\u56fe\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<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<p>y3 = np.tan(x)<\/p>\n<h2><strong>\u7ed3\u5408\u591a\u79cd\u5206\u652f\u7ed3\u6784\u8fdb\u884c\u590d\u6742\u7ed8\u56fe<\/strong><\/h2>\n<p>for y in [y1, y2, y3]:<\/p>\n<p>    if max(y) &gt; 1:<\/p>\n<p>        plt.plot(x, y, linestyle=&#39;--&#39;)<\/p>\n<p>    else:<\/p>\n<p>        plt.plot(x, y, linestyle=&#39;-&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Trigonometric Functions&#39;)<\/p>\n<p>plt.legend([&#39;sin(x)&#39;, &#39;cos(x)&#39;, &#39;tan(x)&#39;])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u7528\u4e8e\u904d\u5386y1\u3001y2\u548cy3\uff0cif-else\u8bed\u53e5\u7528\u4e8e\u6839\u636e\u6bcf\u4e2a\u51fd\u6570\u7684\u6700\u5927\u503c\u51b3\u5b9a\u7ebf\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5b9e\u7528\u6848\u4f8b\uff1a\u7ed8\u5236\u5206\u7c7b\u6570\u636e\u7684\u6563\u70b9\u56fe<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u7ed8\u5236\u5206\u7c7b\u6570\u636e\u7684\u6563\u70b9\u56fe\u662f\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u53ef\u4ee5\u4f7f\u7528if-else\u8bed\u53e5\u548cfor\u5faa\u73af\uff0c\u6839\u636e\u5206\u7c7b\u6570\u636e\u7684\u6807\u7b7e\u7ed8\u5236\u4e0d\u540c\u989c\u8272\u7684\u6563\u70b9\u56fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u5206\u7c7b\u6570\u636e\u7684\u6563\u70b9\u56fe\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<h2><strong>\u751f\u6210\u5206\u7c7b\u6570\u636e<\/strong><\/h2>\n<p>np.random.seed(0)<\/p>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<p>labels = np.random.choice([&#39;A&#39;, &#39;B&#39;], size=100)<\/p>\n<h2><strong>\u7ed8\u5236\u5206\u7c7b\u6570\u636e\u7684\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>for label in np.unique(labels):<\/p>\n<p>    plt.scatter(x[labels == label], y[labels == label], label=label)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Scatter Plot of Classified Data&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u7528\u4e8e\u904d\u5386\u552f\u4e00\u7684\u6807\u7b7e\uff0c\u5e76\u4f7f\u7528plt.scatter\u65b9\u6cd5\u7ed8\u5236\u4e0d\u540c\u989c\u8272\u7684\u6563\u70b9\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5b9e\u7528\u6848\u4f8b\uff1a\u6839\u636e\u6570\u636e\u7279\u5f81\u7ed8\u5236\u4e0d\u540c\u7684\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u7279\u5f81\u9009\u62e9\u4e0d\u540c\u7684\u56fe\u5f62\u7c7b\u578b\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u4f8b\u5982\uff0c\u6839\u636e\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u9009\u62e9\u7ed8\u5236\u76f4\u65b9\u56fe\u3001\u7bb1\u7ebf\u56fe\u6216\u6563\u70b9\u56fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u6839\u636e\u6570\u636e\u7684\u7279\u5f81\u7ed8\u5236\u4e0d\u540c\u7684\u56fe\u5f62\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<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randn(1000)<\/p>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<h2><strong>\u6839\u636e\u6570\u636e\u7279\u5f81\u7ed8\u5236\u4e0d\u540c\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>if np.mean(data) &gt; 0:<\/p>\n<p>    plt.hist(data, bins=30, color=&#39;blue&#39;)<\/p>\n<p>    plt.title(&#39;Histogram&#39;)<\/p>\n<p>elif np.median(data) &gt; 0:<\/p>\n<p>    plt.boxplot(data)<\/p>\n<p>    plt.title(&#39;Boxplot&#39;)<\/p>\n<p>else:<\/p>\n<p>    plt.scatter(x, y)<\/p>\n<p>    plt.title(&#39;Scatter Plot&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cif-elif-else\u8bed\u53e5\u7528\u4e8e\u6839\u636e\u6570\u636e\u7684\u5747\u503c\u548c\u4e2d\u4f4d\u6570\u9009\u62e9\u4e0d\u540c\u7684\u56fe\u5f62\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5b9e\u7528\u6848\u4f8b\uff1a\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u53c2\u6570\u4ee5\u9002\u5e94\u4e0d\u540c\u6570\u636e\u96c6<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u591a\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u96c6\uff0c\u5e76\u6839\u636e\u6570\u636e\u96c6\u7684\u7279\u5f81\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u53c2\u6570\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u6570\u636e\u96c6\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<h2><strong>\u751f\u6210\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data_sets = [np.random.randn(1000), np.random.rand(1000)]<\/p>\n<h2><strong>\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u53c2\u6570\u4ee5\u9002\u5e94\u4e0d\u540c\u6570\u636e\u96c6<\/strong><\/h2>\n<p>for data in data_sets:<\/p>\n<p>    plt.figure()<\/p>\n<p>    if np.std(data) &gt; 0.5:<\/p>\n<p>        plt.hist(data, bins=30, color=&#39;green&#39;)<\/p>\n<p>        plt.title(&#39;Histogram (High Variance)&#39;)<\/p>\n<p>    else:<\/p>\n<p>        plt.hist(data, bins=30, color=&#39;orange&#39;)<\/p>\n<p>        plt.title(&#39;Histogram (Low Variance)&#39;)<\/p>\n<p>    plt.xlabel(&#39;Value&#39;)<\/p>\n<p>    plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>    plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u7528\u4e8e\u904d\u5386\u4e0d\u540c\u7684\u6570\u636e\u96c6\uff0cif-else\u8bed\u53e5\u7528\u4e8e\u6839\u636e\u6570\u636e\u7684\u6807\u51c6\u5dee\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u7684\u989c\u8272\u548c\u6807\u9898\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cPython\u7684\u5206\u652f\u7ed3\u6784\u5728\u7ed8\u56fe\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<strong>\u901a\u8fc7\u4f7f\u7528if-else\u8bed\u53e5\u3001for\u5faa\u73af\u548cwhile\u5faa\u73af\u7b49\u5206\u652f\u7ed3\u6784\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6761\u4ef6\u63a7\u5236\u3001\u6279\u91cf\u7ed8\u56fe\u3001\u52a8\u6001\u66f4\u65b0\u56fe\u5f62\u3001\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u7ed8\u5236\u4e0d\u540c\u56fe\u5f62\u4ee5\u53ca\u52a8\u6001\u8c03\u6574\u56fe\u5f62\u53c2\u6570\u7b49\u529f\u80fd\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5728\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u4e2d\u5177\u6709\u91cd\u8981\u7684\u5e94\u7528\u4ef7\u503c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5c55\u793a\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u5206\u652f\u7ed3\u6784\uff0c\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u7ed8\u56fe\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5206\u652f\u7ed3\u6784\u6765\u9009\u62e9\u4e0d\u540c\u7684\u7ed8\u56fe\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\uff08\u5982if-elif-else\uff09\u6765\u6839\u636e\u7528\u6237\u8f93\u5165\u6216\u5176\u4ed6\u6761\u4ef6\u9009\u62e9\u4e0d\u540c\u7684\u7ed8\u56fe\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u7ed8\u5236\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u6216\u6563\u70b9\u56fe\u3002\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\uff0c\u53ef\u4ee5\u5728\u5206\u652f\u7ed3\u6784\u4e2d\u8c03\u7528\u4e0d\u540c\u7684\u7ed8\u56fe\u51fd\u6570\uff0c\u4ece\u800c\u521b\u5efa\u6240\u9700\u7684\u56fe\u5f62\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5206\u652f\u7ed3\u6784\u7ed8\u56fe\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u5206\u652f\u7ed3\u6784\u7ed8\u56fe\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u6761\u4ef6\u5224\u65ad\u4e0d\u51c6\u786e\u3001\u672a\u5bfc\u5165\u5fc5\u8981\u7684\u7ed8\u56fe\u5e93\u4ee5\u53ca\u672a\u6b63\u786e\u8bbe\u7f6e\u56fe\u5f62\u7684\u6807\u7b7e\u548c\u6807\u9898\u3002\u786e\u4fdd\u903b\u8f91\u6e05\u6670\u4e14\u6761\u4ef6\u8986\u76d6\u5168\u9762\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\u548c\u56fe\u5f62\u663e\u793a\u4e0d\u6b63\u786e\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5206\u652f\u7ed3\u6784\u52a8\u6001\u8c03\u6574\u7ed8\u56fe\u53c2\u6570\uff1f<\/strong><br \/>\u901a\u8fc7\u4f7f\u7528\u5206\u652f\u7ed3\u6784\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u52a8\u6001\u8c03\u6574\u7ed8\u56fe\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u503c\u6539\u53d8\u56fe\u5f62\u7684\u989c\u8272\u3001\u7ebf\u578b\u6216\u6807\u8bb0\u6837\u5f0f\u3002\u5728\u7ed8\u5236\u56fe\u5f62\u65f6\uff0c\u6355\u6349\u7528\u6237\u7684\u8f93\u5165\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u7ed8\u56fe\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u548c\u4e2a\u6027\u5316\u7684\u56fe\u5f62\u5c55\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5206\u652f\u7ed3\u6784\u753b\u56fe\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\uff1aif-else\u8bed\u53e5\u3001for\u5faa\u73af\u548cwhile\u5faa\u73af\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1157950,"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\/1157948"}],"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=1157948"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157948\/revisions"}],"predecessor-version":[{"id":1157951,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157948\/revisions\/1157951"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1157950"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1157948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1157948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1157948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}