{"id":1097975,"date":"2025-01-08T15:16:55","date_gmt":"2025-01-08T07:16:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097975.html"},"modified":"2025-01-08T15:16:57","modified_gmt":"2025-01-08T07:16:57","slug":"python%e7%9b%b4%e6%96%b9%e5%9b%be%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%9d%90%e6%a0%87%e7%9a%84%e5%88%86%e5%ba%a6%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097975.html","title":{"rendered":"python\u76f4\u65b9\u56fe\u5982\u4f55\u8bbe\u7f6e\u5750\u6807\u7684\u5206\u5ea6\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212503\/12a4c213-f16b-4b63-96a9-3c8c546adfca.webp\" alt=\"python\u76f4\u65b9\u56fe\u5982\u4f55\u8bbe\u7f6e\u5750\u6807\u7684\u5206\u5ea6\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u76f4\u65b9\u56fe\u65f6\uff0c\u8bbe\u7f6e\u5750\u6807\u7684\u5206\u5ea6\u503c\u4e3b\u8981\u901a\u8fc7xticks\u548cyticks\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u53ef\u4ee5\u8bbe\u5b9a\u5750\u6807\u8f74\u7684\u523b\u5ea6\u4f4d\u7f6e\u548c\u6807\u7b7e\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u8bbe\u7f6e\u523b\u5ea6\u95f4\u9694\u3001\u8bbe\u5b9a\u81ea\u5b9a\u4e49\u523b\u5ea6\u7b49\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u76f4\u65b9\u56fe\u5e76\u8bbe\u7f6e\u5750\u6807\u7684\u5206\u5ea6\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u6570\u636e\u51c6\u5907<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236\u76f4\u65b9\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u5e76\u51c6\u5907\u597d\u6570\u636e\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecmatplotlib\u548cnumpy\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<h2><strong>\u751f\u6210\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randn(1000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u7ed8\u5236\u76f4\u65b9\u56fe<\/p>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u5e93\u7684hist\u51fd\u6570\u6765\u7ed8\u5236\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.hist(data, bins=30, edgecolor=&#39;black&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Histogram of Data&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u8bbe\u7f6eX\u8f74\u7684\u5206\u5ea6\u503c<\/p>\n<\/p>\n<p><p>\u4f7f\u7528xticks\u51fd\u6570\u6765\u8bbe\u7f6eX\u8f74\u7684\u523b\u5ea6\u4f4d\u7f6e\u548c\u6807\u7b7e\u3002\u53ef\u4ee5\u901a\u8fc7linspace\u51fd\u6570\u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u523b\u5ea6\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6eX\u8f74\u7684\u523b\u5ea6\u4f4d\u7f6e\u548c\u6807\u7b7e<\/p>\n<p>x_ticks = np.linspace(min(data), max(data), num=10)  # \u751f\u621010\u4e2a\u5747\u5300\u5206\u5e03\u7684\u523b\u5ea6<\/p>\n<p>plt.xticks(x_ticks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u8bbe\u7f6eY\u8f74\u7684\u5206\u5ea6\u503c<\/p>\n<\/p>\n<p><p>\u4f7f\u7528yticks\u51fd\u6570\u6765\u8bbe\u7f6eY\u8f74\u7684\u523b\u5ea6\u4f4d\u7f6e\u548c\u6807\u7b7e\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528linspace\u51fd\u6570\u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u523b\u5ea6\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6eY\u8f74\u7684\u523b\u5ea6\u4f4d\u7f6e\u548c\u6807\u7b7e<\/p>\n<p>y_ticks = np.linspace(0, 100, num=11)  # \u751f\u621011\u4e2a\u5747\u5300\u5206\u5e03\u7684\u523b\u5ea6<\/p>\n<p>plt.yticks(y_ticks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u663e\u793a\u56fe\u50cf<\/p>\n<\/p>\n<p><p>\u4f7f\u7528show\u51fd\u6570\u6765\u663e\u793a\u7ed8\u5236\u7684\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5230\u6b64\u4e3a\u6b62\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u76f4\u65b9\u56fe\u5e76\u8bbe\u7f6e\u5750\u6807\u7684\u5206\u5ea6\u503c\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u8fdb\u4e00\u6b65\u5b9a\u5236\u76f4\u65b9\u56fe\u7684\u5916\u89c2\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u7ed8\u5236\u76f4\u65b9\u56fe\u7684\u57fa\u672c\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u76f4\u65b9\u56fe\u65f6\uff0c\u9664\u4e86\u57fa\u672c\u7684hist\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6765\u5b9a\u5236\u76f4\u65b9\u56fe\u7684\u5916\u89c2\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u76f4\u65b9\u56fe\u7684\u989c\u8272\u3001\u8fb9\u6846\u989c\u8272\u3001\u900f\u660e\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.hist(data, bins=30, color=&#39;blue&#39;, edgecolor=&#39;black&#39;, alpha=0.7)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Customized Histogram of Data&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u81ea\u5b9a\u4e49X\u8f74\u548cY\u8f74\u7684\u523b\u5ea6\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u7684\u523b\u5ea6\u6807\u7b7e\u53ef\u80fd\u4e0d\u7b26\u5408\u9700\u6c42\uff0c\u53ef\u4ee5\u901a\u8fc7xticks\u548cyticks\u51fd\u6570\u81ea\u5b9a\u4e49\u523b\u5ea6\u6807\u7b7e\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6eX\u8f74\u548cY\u8f74\u7684\u523b\u5ea6\u6807\u7b7e\u4e3a\u7279\u5b9a\u7684\u6570\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u5b9a\u4e49X\u8f74\u7684\u523b\u5ea6\u6807\u7b7e<\/p>\n<p>x_ticks_custom = np.arange(-3, 4, 1)  # \u8bbe\u7f6e\u4ece-3\u52303\uff0c\u6bcf\u96941\u4e2a\u5355\u4f4d<\/p>\n<p>plt.xticks(x_ticks_custom)<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49Y\u8f74\u7684\u523b\u5ea6\u6807\u7b7e<\/strong><\/h2>\n<p>y_ticks_custom = [0, 20, 40, 60, 80, 100]<\/p>\n<p>plt.yticks(y_ticks_custom)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Logarithmic Scale<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5177\u6709\u8f83\u5927\u8303\u56f4\u7684\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5bf9\u6570\u523b\u5ea6\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6exscale\u548cyscale\u53c2\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.hist(data, bins=30, edgecolor=&#39;black&#39;)<\/p>\n<p>plt.yscale(&#39;log&#39;)  # \u8bbe\u7f6eY\u8f74\u4e3a\u5bf9\u6570\u523b\u5ea6<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency (log scale)&#39;)<\/p>\n<p>plt.title(&#39;Histogram with Logarithmic Y-Axis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u53ccY\u8f74\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5728\u540c\u4e00\u56fe\u8868\u4e2d\u663e\u793a\u4e24\u4e2aY\u8f74\uff0c\u53ef\u4ee5\u4f7f\u7528twinx\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5171\u4eabX\u8f74\u4f46\u5177\u6709\u4e0d\u540cY\u8f74\u7684\u56fe\u8868\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u663e\u793a\u9891\u7387\u548c\u7d2f\u79ef\u9891\u7387\u7684\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u7b2c\u4e00\u4e2aY\u8f74\u7684\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>ax1.hist(data, bins=30, edgecolor=&#39;black&#39;, alpha=0.7)<\/p>\n<p>ax1.set_xlabel(&#39;Value&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Frequency&#39;, color=&#39;blue&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5171\u4eabX\u8f74\u7684\u7b2c\u4e8c\u4e2aY\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.hist(data, bins=30, edgecolor=&#39;black&#39;, cumulative=True, histtype=&#39;step&#39;, color=&#39;red&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Cumulative Frequency&#39;, color=&#39;red&#39;)<\/p>\n<p>plt.title(&#39;Histogram with Dual Y-Axis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6dfb\u52a0\u7f51\u683c\u548c\u6ce8\u91ca<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u56fe\u8868\u7684\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u6dfb\u52a0\u7f51\u683c\u548c\u6ce8\u91ca\u3002\u4f7f\u7528grid\u51fd\u6570\u53ef\u4ee5\u6dfb\u52a0\u7f51\u683c\uff0c\u4f7f\u7528annotate\u51fd\u6570\u53ef\u4ee5\u6dfb\u52a0\u6ce8\u91ca\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.hist(data, bins=30, edgecolor=&#39;black&#39;, alpha=0.7)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Histogram with Grid and Annotations&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u7f51\u683c<\/strong><\/h2>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u6dfb\u52a0\u6ce8\u91ca<\/strong><\/h2>\n<p>plt.annotate(&#39;Peak&#39;, xy=(0, 50), xytext=(1, 80),<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4fdd\u5b58\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u5b8c\u56fe\u50cf\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528savefig\u51fd\u6570\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002\u53ef\u4ee5\u9009\u62e9\u4fdd\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5982PNG\u3001PDF\u3001SVG\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.hist(data, bins=30, edgecolor=&#39;black&#39;, alpha=0.7)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Histogram of Data&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>plt.savefig(&#39;histogram.png&#39;, dpi=300, bbox_inches=&#39;tight&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed8\u5236\u5e26\u6709\u5bc6\u5ea6\u66f2\u7ebf\u7684\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\uff0c\u53ef\u4ee5\u5728\u76f4\u65b9\u56fe\u4e0a\u53e0\u52a0\u5bc6\u5ea6\u66f2\u7ebf\u3002\u4f7f\u7528seaborn\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>sns.histplot(data, bins=30, kde=True, color=&#39;blue&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Histogram with Density Curve&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4ea4\u4e92\u5f0f\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4ea4\u4e92\u5f0f\u56fe\u8868\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002\u4f7f\u7528plotly\u5e93\u53ef\u4ee5\u521b\u5efa\u4ea4\u4e92\u5f0f\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<p>fig = px.histogram(data, nbins=30, title=&#39;Interactive Histogram of Data&#39;)<\/p>\n<p>fig.update_xaxes(title_text=&#39;Value&#39;)<\/p>\n<p>fig.update_yaxes(title_text=&#39;Frequency&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u7ed8\u5236\u5206\u7ec4\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u6bd4\u8f83\u591a\u4e2a\u6570\u636e\u96c6\u7684\u5206\u5e03\u65f6\uff0c\u53ef\u4ee5\u7ed8\u5236\u5206\u7ec4\u76f4\u65b9\u56fe\u3002\u4f7f\u7528matplotlib\u5e93\u7684hist\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u591a\u4e2a\u6570\u636e\u96c6\u7684\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data1 = np.random.randn(1000)<\/p>\n<p>data2 = np.random.randn(1000) + 1<\/p>\n<p>plt.hist([data1, data2], bins=30, edgecolor=&#39;black&#39;, alpha=0.7, label=[&#39;Data1&#39;, &#39;Data2&#39;])<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Grouped Histogram of Data&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u7ed8\u5236\u5806\u53e0\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u5806\u53e0\u76f4\u65b9\u56fe\u53ef\u4ee5\u5c55\u793a\u591a\u4e2a\u6570\u636e\u96c6\u7684\u7d2f\u79ef\u9891\u7387\u3002\u4f7f\u7528matplotlib\u5e93\u7684hist\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u5806\u53e0\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.hist([data1, data2], bins=30, edgecolor=&#39;black&#39;, alpha=0.7, stacked=True, label=[&#39;Data1&#39;, &#39;Data2&#39;])<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Stacked Histogram of Data&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u7ed8\u5236\u591a\u7ef4\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5c55\u793a\u591a\u7ef4\u6570\u636e\u7684\u5206\u5e03\u65f6\uff0c\u53ef\u4ee5\u7ed8\u5236\u591a\u7ef4\u76f4\u65b9\u56fe\u3002\u4f7f\u7528matplotlib\u5e93\u7684hist2d\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u4e8c\u7ef4\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.random.randn(1000)<\/p>\n<p>y = np.random.randn(1000)<\/p>\n<p>plt.hist2d(x, y, bins=30, cmap=&#39;Blues&#39;)<\/p>\n<p>plt.xlabel(&#39;X Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Y Value&#39;)<\/p>\n<p>plt.title(&#39;2D Histogram of Data&#39;)<\/p>\n<p>plt.colorbar(label=&#39;Frequency&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u7ed8\u5236\u6781\u5750\u6807\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u6781\u5750\u6807\u76f4\u65b9\u56fe\u53ef\u4ee5\u5c55\u793a\u89d2\u5ea6\u6570\u636e\u7684\u5206\u5e03\u3002\u4f7f\u7528matplotlib\u5e93\u7684polar\u53c2\u6570\u53ef\u4ee5\u7ed8\u5236\u6781\u5750\u6807\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">theta = 2 * np.pi * np.random.rand(1000)<\/p>\n<p>plt.subplot(projection=&#39;polar&#39;)<\/p>\n<p>plt.hist(theta, bins=30, edgecolor=&#39;black&#39;, alpha=0.7)<\/p>\n<p>plt.title(&#39;Polar Histogram of Data&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5728\u76f4\u65b9\u56fe\u4e0a\u7ed8\u5236\u62df\u5408\u66f2\u7ebf<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c55\u793a\u6570\u636e\u7684\u62df\u5408\u6548\u679c\uff0c\u53ef\u4ee5\u5728\u76f4\u65b9\u56fe\u4e0a\u53e0\u52a0\u62df\u5408\u66f2\u7ebf\u3002\u4f7f\u7528scipy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u62df\u5408\u6570\u636e\u5e76\u7ed8\u5236\u62df\u5408\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import norm<\/p>\n<h2><strong>\u62df\u5408\u6570\u636e<\/strong><\/h2>\n<p>mu, std = norm.fit(data)<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.hist(data, bins=30, density=True, edgecolor=&#39;black&#39;, alpha=0.7)<\/p>\n<h2><strong>\u7ed8\u5236\u62df\u5408\u66f2\u7ebf<\/strong><\/h2>\n<p>xmin, xmax = plt.xlim()<\/p>\n<p>x = np.linspace(xmin, xmax, 100)<\/p>\n<p>p = norm.pdf(x, mu, std)<\/p>\n<p>plt.plot(x, p, &#39;k&#39;, linewidth=2)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Density&#39;)<\/p>\n<p>plt.title(&#39;Histogram with Fitted Curve&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u7ed8\u5236\u5e26\u6709\u8bef\u5dee\u6761\u7684\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c55\u793a\u6570\u636e\u7684\u4e0d\u786e\u5b9a\u6027\uff0c\u53ef\u4ee5\u5728\u76f4\u65b9\u56fe\u4e0a\u6dfb\u52a0\u8bef\u5dee\u6761\u3002\u4f7f\u7528matplotlib\u5e93\u7684errorbar\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u8bef\u5dee\u6761\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counts, bins, patches = plt.hist(data, bins=30, edgecolor=&#39;black&#39;, alpha=0.7)<\/p>\n<p>bin_centers = 0.5 * (bins[:-1] + bins[1:])<\/p>\n<p>errors = np.sqrt(counts)<\/p>\n<h2><strong>\u6dfb\u52a0\u8bef\u5dee\u6761<\/strong><\/h2>\n<p>plt.errorbar(bin_centers, counts, yerr=errors, fmt=&#39;o&#39;, color=&#39;black&#39;, capsize=5)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Histogram with Error Bars&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u548c\u5176\u4ed6\u8f85\u52a9\u5e93\uff08\u5982seaborn\u3001plotly\u3001scipy\uff09\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u76f4\u65b9\u56fe\uff0c\u5e76\u8bbe\u7f6e\u5750\u6807\u7684\u5206\u5ea6\u503c\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u7ed8\u56fe\u65b9\u6cd5\u548c\u53c2\u6570\uff0c\u5b9a\u5236\u51fa\u7b26\u5408\u9700\u6c42\u7684\u76f4\u65b9\u56fe\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u548c\u89e3\u91ca\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u7ed8\u5236\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u76f4\u65b9\u56fe\u5e76\u81ea\u5b9a\u4e49\u5750\u6807\u7684\u5206\u5ea6\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u76f4\u65b9\u56fe\u3002\u4e3a\u4e86\u81ea\u5b9a\u4e49\u5750\u6807\u7684\u5206\u5ea6\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.xticks()<\/code>\u548c<code>plt.yticks()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6eX\u8f74\u548cY\u8f74\u7684\u523b\u5ea6\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u751f\u6210\u76f4\u65b9\u56fe\u540e\uff0c\u8c03\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u5e76\u4f20\u5165\u6240\u9700\u7684\u523b\u5ea6\u503c\u5217\u8868\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ed8\u5236\u76f4\u65b9\u56fe\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5206\u7bb1\uff08bins\uff09\u6570\u91cf\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u5206\u7bb1\u6570\u91cf\u5bf9\u4e8e\u76f4\u65b9\u56fe\u7684\u53ef\u8bfb\u6027\u548c\u6570\u636e\u5206\u6790\u81f3\u5173\u91cd\u8981\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u9009\u62e9\u5206\u7bb1\u6570\u91cf\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Sturges\u516c\u5f0f\u3001\u5e73\u65b9\u6839\u6cd5\u6216Freedman-Diaconis\u89c4\u5219\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8ba1\u7b97\u51fa\u4e00\u4e2a\u5408\u7406\u7684\u5206\u7bb1\u6570\u91cf\uff0c\u4ece\u800c\u4f7f\u76f4\u65b9\u56fe\u66f4\u5177\u4ee3\u8868\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u76f4\u65b9\u56fe\u4e2d\u6dfb\u52a0\u56fe\u4f8b\u6216\u6807\u7b7e\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u76f4\u65b9\u56fe\u65f6\uff0c\u6dfb\u52a0\u56fe\u4f8b\u6216\u6807\u7b7e\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u53ef\u4ee5\u4f7f\u7528<code>plt.legend()<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u56fe\u4f8b\uff0c\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u96c6\u6216\u7c7b\u522b\u63d0\u4f9b\u8bf4\u660e\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.xlabel()<\/code>\u548c<code>plt.ylabel()<\/code>\u51fd\u6570\u4e3aX\u8f74\u548cY\u8f74\u6dfb\u52a0\u6807\u7b7e\uff0c\u8fd9\u6837\u89c2\u4f17\u5c31\u80fd\u66f4\u6e05\u695a\u5730\u7406\u89e3\u56fe\u8868\u6240\u4f20\u8fbe\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u76f4\u65b9\u56fe\u65f6\uff0c\u8bbe\u7f6e\u5750\u6807\u7684\u5206\u5ea6\u503c\u4e3b\u8981\u901a\u8fc7xticks\u548cyticks\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1097979,"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\/1097975"}],"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=1097975"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097975\/revisions"}],"predecessor-version":[{"id":1097980,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097975\/revisions\/1097980"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097979"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}