farry

covid19-heatmap-agecorrected.py

Jan 4th, 2022
1,066
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Browse to this URL and click download link under heatmap:
  4. # https://coronavirus.data.gov.uk/details/cases?areaType=nation&areaName=England
  5.  
  6. import json, datetime
  7. import matplotlib.pyplot as plt
  8. import matplotlib.dates as mdates
  9. import matplotlib.colors as colors
  10.  
  11.  
  12. with open ("data.json", "r") as fs:
  13.     alldata = json.load(fs)
  14.    
  15. datablock = list(reversed(alldata["data"]))
  16. caselist = [x["newCasesBySpecimenDateAgeDemographics"] for x in datablock]
  17. dates = [x["date"] for x in datablock]
  18. dateobj = [datetime.datetime.strptime(d,'%Y-%m-%d').date() for d in dates]
  19.  
  20. agelist = [x["age"] for x in caselist[0]]
  21. removelist = ["00_59", "60+", "unassigned"]
  22. agelist = [a for a in agelist if a not in removelist]
  23. ageticks = [a.replace("_", "-") for a in agelist]
  24.  
  25. rollingrate = []
  26. for daylist in caselist:
  27.     daydict = []
  28.     for agedict in daylist:
  29.         if agedict["age"] in agelist:
  30.             daily = agedict["rollingRate"]
  31.             daydict.append(daily)
  32.     rollingrate.append(daydict)
  33. rollingmap = list(zip(*rollingrate))
  34.  
  35. fig, ax = plt.subplots()
  36. cmesh = ax.pcolormesh(dateobj, ageticks, rollingmap,
  37.         cmap="gist_heat", norm=colors.PowerNorm(gamma=0.65),  shading='auto')
  38.  
  39. ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
  40. ax.xaxis.set_major_locator(mdates.MonthLocator())
  41. fig.autofmt_xdate()
  42.  
  43. cbar = fig.colorbar(cmesh, fraction=0.08)
  44. cbar.cmap.set_gamma(0.5)
  45.  
  46. fig.set_size_inches(10, 5)
  47. top = "ENGLAND - Covid 19 Heatmap - Cases per 100,000 in each age range per day"
  48. ax.set_title(top, fontsize=13)
  49. fig.tight_layout()
  50. fig.savefig("heatmap2.png")
  51. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment