Written and maintained by CASRAI Editorial Board
Last updated
The Shannon diversity index (also called the Shannon-Wiener index, Shannon entropy as applied to species counts, or H′) is a single number that summarises how many categories a sample contains and how evenly individuals are spread across them. This guide gives the formula, a fully worked calculation you can check by hand, how to read the result, and copy-ready code for Excel, R and Python.
What is the Shannon diversity index?
The Shannon diversity index measures the uncertainty in predicting which category a randomly drawn individual belongs to. It is highest when every category (usually a species) is equally common and falls to zero when the whole sample is one category. It rises with both richness (the number of categories) and evenness (how equal their proportions are), which is why it is often used in ecology alongside the concepts in descriptive statistics.
The measure comes from Claude Shannon’s information entropy, published in the 1948 paper “A mathematical theory of communication” (Bell System Technical Journal 27:379-423, 623-656). Ecologists adopted the same quantity for community data, which is why the names “Shannon-Wiener index”, “Shannon-Weaver index” and “Shannon entropy” all appear in the literature for essentially the same formula.
Shannon diversity index formula
As given in the vegan package vignette “Diversity analysis in vegan” (CRAN), the index is:
H′ = − ∑ pi logb(pi), summed over i = 1 to S.
- H′: the Shannon diversity index.
- S: the number of species (categories) in the sample, its richness.
- pi: the proportion of all individuals that belong to species i (count of species i divided by the total count N). The proportions sum to 1.
- b: the base of the logarithm. The vegan vignette states that natural logarithms are the most common choice (and the index is then written H′), that b = 2 has theoretical justification, and that natural logarithms are the default.
Which logarithm base should you use?
Use the natural logarithm unless your field or reviewer expects another. The base changes the number: natural logs give units of nats and base 2 gives bits, so H′ values computed with different bases cannot be compared directly. Always state the base in your methods. The conversion is Hbits = Hnats / ln 2.
Worked example with real numbers
Suppose a quadrat survey records N = 105 individuals across S = 5 species, with counts of 40, 25, 20, 12 and 8. (These counts are an illustrative calculation, not field data.) Step 1: divide each count by 105 to get pi. Step 2: take the natural log of each pi. Step 3: multiply, then sum and flip the sign.
| Species | Count | pi | ln pi | − pi ln pi |
|---|---|---|---|---|
| A | 40 | 0.3810 | −0.9651 | 0.3676 |
| B | 25 | 0.2381 | −1.4351 | 0.3417 |
| C | 20 | 0.1905 | −1.6582 | 0.3159 |
| D | 12 | 0.1143 | −2.1691 | 0.2479 |
| E | 8 | 0.0762 | −2.5745 | 0.1962 |
| Total | 105 | 1.0000 | 1.4693 |
So H′ ≈ 1.47 (1.4692 before rounding the table entries). Check against the bounds: with S = 5, the largest possible value is ln 5 = 1.6094, reached when all five species have p = 0.2. Our sample reaches 91% of that ceiling.
Evenness. Pielou’s evenness is J = H′ / ln S (given in the vegan vignette as H/log(specnumber(BCI))). Here J = 1.4692 / 1.6094 = 0.913. Effective number of species. The vignette identifies exp(H′) as the Hill number N1; here exp(1.4692) = 4.35, meaning this community is as diverse as one with about 4.35 equally common species, against 5 actually present.
How to interpret the Shannon diversity index
H′ has two anchor points that come straight from Shannon (1948): for a given number of categories n, H is a maximum, equal to log n, when all pi are equal; and H = 0 if and only if all pi but one are zero. Everything in between is relative, so interpret H′ against these bounds and against other samples measured the same way, not against a universal threshold.
| Result | What it means |
|---|---|
| H′ = 0 | Only one species is present. |
| H′ = ln S | The maximum for S species: every species is equally common (J = 1). |
| J close to 1 | Individuals are spread nearly evenly across the species present. |
| J well below 1 | One or a few species dominate the sample. |
| H′ higher in sample X than sample Y (same S, same log base) | Sample X is more even, so more diverse by this measure. |
| exp(H′) (Hill N1) | The effective number of equally common species; easier to explain to non-specialists than a raw H′. |
Assumptions and common mistakes
- Mixing log bases. A value in bits is not comparable with one in nats. Fix the base for the whole study.
- Comparing across different richness without context. Because the maximum is ln S, a higher H′ can simply reflect more species. Report J or exp(H′) next to H′ so readers can separate richness from evenness.
- Treating it as a species count. H′ is not richness. Two samples with the same H′ can have different numbers of species.
- Using proportions that do not come from comparable sampling. The pi are only meaningful if every sample was collected with comparable effort and method. See sampling methods for how design affects what a sample represents.
- Taking the log of zero. Species with a count of zero must be dropped from the sum (the term p ln p is taken as zero); spreadsheet formulas that include them return errors.
- Treating the index as a test. H′ is a descriptive statistic. To compare groups you still need a suitable statistical test and, ideally, a confidence interval or effect size alongside it.
How to report the Shannon diversity index (APA example)
State the log base, the number of species and individuals, and the evenness. Check your style guide for how to typeset symbols such as H′ and J. Template:
Species diversity was calculated with the Shannon index (natural logarithm). Diversity at Site 1 was H′ = 1.47 (S = 5, N = 105), with Pielou’s evenness J = 0.91.
Then cite how you computed it, for example the R package: the vegan package for R (CRAN) and Shannon (1948) for the index itself. Follow your journal’s reference style for the exact format.
How to calculate the Shannon diversity index in Excel, R or Python
Excel
Put the counts in B2:B6 (no zeros). Then:
=-SUMPRODUCT(B2:B6/SUM(B2:B6), LN(B2:B6/SUM(B2:B6)))
Evenness: divide that cell by LN(COUNT(B2:B6)). For the example counts the result is 1.4692.
R (vegan)
library(vegan)
counts <- c(40, 25, 20, 12, 8)
H <- diversity(counts, index = "shannon") # natural log by default
J <- H / log(specnumber(counts)) # Pielou's evenness
exp(H) # effective number of species
Python
import math
counts = [40, 25, 20, 12, 8]
n = sum(counts)
p = [c / n for c in counts if c > 0]
H = -sum(x * math.log(x) for x in p) # nats; use math.log2 for bits
J = H / math.log(len(p))
print(round(H, 4), round(J, 4), round(math.exp(H), 3)) # 1.4692 0.9129 4.346
Frequently asked questions
What is a good Shannon diversity index value?
There is no universal “good” value. H′ ranges from 0 up to ln S, so judge it against that ceiling (via J) and against comparable samples collected the same way.
Is the Shannon index the same as the Shannon-Wiener index?
Yes in practice: the same formula is called Shannon, Shannon-Wiener, Shannon-Weaver or Shannon entropy. For a paper on how the “Shannon-Wiener” name is used, see Spellerberg and Fedor (2003) in the sources below.
Can the Shannon diversity index be negative?
No. Each pi is between 0 and 1, so ln pi is zero or negative and the leading minus sign makes every term non-negative. The minimum is 0.
Should I use log base 2, e or 10?
Natural log is the common default (and vegan’s default). Base 2 is also used. Whatever you pick, state it and use it throughout.
How is it different from the Simpson index?
Both combine richness and evenness. The vegan vignette places them in the same family of Hill numbers: N1 = exp(H′) for Shannon and N2 = inverse Simpson.
Sources
- Shannon, C. E. (1948). A mathematical theory of communication. Bell System Technical Journal, 27, 379-423, 623-656. https://people.math.harvard.edu/~ctm/home/text/others/shannon/entropy/entropy.pdf (checked 2026-10-29).
- Diversity analysis in vegan (package vignette), CRAN. https://cran.r-project.org/web/packages/vegan/vignettes/diversity-vegan.pdf (checked 2026-10-29).
- Hill, M. O. (1973). Diversity and evenness: a unifying notation and its consequences. Ecology, 54(2), 427-432 (cited in the vegan vignette).
- Spellerberg, I. F., & Fedor, P. J. (2003). A tribute to Claude Shannon (1916-2001) and a plea for more rigorous use of species richness, species diversity and the “Shannon-Wiener” Index. Global Ecology and Biogeography, 12(3), 177-179. Further reading.
- Jost, L. (2006). Entropy and diversity. Oikos, 113(2), 363-375. Further reading.
Related: biodiversity datasets are published through GBIF, and a wider overview of the discipline is in What Is Statistics?








