'Making graph with specific nodes

I'm trying to print a graph, but in the matrix representation of the graph there's a node that has no edges and so I would like to not have it in the printed graph. I can't change the matrix so I can't remove the rows and columns where the node is at, so that's unfortunately out of the question. I was thinking making a subgraph of the original graph, but I don't know how not to add that specific node without any edges because in theory I don't know what the matrix is to begin with. Any help would be appreciated.

Here's some example code

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np

A = np.array([[0, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 8],
[0, 3, 0, 7, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 6, 0, 0, 5, 0, 0],
[0, 0, 0, 0, 0, 5, 0, 7, 1],
[0, 0, 0, 0, 0, 0, 7, 0, 0],
[0, 0, 0, 3, 0, 3, 0, 2, 0]])

G = nx.from_numpy_matrix(A)
layout = nx.spring_layout(G)

sizes = [500 for node in G.nodes()]

color_map = ['b' for node in G.nodes()]

nx.draw(G, layout, with_labels=True, node_size=sizes, node_color=color_map)

labels = nx.get_edge_attributes(G, "weight")

nx.draw_networkx_edge_labels(G, pos=layout, edge_labels=labels)

plt.show()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source