Skip to content
scipython.com

P7.4.2West Nile Virus disease statistics

Plot, as a histogram, the data in the table below concerning the number of cases of West Nile Virus disease in the United States betwee 1999 and 2008. The two types of disease, neuroinvasive and non-neuroinvasive should be plotted as separate bars on the same chart for each year.

YearNeuroinvasive casesNon-neuroinvasive cases
1999593
2000192
2001642
200229461210
200328666996
200411481391
200513091691
200614952774
20071227117
2008689667

These data can be downloaded as the file wnv.txt

Show solution

Two bar charts can be plotted on the same axes by repeating the call to ax.bar, but we need to offset the bars in each chart.

We set the xticks explicitly with ax.set_xticks(year) so that every year is labelled (otherwise matplotlib defaults to a sparser tick layout which labels every other year).

westnilevirus
import numpy as np
import matplotlib.pyplot as plt

year, cases_inv, cases_noninv = np.loadtxt("wnv.txt", unpack=True, skiprows=4)
fig, ax = plt.subplots()

w = 0.3
noninv_bars = ax.bar(year - w, cases_noninv, w, color="gray")
inv_bars = ax.bar(year, cases_inv, w, color="k")

ax.set_ylabel("Number of cases")
ax.set_xlabel("Year")
ax.set_title(
    "Cases of neuroinvasive and non-neuroinvasive\n"
    "West Nile Virus disease in the United States, 1999-2008"
)
ax.set_xticks(year)
ax.legend((noninv_bars, inv_bars), ("Non-neuroinvasive", "Neuroinvasive"))

plt.show()
West Nile Virus statistics
West Nile Virus statistics.