Just a simple question. Let k(i) be the degree of node i in an undirected network. Is there a defined command like "DegreeCentrality" to plot the degree distribution (plotting P(k) versus k) of a graph like

g = ExampleData[{"NetworkGraph", "ZacharyKarateClub"}]

or do I have to write the code?

  • $\begingroup$ Like Histogram? Histogram[DegreeCentrality[g], {1}, "Probability"]? $\endgroup$ Apr 26, 2017 at 11:55
  • $\begingroup$ Thank you. I just wrote: data = VertexDegree[g] // Tally, data2 = Table[{data[[i, 1]], data[[i, 2]]/VertexCount[g]}, {i, data // Length}] // Sort, plot1 = ListPlot[data2] and plot2 = Histogram[DegreeCentrality[g], {1}, "Probability"] which is yours. And they overlap well. Mine was ListPlot and yours was Histogram. You showed me the short way. Thanks again. $\endgroup$
    – Haliki
    Apr 26, 2017 at 12:08
  • $\begingroup$ No worries. If you want to make your own plot using the same underlying data as Histogram you can look at HistogramList. $\endgroup$ Apr 26, 2017 at 12:23

1 Answer 1


You have to write the code.

Histogram[VertexDegree[g], {1}, "Probability", 
 AxesLabel -> {"degree", "probability"}]

Mathematica graphics

As you can see, it is simple enough that there is little reason to have a command for it.

  • $\begingroup$ Thank you. I think there is a difference between Mathematica 10 and 11. I am using 10 and same code gave me a shifted version of this histogram. I mean first yellow bar is between points (horizontal) 1 and 2, second bar (the long one) is between 2 and 3 etc. $\endgroup$
    – Haliki
    Apr 26, 2017 at 12:51
  • $\begingroup$ @Haliki You could use a more precise bin specification (2nd argument of Histogram), such as {-1/2, Max@VertexDegree[g] + 1/2, 1}. It seems that there are differences between various versions in how they determine the bins in automatic mode. BTW you can consider x.y to be a major version. 10.3 and 10.4 may have differences as big as 10.4 and 11.0. $\endgroup$
    – Szabolcs
    Apr 26, 2017 at 13:02
  • 1
    $\begingroup$ @Haliki Shameless advertisement: if you work with graphs frequently, you may want to check out the IGraph/M package. I am looking for feedback in preparation for a new release. Contributions to the documentation would also be very welcome. $\endgroup$
    – Szabolcs
    Apr 26, 2017 at 13:07
  • $\begingroup$ It worked thank you very much. I'll download and investigate the package. $\endgroup$
    – Haliki
    Apr 26, 2017 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.