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.
| Year | Neuroinvasive cases | Non-neuroinvasive cases |
|---|---|---|
| 1999 | 59 | 3 |
| 2000 | 19 | 2 |
| 2001 | 64 | 2 |
| 2002 | 2946 | 1210 |
| 2003 | 2866 | 6996 |
| 2004 | 1148 | 1391 |
| 2005 | 1309 | 1691 |
| 2006 | 1495 | 2774 |
| 2007 | 1227 | 117 |
| 2008 | 689 | 667 |
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()