{"id":1117204,"date":"2025-01-08T18:23:50","date_gmt":"2025-01-08T10:23:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1117204.html"},"modified":"2025-01-08T18:23:53","modified_gmt":"2025-01-08T10:23:53","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e7%bb%98%e5%88%b6%e7%bd%91%e7%bb%9c%e7%bb%93%e6%9e%84%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1117204.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081223\/3391d55c-c3cf-4481-bb81-fdfb65080ed8.webp\" alt=\"\u5982\u4f55\u4f7f\u7528python\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\uff0c\u6838\u5fc3\u8981\u70b9\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u7406\u89e3\u7f51\u7edc\u56fe\u7684\u57fa\u672c\u6982\u5ff5\u3001\u6570\u636e\u51c6\u5907\u3001\u7ed8\u5236\u548c\u7f8e\u5316\u56fe\u5f62\u3002<\/strong> \u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u5e93\u6709\u4e0d\u540c\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5e38\u7528\u7684\u5e93\u6709NetworkX\u3001Graphviz\u548cMatplotlib\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h3>\n<\/p>\n<p><p>Python\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u5e93\u53ef\u7528\u4e8e\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u6709NetworkX\u3001Graphviz\u548cMatplotlib\u3002<\/p>\n<\/p>\n<p><h4>1\u3001NetworkX<\/h4>\n<\/p>\n<p><p>NetworkX\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u7814\u7a76\u590d\u6742\u7f51\u7edc\u7ed3\u6784\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u7684\u7b97\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u5404\u79cd\u7f51\u7edc\u56fe\u3002NetworkX\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u6613\u7528\u6027\u548c\u7075\u6d3b\u6027\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0eMatplotlib\u7b49\u5176\u4ed6\u53ef\u89c6\u5316\u5e93\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>2\u3001Graphviz<\/h4>\n<\/p>\n<p><p>Graphviz\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u56fe\u5f62\u53ef\u89c6\u5316\u8f6f\u4ef6\uff0c\u652f\u6301\u591a\u79cd\u56fe\u5f62\u5e03\u5c40\u7b97\u6cd5\uff0c\u53ef\u4ee5\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u7f51\u7edc\u56fe\u3002Graphviz\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5f3a\u5927\u7684\u5e03\u5c40\u80fd\u529b\u548c\u4e30\u5bcc\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u4f46\u4f7f\u7528\u8d77\u6765\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u4e00\u5b9a\u7684\u5b66\u4e60\u6210\u672c\u3002<\/p>\n<\/p>\n<p><h4>3\u3001Matplotlib<\/h4>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u867d\u7136\u5b83\u4e0d\u662f\u4e13\u95e8\u4e3a\u7ed8\u5236\u7f51\u7edc\u56fe\u8bbe\u8ba1\u7684\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4e0eNetworkX\u7ed3\u5408\u4f7f\u7528\u6765\u7ed8\u5236\u7b80\u5355\u7684\u7f51\u7edc\u56fe\u3002Matplotlib\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u901a\u7528\u6027\u548c\u5f3a\u5927\u7684\u56fe\u5f62\u5b9a\u5236\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7406\u89e3\u7f51\u7edc\u56fe\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u7f51\u7edc\u56fe\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u7406\u89e3\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\uff0c\u5982\u8282\u70b9\uff08Node\uff09\u3001\u8fb9\uff08Edge\uff09\u3001\u6743\u91cd\uff08Weight\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8282\u70b9\uff08Node\uff09<\/h4>\n<\/p>\n<p><p>\u8282\u70b9\u662f\u7f51\u7edc\u56fe\u7684\u57fa\u672c\u5355\u5143\uff0c\u8868\u793a\u7f51\u7edc\u4e2d\u7684\u5b9e\u4f53\u3002\u5728\u793e\u4ea4\u7f51\u7edc\u4e2d\uff0c\u8282\u70b9\u53ef\u4ee5\u8868\u793a\u7528\u6237\uff1b\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\uff0c\u8282\u70b9\u53ef\u4ee5\u8868\u793a\u8bbe\u5907\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8fb9\uff08Edge\uff09<\/h4>\n<\/p>\n<p><p>\u8fb9\u662f\u8fde\u63a5\u8282\u70b9\u7684\u7ebf\uff0c\u8868\u793a\u8282\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5728\u793e\u4ea4\u7f51\u7edc\u4e2d\uff0c\u8fb9\u53ef\u4ee5\u8868\u793a\u7528\u6237\u4e4b\u95f4\u7684\u53cb\u8c0a\u5173\u7cfb\uff1b\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\uff0c\u8fb9\u53ef\u4ee5\u8868\u793a\u8bbe\u5907\u4e4b\u95f4\u7684\u901a\u4fe1\u94fe\u8def\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6743\u91cd\uff08Weight\uff09<\/h4>\n<\/p>\n<p><p>\u6743\u91cd\u662f\u8fb9\u7684\u5c5e\u6027\uff0c\u8868\u793a\u8282\u70b9\u4e4b\u95f4\u5173\u7cfb\u7684\u5f3a\u5ea6\u3002\u5728\u793e\u4ea4\u7f51\u7edc\u4e2d\uff0c\u6743\u91cd\u53ef\u4ee5\u8868\u793a\u7528\u6237\u4e4b\u95f4\u7684\u4e92\u52a8\u9891\u7387\uff1b\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\uff0c\u6743\u91cd\u53ef\u4ee5\u8868\u793a\u901a\u4fe1\u94fe\u8def\u7684\u5e26\u5bbd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u636e\u51c6\u5907<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u7f51\u7edc\u56fe\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u591a\u79cd\u6765\u6e90\uff0c\u5982\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001API\u7b49\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6570\u636e\u9700\u8981\u5305\u62ec\u8282\u70b9\u548c\u8fb9\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8282\u70b9\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8282\u70b9\u6570\u636e\u901a\u5e38\u5305\u62ec\u8282\u70b9\u7684ID\u548c\u5c5e\u6027\uff0c\u5982\u540d\u79f0\u3001\u7c7b\u578b\u7b49\u3002\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5728CSV\u3001JSON\u7b49\u683c\u5f0f\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8fb9\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8fb9\u6570\u636e\u901a\u5e38\u5305\u62ec\u8d77\u59cb\u8282\u70b9\u548c\u76ee\u6807\u8282\u70b9\u7684ID\uff0c\u4ee5\u53ca\u8fb9\u7684\u5c5e\u6027\uff0c\u5982\u6743\u91cd\u3001\u7c7b\u578b\u7b49\u3002\u6570\u636e\u540c\u6837\u53ef\u4ee5\u5b58\u50a8\u5728CSV\u3001JSON\u7b49\u683c\u5f0f\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u7f51\u7edc\u56fe<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528NetworkX\u7ed8\u5236\u7f51\u7edc\u56fe<\/h4>\n<\/p>\n<p><p>NetworkX\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u7f51\u7edc\u56fe\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528NetworkX\u7ed8\u5236\u7f51\u7edc\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u65e0\u5411\u56fe<\/strong><\/h2>\n<p>G = nx.Graph()<\/p>\n<h2><strong>\u6dfb\u52a0\u8282\u70b9<\/strong><\/h2>\n<p>G.add_node(1, label=&#39;A&#39;)<\/p>\n<p>G.add_node(2, label=&#39;B&#39;)<\/p>\n<p>G.add_node(3, label=&#39;C&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u8fb9<\/strong><\/h2>\n<p>G.add_edge(1, 2, weight=1.0)<\/p>\n<p>G.add_edge(2, 3, weight=2.0)<\/p>\n<p>G.add_edge(3, 1, weight=3.0)<\/p>\n<h2><strong>\u7ed8\u5236\u7f51\u7edc\u56fe<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)  # \u9009\u62e9\u5e03\u5c40\u7b97\u6cd5<\/p>\n<p>nx.draw(G, pos, with_labels=True, node_size=700, node_color=&#39;lightblue&#39;, font_size=10)<\/p>\n<p>labels = nx.get_edge_attributes(G, &#39;weight&#39;)<\/p>\n<p>nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Graphviz\u7ed8\u5236\u7f51\u7edc\u56fe<\/h4>\n<\/p>\n<p><p>Graphviz\u53ef\u4ee5\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u7f51\u7edc\u56fe\uff0c\u4f46\u9700\u8981\u5b89\u88c5Graphviz\u8f6f\u4ef6\u548cPython\u63a5\u53e3\u5e93\uff08graphviz\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from graphviz import Digraph<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6709\u5411\u56fe<\/strong><\/h2>\n<p>dot = Digraph()<\/p>\n<h2><strong>\u6dfb\u52a0\u8282\u70b9<\/strong><\/h2>\n<p>dot.node(&#39;A&#39;)<\/p>\n<p>dot.node(&#39;B&#39;)<\/p>\n<p>dot.node(&#39;C&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u8fb9<\/strong><\/h2>\n<p>dot.edge(&#39;A&#39;, &#39;B&#39;, label=&#39;1.0&#39;)<\/p>\n<p>dot.edge(&#39;B&#39;, &#39;C&#39;, label=&#39;2.0&#39;)<\/p>\n<p>dot.edge(&#39;C&#39;, &#39;A&#39;, label=&#39;3.0&#39;)<\/p>\n<h2><strong>\u6e32\u67d3\u5e76\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>dot.render(&#39;network_graph&#39;, format=&#39;png&#39;, view=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed3\u5408NetworkX\u548cMatplotlib\u7ed8\u5236\u7f51\u7edc\u56fe<\/h4>\n<\/p>\n<p><p>NetworkX\u53ef\u4ee5\u4e0eMatplotlib\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u4e30\u5bcc\u7684\u56fe\u5f62\u5b9a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u65e0\u5411\u56fe<\/strong><\/h2>\n<p>G = nx.Graph()<\/p>\n<h2><strong>\u6dfb\u52a0\u8282\u70b9<\/strong><\/h2>\n<p>G.add_node(1, label=&#39;A&#39;)<\/p>\n<p>G.add_node(2, label=&#39;B&#39;)<\/p>\n<p>G.add_node(3, label=&#39;C&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u8fb9<\/strong><\/h2>\n<p>G.add_edge(1, 2, weight=1.0)<\/p>\n<p>G.add_edge(2, 3, weight=2.0)<\/p>\n<p>G.add_edge(3, 1, weight=3.0)<\/p>\n<h2><strong>\u7ed8\u5236\u7f51\u7edc\u56fe<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)  # \u9009\u62e9\u5e03\u5c40\u7b97\u6cd5<\/p>\n<p>nx.draw(G, pos, with_labels=True, node_size=700, node_color=&#39;lightblue&#39;, font_size=10)<\/p>\n<p>labels = nx.get_edge_attributes(G, &#39;weight&#39;)<\/p>\n<p>nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)<\/p>\n<p>plt.title(&#39;Network Graph&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7f8e\u5316\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u57fa\u672c\u7684\u7f51\u7edc\u56fe\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u7f8e\u5316\u56fe\u5f62\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8c03\u6574\u8282\u70b9\u548c\u8fb9\u7684\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8282\u70b9\u548c\u8fb9\u7684\u989c\u8272\u3001\u5927\u5c0f\u3001\u5f62\u72b6\u7b49\u5c5e\u6027\u6765\u7f8e\u5316\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u989c\u8272\u8868\u793a\u4e0d\u540c\u7c7b\u578b\u7684\u8282\u70b9\uff0c\u4f7f\u7528\u4e0d\u540c\u5bbd\u5ea6\u8868\u793a\u4e0d\u540c\u6743\u91cd\u7684\u8fb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u65e0\u5411\u56fe<\/strong><\/h2>\n<p>G = nx.Graph()<\/p>\n<h2><strong>\u6dfb\u52a0\u8282\u70b9<\/strong><\/h2>\n<p>G.add_node(1, label=&#39;A&#39;, color=&#39;red&#39;)<\/p>\n<p>G.add_node(2, label=&#39;B&#39;, color=&#39;green&#39;)<\/p>\n<p>G.add_node(3, label=&#39;C&#39;, color=&#39;blue&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u8fb9<\/strong><\/h2>\n<p>G.add_edge(1, 2, weight=1.0, color=&#39;black&#39;)<\/p>\n<p>G.add_edge(2, 3, weight=2.0, color=&#39;gray&#39;)<\/p>\n<p>G.add_edge(3, 1, weight=3.0, color=&#39;purple&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u7f51\u7edc\u56fe<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)  # \u9009\u62e9\u5e03\u5c40\u7b97\u6cd5<\/p>\n<p>node_colors = [G.nodes[n][&#39;color&#39;] for n in G.nodes()]<\/p>\n<p>edge_colors = [G.edges[e][&#39;color&#39;] for e in G.edges()]<\/p>\n<p>nx.draw(G, pos, with_labels=True, node_size=700, node_color=node_colors, font_size=10, edge_color=edge_colors)<\/p>\n<p>labels = nx.get_edge_attributes(G, &#39;weight&#39;)<\/p>\n<p>nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)<\/p>\n<p>plt.title(&#39;Colored Network Graph&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8c03\u6574\u5e03\u5c40\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u5e03\u5c40\u7b97\u6cd5\u53ef\u4ee5\u4f7f\u7f51\u7edc\u56fe\u66f4\u6e05\u6670\u6613\u8bfb\u3002NetworkX\u63d0\u4f9b\u4e86\u591a\u79cd\u5e03\u5c40\u7b97\u6cd5\uff0c\u5982spring_layout\u3001circular_layout\u3001shell_layout\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u65e0\u5411\u56fe<\/strong><\/h2>\n<p>G = nx.Graph()<\/p>\n<h2><strong>\u6dfb\u52a0\u8282\u70b9<\/strong><\/h2>\n<p>G.add_node(1, label=&#39;A&#39;)<\/p>\n<p>G.add_node(2, label=&#39;B&#39;)<\/p>\n<p>G.add_node(3, label=&#39;C&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u8fb9<\/strong><\/h2>\n<p>G.add_edge(1, 2, weight=1.0)<\/p>\n<p>G.add_edge(2, 3, weight=2.0)<\/p>\n<p>G.add_edge(3, 1, weight=3.0)<\/p>\n<h2><strong>\u7ed8\u5236\u7f51\u7edc\u56fe<\/strong><\/h2>\n<h2><strong>\u9009\u62e9\u4e0d\u540c\u7684\u5e03\u5c40\u7b97\u6cd5<\/strong><\/h2>\n<p>pos_spring = nx.spring_layout(G)<\/p>\n<p>pos_circular = nx.circular_layout(G)<\/p>\n<p>pos_shell = nx.shell_layout(G)<\/p>\n<h2><strong>\u4f7f\u7528\u4e0d\u540c\u7684\u5e03\u5c40\u7ed8\u5236\u7f51\u7edc\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(12, 4))<\/p>\n<p>plt.subplot(131)<\/p>\n<p>nx.draw(G, pos_spring, with_labels=True, node_size=700, node_color=&#39;lightblue&#39;, font_size=10)<\/p>\n<p>plt.title(&#39;Spring Layout&#39;)<\/p>\n<p>plt.subplot(132)<\/p>\n<p>nx.draw(G, pos_circular, with_labels=True, node_size=700, node_color=&#39;lightgreen&#39;, font_size=10)<\/p>\n<p>plt.title(&#39;Circular Layout&#39;)<\/p>\n<p>plt.subplot(133)<\/p>\n<p>nx.draw(G, pos_shell, with_labels=True, node_size=700, node_color=&#39;lightcoral&#39;, font_size=10)<\/p>\n<p>plt.title(&#39;Shell Layout&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u4e3a\u8282\u70b9\u548c\u8fb9\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca\u53ef\u4ee5\u5e2e\u52a9\u7406\u89e3\u7f51\u7edc\u56fe\u4e2d\u7684\u4fe1\u606f\u3002NetworkX\u548cMatplotlib\u90fd\u63d0\u4f9b\u4e86\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import networkx as nx<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u65e0\u5411\u56fe<\/strong><\/h2>\n<p>G = nx.Graph()<\/p>\n<h2><strong>\u6dfb\u52a0\u8282\u70b9<\/strong><\/h2>\n<p>G.add_node(1, label=&#39;A&#39;)<\/p>\n<p>G.add_node(2, label=&#39;B&#39;)<\/p>\n<p>G.add_node(3, label=&#39;C&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u8fb9<\/strong><\/h2>\n<p>G.add_edge(1, 2, weight=1.0)<\/p>\n<p>G.add_edge(2, 3, weight=2.0)<\/p>\n<p>G.add_edge(3, 1, weight=3.0)<\/p>\n<h2><strong>\u7ed8\u5236\u7f51\u7edc\u56fe<\/strong><\/h2>\n<p>pos = nx.spring_layout(G)  # \u9009\u62e9\u5e03\u5c40\u7b97\u6cd5<\/p>\n<p>nx.draw(G, pos, with_labels=True, node_size=700, node_color=&#39;lightblue&#39;, font_size=10)<\/p>\n<p>labels = nx.get_edge_attributes(G, &#39;weight&#39;)<\/p>\n<p>nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)<\/p>\n<h2><strong>\u6dfb\u52a0\u6ce8\u91ca<\/strong><\/h2>\n<p>for node in G.nodes(data=True):<\/p>\n<p>    x, y = pos[node[0]]<\/p>\n<p>    plt.text(x, y+0.1, f&quot;Node {node[1][&#39;label&#39;]}&quot;, fontsize=12, ha=&#39;center&#39;)<\/p>\n<p>plt.title(&#39;Network Graph with Annotations&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\u53ef\u4ee5\u9009\u62e9\u591a\u79cd\u5e93\uff0c\u5982NetworkX\u3001Graphviz\u548cMatplotlib\u3002\u7406\u89e3\u7f51\u7edc\u56fe\u7684\u57fa\u672c\u6982\u5ff5\u548c\u51c6\u5907\u597d\u6570\u636e\u662f\u7ed8\u5236\u7f51\u7edc\u56fe\u7684\u524d\u63d0\u3002\u7ed8\u5236\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u8282\u70b9\u548c\u8fb9\u7684\u6837\u5f0f\u3001\u9009\u62e9\u5408\u9002\u7684\u5e03\u5c40\u7b97\u6cd5\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca\u7b49\u65b9\u5f0f\u7f8e\u5316\u56fe\u5f62\u3002\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u7ed8\u5236\u51fa\u6e05\u6670\u3001\u7f8e\u89c2\u7684\u7f51\u7edc\u7ed3\u6784\u56fe\uff0c\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5c55\u793a\u590d\u6742\u7f51\u7edc\u4e2d\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\u901a\u5e38\u9700\u8981\u51e0\u4e2a\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u7ed8\u56fe\u5e93\uff0c\u6bd4\u5982NetworkX\u7ed3\u5408Matplotlib\u3002\u5176\u6b21\uff0c\u4f7f\u7528NetworkX\u521b\u5efa\u56fe\u5bf9\u8c61\u5e76\u6dfb\u52a0\u8282\u70b9\u548c\u8fb9\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6765\u8bbe\u7f6e\u56fe\u7684\u5e03\u5c40\u548c\u6837\u5f0f\uff0c\u6700\u540e\u8c03\u7528\u7ed8\u56fe\u51fd\u6570\u6765\u751f\u6210\u5e76\u5c55\u793a\u7f51\u7edc\u7ed3\u6784\u56fe\u3002\u6574\u4e2a\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\uff0c\u9002\u5408\u5404\u79cd\u7c7b\u578b\u7684\u7f51\u7edc\u5206\u6790\u3002<\/p>\n<p><strong>\u5728\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u8282\u70b9\u548c\u8fb9\u7684\u6837\u5f0f\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u8282\u70b9\u548c\u8fb9\u7684\u6837\u5f0f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u5c5e\u6027\u6765\u5b9e\u73b0\u3002\u5728\u4f7f\u7528NetworkX\u65f6\uff0c\u53ef\u4ee5\u4e3a\u8282\u70b9\u6307\u5b9a\u989c\u8272\u3001\u5927\u5c0f\u548c\u5f62\u72b6\uff0c\u800c\u8fb9\u5219\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u6761\u989c\u8272\u548c\u5bbd\u5ea6\u6765\u8fdb\u884c\u81ea\u5b9a\u4e49\u3002\u5229\u7528Matplotlib\u7684\u529f\u80fd\uff0c\u4f60\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca\uff0c\u4ee5\u4fbf\u66f4\u6e05\u6670\u5730\u4f20\u8fbe\u4fe1\u606f\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\uff1f<\/strong><br \/>\u9664\u4e86NetworkX\u548cMatplotlib\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\u3002\u4f8b\u5982\uff0cPlotly\u63d0\u4f9b\u4e86\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u52a8\u6001\u5c55\u793a\u7684\u573a\u666f\uff1bGephi\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7f51\u7edc\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5bfc\u5165Python\u751f\u6210\u7684\u6570\u636e\u8fdb\u884c\u6df1\u5165\u5206\u6790\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe \u4f7f\u7528Python\u7ed8\u5236\u7f51\u7edc\u7ed3\u6784\u56fe\uff0c\u6838\u5fc3\u8981\u70b9\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u7406\u89e3\u7f51\u7edc\u56fe\u7684\u57fa [&hellip;]","protected":false},"author":3,"featured_media":1117214,"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\/1117204"}],"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=1117204"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117204\/revisions"}],"predecessor-version":[{"id":1117215,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117204\/revisions\/1117215"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1117214"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1117204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1117204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1117204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}