|
import networkx as nx |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
|
|
|
|
def plot_G_and_its_complement(G: nx.classes.graph.Graph, save_path="ramsey_graph.png"): |
|
""" |
|
绘制图 G 和其补图 |
|
:param G: 图 G |
|
:param save_path: 保存路径 |
|
:return: None |
|
""" |
|
|
|
Gc = nx.complement(G) |
|
|
|
|
|
pos = nx.spring_layout(G, seed=482) |
|
|
|
|
|
node_colors = plt.cm.Set2(np.linspace(0, 1, len(G.nodes))) |
|
|
|
|
|
edge_color_G = "#FF6B6B" |
|
edge_color_Gc = "#4D79FF" |
|
|
|
|
|
plt.figure(figsize=(14, 7)) |
|
|
|
|
|
plt.rcParams['font.sans-serif'] = ['Heiti SC'] |
|
plt.rcParams['axes.unicode_minus'] = False |
|
|
|
plt.subplot(1, 2, 1) |
|
nx.draw_networkx_nodes(G, pos, node_color=node_colors, node_size=500) |
|
nx.draw_networkx_edges(G, pos, edge_color=edge_color_G, width=2) |
|
nx.draw_networkx_labels(G, pos, font_size=16, font_color='black') |
|
plt.title("图 G", fontdict={'family': 'Heiti SC', 'size': 32}) |
|
plt.axis('off') |
|
|
|
|
|
plt.subplot(1, 2, 2) |
|
nx.draw_networkx_nodes(Gc, pos, node_color=node_colors, node_size=500) |
|
nx.draw_networkx_edges(Gc, pos, edge_color=edge_color_Gc, width=2) |
|
nx.draw_networkx_labels(Gc, pos, font_size=16, font_color='black') |
|
plt.title("图 G 的补图", fontdict={'family': 'Heiti SC', 'size': 32}) |
|
plt.axis('off') |
|
|
|
plt.tight_layout() |
|
plt.savefig(save_path) |
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Gs = nx.read_graph6('raw_data/r44_6.g6') |
|
G: nx.classes.graph.Graph = Gs[10] |
|
plot_G_and_its_complement(G, save_path="ramsey_graph.png") |
|
|