在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
圖形和複雜網路研究是用於建模複雜連結和互動的計算機科學和數學的基本概念。 圖中的節點,有時也稱為頂點,和邊,有時稱為連結,本質上只是實體及其關係的視覺表示和解釋,這些關係通過連接節點的邊來顯示。
更普遍地,所有用於交通系統、社交網路和通信網路的圖表都被認為是網路。 透過觀察圖表和網路,我們可以理解並克服與連接性、流量和網路結構有關的問題。 這類工作提供了不同領域的見解,從社會動態和組織結構到高效率路由和優化的算法。 這些概念在網路理論、運籌學和數據科學中非常核心。
在本文中,我們使用 igraph 展示如何生成網路圖並使用靈活且可靠的 IronPDF 庫將其列印到 PDF 文件中。
Igraph 是一個強大的 Python 包,用於生成、操作和分析複雜的圖形和網絡。 它提供了一個龐大的工具包,從圖形生成到操作及其視覺化處理。 Python igraph通過許多算法進行網路分析的實現,計算各種中心性、最短路徑、社區結構等指標。
因此,該庫提供了具有自適應佈局和屬性化的良好視覺化,適用於有向圖和無向圖。Igraph 非常靈活且快速,常用於分析複雜關係數據的應用中,如數據科學、計算生物學和社交網絡研究。
首先,要在 Python 中進行基本的圖論操作和配置,請按以下步驟自行創建、配置和使用 igraph。
您必須先安裝 igraph 套件。可以使用以下 pip 命令:
pip install igraph
pip install igraph
下面有一個簡單的例子,向您展示如何使用 igraph 構建和設置一個圖形:
from igraph import Graph, plot
# Create an empty graph
g = Graph()
# Add vertices (nodes)
g.add_vertices(5) # Adding 5 vertices
# Add edges (connections between vertices)
g.add_edges([(0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (0, 2)]) # Adding edges
# Add vertex ids and edge attributes
g.vs["name"] = ["A", "B", "C", "D", "E"] # Vertex labels
g.es["weight"] = [1, 2, 3, 4, 5, 6] # Edge weights
# Basic graph structural properties
print("Number of vertices:", g.vcount())
print("Number of edges:", g.ecount())
print("Graph summary:", g.summary())
py
我們可以使用 igraph 的一些內建功能來繪製圖形。 使用以下內容更改外觀和佈局:
# Define a layout for the graph
layout = g.layout("circle") # Layout in a circular arrangement
# Plot the graph with labels and custom options
plot(
g,
layout=layout,
vertex_label=g.vs["name"], # Label vertices
vertex_color="lightblue", # Vertex color
edge_width=g.es["weight"], # Edge width based on weight
vertex_size=30, # Vertex size
edge_color="grey", # Edge color
bbox=(300, 300), # Size of the plot
margin=20 # Margin around the plot
)
g.savefig('exampleGraph.png') # You can also save to many file formats
py
以下是透過 Matplotlib 函式庫生成的簡單圖形影像,以及利用 Cairo 函式庫的 Python 綁定生成的。
執行各種圖形操作和分析,例如計算中心性、尋找社群或識別最短路徑:
# Calculate degree centrality for each vertex
degrees = g.degree()
print("Degrees of vertices:", degrees)
# Compute shortest path between two vertices that dont have a predefined
# distance
shortest_path = g.shortest_paths_dijkstra(source=0, target=3)
print("Shortest path from vertex 0 to 3:", shortest_path)
# Detect communities using the Louvain method
communities = g.community_multilevel()
print("Detected communities:", communities)
py
!igraph python(對開發人員的運作方式):圖5 - IronPDF網頁
我們甚至可以使用IronPDF Python 模組以程式化方式生成和編輯 PDF。 使用這個庫,您將擁有從 HTML 創建 PDF 文件的強大功能,合併兩個或多個 PDF 文件,甚至可以利用現有的 PDF 及修改其以包含文字、照片和註釋。 IronPDF 使您能夠從任何 HTML 網站或 Web 內容生成專業品質的 PDF,這些 PDF 可用於生成具有預設樣式的報告、發票和其他文件。
其中一些高級功能包括編輯頁面佈局、文件加密以及從 PDF 中提取文本。 如果開發者能更好地處理 PDF,將能更好地改進您產品的一般實用性。
您可以使用以下命令來安裝包,這些包允許Python介面為您的項目啟用IronPDF功能:
pip install ironpdf
pip install ironpdf
以下是在 Python 中合併 igraph 和 IronPDF 的步驟:首先,您將使用 igraph 創建一個圖形並顯示它。 然後,將生成的視覺化內容轉換為 PDF。
from igraph import Graph, plot
import matplotlib.pyplot as plt
from ironpdf import * import warnings
warnings.filterwarnings('ignore')
# Ensure that you have replaces the string with your own license key
License.LicenseKey = "YOUR LICENSE KEY GOES HERE";
# Create an empty graph
g = Graph()
# Add adjacent vertices (nodes)
g.add_vertices(5) # Adding 5 vertices
# Add edges (connections between vertices)
g.add_edges([(0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (0, 2)]) # Adding edges
# Add vertex and edge attributes
g.vs["name"] = ["A", "B", "C", "D", "E"] # Vertex labels
g.es["weight"] = [1, 2, 3, 4, 5, 6] # Edge weights
# Define a layout for the graph
layout = g.layout("circle") # Layout in a circular arrangement
# Create a plot using matplotlib
fig, ax = plt.subplots()
# Plot the geometric random graph
plot(
g,
target=ax,
layout=layout,
vertex_label=g.vs["name"], # Label vertices
vertex_color="lightblue", # Vertex color
edge_width=g.es["weight"], # Edge width based on weight
vertex_size=30, # Vertex size
edge_color="grey", # Edge color
bbox=(300, 300), # Size of the plot
margin=20 # Margin around the plot
)
# save in different file formats
plt.savefig('result.png')
ImageToPdfConverter.ImageToPdf('result.png').SaveAs("result.pdf")
py
此腳本將透過 igraph 生成圖表,並使用 matplotlib 進行可視化,然後使用 IronPDF 將圖表轉換為 PDF。 此程式碼將匯入所有需要的庫並使用授權密鑰設置IronPDF。 創建一個具有五個頂點和六條邊的空圖,並添加權重和標籤以增加清晰度。
然後將圖形呈現為圓形,繪製過程涉及使用matplotlib和多個視覺化屬性,例如頂點的顏色和大小,以及邊的線寬。 之後,結果將被儲存為影像檔案 result.png。 最後,使用 IronPDF 的ImageToPdfConverter轉換成 PDF,result.pdf。 圖形創建、視覺化和生成 PDF 結合為一個工作流程。
需要授權金鑰以允許程式碼在無浮水印的情況下運行。 您可以在此連結註冊免費試用許可證。 請注意,您可以註冊一個而不需要提供身份證明。 您只需輸入您的電子郵件地址即可註冊免費試用版。
通過IronPDF和igraph的強大功能,您可以開發解決方案以可視化和展示複雜的圖形數據。 通過 igraph,您可以輕鬆創建和分析複雜的網絡,同時使用 IronPDF 無縫地將數據可視化轉換為專業級 PDF 文件。 這些結合的力量將幫助您開發綜合報告,包括圖形分析和視覺呈現。
此整合可開發各種需要全面網絡文檔的應用程式,包括學術研究、商業分析和數據驅動報告。 此外,它結合了高品質的文件輸出與功能強大的圖形操作能力。最重要的是,Iron Software 提供了多種庫,使開發適用於各種平台和操作系統(如 Windows、Android、MAC、Linux 等)的應用程序更加容易。