带有颜色的房子#

使用matplotlib绘制图形。

plot house with colors
import matplotlib.pyplot as plt
import networkx as nx

G = nx.house_graph()
# 显式设置位置
pos = {0: (0, 0), 1: (1, 0), 2: (0, 1), 3: (1, 1), 4: (0.5, 2.0)}

# 绘制具有不同属性的节点,用于"墙"和"屋顶"节点
nx.draw_networkx_nodes(
    G, pos, node_size=3000, nodelist=[0, 1, 2, 3], node_color="tab:blue"
)
nx.draw_networkx_nodes(G, pos, node_size=2000, nodelist=[4], node_color="tab:orange")
nx.draw_networkx_edges(G, pos, alpha=0.5, width=6)
# 自定义轴
ax = plt.gca()
ax.margins(0.11)
plt.tight_layout()
plt.axis("off")
plt.show()

Total running time of the script: (0 minutes 0.031 seconds)

Gallery generated by Sphinx-Gallery