farry

covid19-heatmap.py

Oct 22nd, 2021
1,011
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. # Browse to this URL, click download link under heatmap, save as "data.json"
  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.  
  10. with open ("data.json", "r") as fs:
  11.     alldata = json.load(fs)
  12.    
  13. datablock = list(reversed(alldata["data"]))
  14. caselist = [x["newCasesBySpecimenDateAgeDemographics"] for x in datablock]
  15. dates = [x["date"] for x in datablock]
  16. dateobj = [datetime.datetime.strptime(d,'%Y-%m-%d').date() for d in dates]
  17.  
  18. agelist = [x["age"] for x in caselist[0]]
  19. removelist = ["00_59", "60+", "unassigned"]
  20. agelist = [a for a in agelist if a not in removelist]
  21. ageticks = [a.replace("_", "-") for a in agelist]
  22.  
  23. rollingsums = []
  24. for daylist in caselist:
  25.     daydict = []
  26.     for agedict in daylist:
  27.         if agedict["age"] in agelist:
  28.             daily = agedict["rollingSum"] / 7.0
  29.             daydict.append(daily)
  30.     rollingsums.append(daydict)
  31. rollingmap = list(zip(*rollingsums))
  32.  
  33. cmesh = plt.pcolormesh(dateobj, ageticks, rollingmap,
  34.                        cmap="gist_heat", shading='auto')
  35.  
  36. plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
  37. plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
  38. plt.gcf().autofmt_xdate()
  39.  
  40. cbar = plt.colorbar(cmesh)
  41. cbar.cmap.set_gamma(0.4)
  42.  
  43. plt.gcf().set_size_inches(10, 5)
  44. plt.title("ENGLAND - heatmap of daily COVID-19 cases by age", size=14)
  45. plt.tight_layout()
  46. plt.savefig("heatmap.png")
  47. plt.show()
  48.  
RAW Paste Data