简体   繁体   中英

Plot degree distribution in python

I'm new to python and im trying to plot the degree distribution for some data. So I wrote the following function:

def plotDegDistLogLog(G):
    degree_sequence = sorted([d for n, d in G.degree()], reverse=True)  # degree sequence
    degreeCount = collections.Counter(degree_sequence)
    deg, cnt = zip(*degreeCount.items())
    frac = [n/G.number_of_nodes() for n in cnt]
    fig, ax = plt.subplots()
    plt.plot(deg, frac, 'o')
    ax.set_yscale('log')
    ax.set_xscale('log')
    plt.ylabel("Fraction of nodes")
    plt.xlabel("Degree")
    plt.show()

I want to ask:

  • How can I create bins that grow exponentially in size?
  • How can I, in each bin, divide the sum of counts by the bin length?

I want to plot a line.

With hist, bins = numpy.histogram(x, bins, density=True) you can specify bins explicitly, so you can choose what you want (for example bins = numpy.exp(numpy.arange(10)) ). The density argument allows to normalize the histogram. Or you can divide each point of hist by each bin in bins[:-1] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM