TiShow
TiShow

Reputation: 1014

How to display labels with matplotlib and networkx

I'm really beginner for setting up development environment and programing. I had installed networkx and matplotlib with anaconda. But when I try to display graph, it cannot display labels like this picture. enter image description here

And this is code.

import networkx as nx
import matplotlib.pyplot as plt

g = nx.Graph()

g.add_node(2)
g.add_node(5)

g.add_edge(2,5)

g.add_edge(4,1)

g.add_edges_from([(2,5),(1,3)])

nx.draw(g)

plt.show()

As I looked up about this error, it seems that I should change backend. But I'm not really sure how to change it. It would be greatly appreciated if you could explain the details. I'm using MAC and Python3.6, anaconda.

Thank you.

Upvotes: 1

Views: 285

Answers (1)

DYZ
DYZ

Reputation: 57033

Use nx.draw_networkx(g) instead of nx.draw(g) because the latter does not draw labels.

Upvotes: 2

Related Questions