{"id":1057050,"date":"2024-12-31T15:04:39","date_gmt":"2024-12-31T07:04:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057050.html"},"modified":"2024-12-31T15:04:41","modified_gmt":"2024-12-31T07:04:41","slug":"python%e5%a6%82%e4%bd%95%e7%bb%98%e5%88%b6%e5%87%a0%e4%b8%87%e6%9d%a1%e6%9b%b2%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057050.html","title":{"rendered":"python\u5982\u4f55\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/26c2f857-858d-4284-9199-3e4eb9dfcccc.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Matplotlib\u3001Plotly\u3001Bokeh\u7b49\u5e93\u3002\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c<strong>Matplotlib\u6027\u80fd\u8f83\u5dee\u3001Plotly\u9002\u5408\u4ea4\u4e92\u5f0f\u56fe\u8868\u3001Bokeh\u5728\u5927\u6570\u636e\u7ed8\u56fe\u65b9\u9762\u8868\u73b0\u51fa\u8272<\/strong>\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u867d\u7136\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u6027\u80fd\u8f83\u5dee\uff0c\u4f46\u901a\u8fc7\u4e00\u4e9b\u6280\u5de7\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165Matplotlib<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Matplotlib\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Matplotlib\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><\/code><\/pre>\n<\/p>\n<p><h4>2. \u751f\u6210\u6570\u636e\u5e76\u7ed8\u5236\u66f2\u7ebf<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u751f\u6210\u51e0\u4e07\u6761\u66f2\u7ebf\u7684\u6570\u636e\uff0c\u5e76\u4f7f\u7528Matplotlib\u7ed8\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u6570\u636e<\/p>\n<p>num_lines = 10000<\/p>\n<p>x = np.linspace(0, 10, 1000)<\/p>\n<p>y = np.sin(x) + np.random.randn(num_lines, x.size) * 0.1<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<h2><strong>\u7ed8\u5236\u66f2\u7ebf<\/strong><\/h2>\n<p>for i in range(num_lines):<\/p>\n<p>    plt.plot(x, y[i, :], alpha=0.1)  # \u4f7f\u7528alpha\u6765\u964d\u4f4e\u900f\u660e\u5ea6<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.xlabel(&#39;X axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y axis&#39;)<\/p>\n<p>plt.title(&#39;Plotting Multiple Lines with Matplotlib&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u63d0\u9ad8Matplotlib\u6027\u80fd\u7684\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u51cf\u5c11\u7ed8\u56fe\u5143\u7d20<\/strong>\uff1a\u5982\u51cf\u5c11\u6570\u636e\u70b9\u6570\u91cf\u3001\u964d\u4f4e\u900f\u660e\u5ea6\uff08alpha\uff09\u3002<\/li>\n<li><strong>\u5206\u6279\u7ed8\u56fe<\/strong>\uff1a\u5c06\u6570\u636e\u5206\u6210\u591a\u4e2a\u6279\u6b21\u7ed8\u5236\u3002<\/li>\n<li><strong>\u4f7f\u7528Agg\u540e\u7aef<\/strong>\uff1a\u53ef\u4ee5\u907f\u514d\u663e\u793a\u7a97\u53e3\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Plotly\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf<\/h3>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684\u53ef\u89c6\u5316\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165Plotly<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Plotly\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install plotly<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Plotly\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objs as go<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u751f\u6210\u6570\u636e\u5e76\u7ed8\u5236\u66f2\u7ebf<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u751f\u6210\u51e0\u4e07\u6761\u66f2\u7ebf\u7684\u6570\u636e\uff0c\u5e76\u4f7f\u7528Plotly\u7ed8\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u6570\u636e<\/p>\n<p>num_lines = 10000<\/p>\n<p>x = np.linspace(0, 10, 1000)<\/p>\n<p>y = np.sin(x) + np.random.randn(num_lines, x.size) * 0.1<\/p>\n<h2><strong>\u521b\u5efa\u66f2\u7ebf\u5217\u8868<\/strong><\/h2>\n<p>data = []<\/p>\n<p>for i in range(num_lines):<\/p>\n<p>    trace = go.Scatter(x=x, y=y[i, :], mode=&#39;lines&#39;, opacity=0.1)<\/p>\n<p>    data.append(trace)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868\u5e03\u5c40<\/strong><\/h2>\n<p>layout = go.Layout(<\/p>\n<p>    title=&#39;Plotting Multiple Lines with Plotly&#39;,<\/p>\n<p>    xaxis=dict(title=&#39;X axis&#39;),<\/p>\n<p>    yaxis=dict(title=&#39;Y axis&#39;)<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = go.Figure(data=data, layout=layout)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u5316Plotly\u6027\u80fd\u7684\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528WebGL<\/strong>\uff1a\u5728\u521b\u5efa\u56fe\u8868\u65f6\u542f\u7528WebGL\u4ee5\u63d0\u9ad8\u6e32\u67d3\u6027\u80fd\u3002<\/li>\n<li><strong>\u7b80\u5316\u6570\u636e<\/strong>\uff1a\u51cf\u5c11\u6570\u636e\u70b9\u6570\u91cf\u3001\u4f7f\u7528\u62bd\u6837\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528Bokeh\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf<\/h3>\n<\/p>\n<p><p>Bokeh\u662f\u4e00\u4e2a\u9002\u5408\u5927\u6570\u636e\u7ed8\u56fe\u7684\u5e93\uff0c\u5177\u6709\u5f3a\u5927\u7684\u4ea4\u4e92\u529f\u80fd\u548c\u4f18\u5f02\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165Bokeh<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Bokeh\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install bokeh<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Bokeh\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, show, output_file<\/p>\n<p>from bokeh.io import output_notebook<\/p>\n<p>import numpy as np<\/p>\n<p>output_notebook()  # \u5728Jupyter Notebook\u4e2d\u4f7f\u7528<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u751f\u6210\u6570\u636e\u5e76\u7ed8\u5236\u66f2\u7ebf<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u751f\u6210\u51e0\u4e07\u6761\u66f2\u7ebf\u7684\u6570\u636e\uff0c\u5e76\u4f7f\u7528Bokeh\u7ed8\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u6570\u636e<\/p>\n<p>num_lines = 10000<\/p>\n<p>x = np.linspace(0, 10, 1000)<\/p>\n<p>y = np.sin(x) + np.random.randn(num_lines, x.size) * 0.1<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>p = figure(title=&quot;Plotting Multiple Lines with Bokeh&quot;, x_axis_label=&#39;X axis&#39;, y_axis_label=&#39;Y axis&#39;, plot_width=800, plot_height=600)<\/p>\n<h2><strong>\u7ed8\u5236\u66f2\u7ebf<\/strong><\/h2>\n<p>for i in range(num_lines):<\/p>\n<p>    p.line(x, y[i, :], line_alpha=0.1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u63d0\u9ad8Bokeh\u6027\u80fd\u7684\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528ColumnDataSource<\/strong>\uff1a\u63d0\u9ad8\u6570\u636e\u4f20\u8f93\u6548\u7387\u3002<\/li>\n<li><strong>\u5206\u5c42\u7ed8\u56fe<\/strong>\uff1a\u5206\u6279\u6b21\u7ed8\u5236\u6570\u636e\u3002<\/li>\n<li><strong>\u7b80\u5316\u6570\u636e<\/strong>\uff1a\u51cf\u5c11\u6570\u636e\u70b9\u6570\u91cf\u3001\u4f7f\u7528\u62bd\u6837\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Matplotlib\u3001Plotly\u3001Bokeh\u7b49\u5e93\u3002<strong>Matplotlib\u6027\u80fd\u8f83\u5dee<\/strong>\uff0c\u4f46\u901a\u8fc7\u4e00\u4e9b\u6280\u5de7\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff1b<strong>Plotly\u9002\u5408\u4ea4\u4e92\u5f0f\u56fe\u8868<\/strong>\uff0c\u53ef\u4ee5\u901a\u8fc7\u542f\u7528WebGL\u548c\u7b80\u5316\u6570\u636e\u6765\u63d0\u9ad8\u6027\u80fd\uff1b<strong>Bokeh\u5728\u5927\u6570\u636e\u7ed8\u56fe\u65b9\u9762\u8868\u73b0\u51fa\u8272<\/strong>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528ColumnDataSource\u548c\u5206\u5c42\u7ed8\u56fe\u7b49\u65b9\u6cd5\u6765\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u6bcf\u79cd\u5e93\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\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\u9ad8\u6548\u7ed8\u5236\u5927\u91cf\u66f2\u7ebf\uff1f<\/strong><br \/>\u5728Python\u4e2d\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u4f18\u5316\u7ed8\u56fe\u8fc7\u7a0b\u81f3\u5173\u91cd\u8981\u3002Matplotlib\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u4f46\u5bf9\u4e8e\u5982\u6b64\u5927\u91cf\u7684\u6570\u636e\uff0c\u4f7f\u7528Matplotlib\u7684<code>plot<\/code>\u51fd\u6570\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u5efa\u8bae\u4f7f\u7528<code>LineCollection<\/code>\u6216<code>FastLine<\/code>\u7b49\u6280\u672f\u6765\u52a0\u901f\u7ed8\u56fe\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5927\u6570\u636e\u53ef\u89c6\u5316\u5e93\u5982Holoviews\u6216Datashader\uff0c\u8fd9\u4e9b\u5e93\u4e13\u4e3a\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u800c\u8bbe\u8ba1\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7ed8\u56fe\u6548\u7387\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ed8\u5236\u66f2\u7ebf\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u5185\u5b58\u4f7f\u7528\uff1f<\/strong><br \/>\u5f53\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf\u65f6\uff0c\u5185\u5b58\u7ba1\u7406\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u5e94\u907f\u514d\u5728\u5185\u5b58\u4e2d\u540c\u65f6\u5b58\u50a8\u6240\u6709\u66f2\u7ebf\u7684\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7\u5206\u6279\u5904\u7406\u548c\u52a8\u6001\u52a0\u8f7d\u6570\u636e\u6765\u7ba1\u7406\u5185\u5b58\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u7b49\u5e93\u4f18\u5316\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u51cf\u5c11\u5185\u5b58\u5360\u7528\u4e5f\u662f\u6709\u6548\u7684\u65b9\u6cd5\u3002\u4f7f\u7528\u751f\u6210\u5668\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63a7\u5236\u5185\u5b58\u6d88\u8017\u3002<\/p>\n<p><strong>\u7ed8\u5236\u5927\u91cf\u66f2\u7ebf\u65f6\uff0c\u5982\u4f55\u63d0\u9ad8\u53ef\u89c6\u5316\u6548\u679c\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u53ef\u89c6\u5316\u6548\u679c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u989c\u8272\u6620\u5c04\u3001\u7ebf\u578b\u53d8\u5316\u548c\u900f\u660e\u5ea6\u7b49\u6280\u672f\u6765\u533a\u5206\u4e0d\u540c\u7684\u66f2\u7ebf\u3002Matplotlib\u652f\u6301\u901a\u8fc7\u8bbe\u7f6e<code>alpha<\/code>\u53c2\u6570\u6765\u8c03\u6574\u66f2\u7ebf\u7684\u900f\u660e\u5ea6\uff0c\u4ece\u800c\u5728\u91cd\u53e0\u7684\u60c5\u51b5\u4e0b\u4fdd\u6301\u53ef\u8bfb\u6027\u3002\u4f7f\u7528\u56fe\u4f8b\u548c\u6ce8\u91ca\u4e3a\u4e0d\u540c\u7684\u66f2\u7ebf\u6dfb\u52a0\u6807\u7b7e\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u63d0\u5347\u53ef\u89c6\u5316\u6548\u679c\u7684\u65b9\u5f0f\u3002\u540c\u65f6\uff0c\u9002\u5f53\u7684\u7f29\u653e\u548c\u5e73\u79fb\u529f\u80fd\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ed8\u5236\u51e0\u4e07\u6761\u66f2\u7ebf\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Matplotlib\u3001Plotly\u3001Bokeh\u7b49\u5e93\u3002\u5728\u5904\u7406\u5927 [&hellip;]","protected":false},"author":3,"featured_media":1057055,"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\/1057050"}],"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=1057050"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057050\/revisions"}],"predecessor-version":[{"id":1057056,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057050\/revisions\/1057056"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1057055"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1057050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1057050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1057050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}