D.Fig
D.Fig

Reputation: 149

Making simple graph histogram in Python3

I'm trying to make a histogram based on graph degrees values. But when I run my code, i'm getting this error:

x has only one data point. bins or range kwarg must be given.

If somebody can tell me what am I doing wrong, i would appreciate it.

The code is:

import numpy as np
import pandas
import networkx as nx
import unicodecsv as csv
import operator
import matplotlib.pyplot as plt
import scipy.stats as st
import community
import plotly.plotly as py

#graph
path="hero-network.csv"

graph = nx.Graph(name="Heroic Social Network")
with open(path, 'rb') as data:
    reader = csv.reader(data)
    for row in reader:
        graph.add_edge(*row)

#histogram
plt.hist(graph.degree().values() , bins=500)
plt.title("Connectedness of Marvel Characters")
plt.xlabel("Degree")
plt.ylabel("Frequency")
plt.show()

csv datafile looks like this (first 10 nodes):

LITTLE, ABNER,"PRINCESS ZANDA"
LITTLE, ABNER,"BLACK PANTHER/T'CHAL"
BLACK PANTHER/T'CHAL,"PRINCESS ZANDA"
LITTLE, ABNER,"PRINCESS ZANDA"
LITTLE, ABNER,"BLACK PANTHER/T'CHAL"
BLACK PANTHER/T'CHAL,"PRINCESS ZANDA"
STEELE, SIMON/WOLFGA,"FORTUNE, DOMINIC"
STEELE, SIMON/WOLFGA,"ERWIN, CLYTEMNESTRA"
STEELE, SIMON/WOLFGA,"IRON MAN/TONY STARK "
STEELE, SIMON/WOLFGA,"IRON MAN IV/JAMES R."

Upvotes: 2

Views: 781

Answers (1)

Rems
Rems

Reputation: 5027

Matplotlib interprets iterables as a single value. In python 3, dict.values returns an iterable. Convert it to list and it will work:

plt.hist(list(graph.degree().values()), bins=500)

Upvotes: 1

Related Questions