farry

covid19-heatmap.py

Oct 22nd, 2021 (edited)
1,297
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. with open ("data.json", "r") as fs:
  12.     alldata = json.load(fs)
  13.    
  14. datablock = list(reversed(alldata["data"]))
  15. caselist = [x["newCasesBySpecimenDateAgeDemographics"] for x in datablock]
  16. dates = [x["date"] for x in datablock]
  17. dateobj = [datetime.datetime.strptime(d,'%Y-%m-%d').date() for d in dates]
  18.  
  19. agelist = [x["age"] for x in caselist[0]]
  20. removelist = ["00_59", "60+", "unassigned"]
  21. agelist = [a for a in agelist if a not in removelist]
  22. ageticks = [a.replace("_", "-") for a in agelist]
  23.  
  24. rollingsums = []
  25. for daylist in caselist:
  26.     daydict = []
  27.     for agedict in daylist:
  28.         if agedict["age"] in agelist:
  29.             daily = agedict["rollingSum"] / 7.0
  30.             daydict.append(daily)
  31.     rollingsums.append(daydict)
  32. rollingmap = list(zip(*rollingsums))
  33.  
  34. fig, ax = plt.subplots()
  35. cmesh = ax.pcolormesh(dateobj, ageticks, rollingmap,
  36.         cmap="gist_heat", norm=colors.PowerNorm(gamma=0.65),  shading='auto')
  37.  
  38. ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
  39. ax.xaxis.set_major_locator(mdates.MonthLocator())
  40. fig.autofmt_xdate()
  41.  
  42. cbar = fig.colorbar(cmesh, fraction=0.1)
  43. cbar.cmap.set_gamma(0.5)
  44.  
  45. fig.set_size_inches(10, 5)
  46. top = "ENGLAND - Covid 19 Heatmap - Total cases in each age range per day"
  47. ax.set_title(top, fontsize=13)
  48. fig.tight_layout()
  49. fig.savefig("heatmap.png")
  50. plt.show()
Add Comment
Please, Sign In to add comment