Reputation: 109
I'm trying to graph a line graph in Matploblib to make a covid graph (scraping off dates for the x axis and plotting infections for the y axis) and while it mostly works, for some countries (like Australia) the graph is shaded???
dateX is a list of datetime objects like so: [datetime.datetime(2020, 1, 26, 0, 0, tzinfo=tzutc()), datetime.datetime(2020, 1, 26, 0, 0, tzinfo=tzutc()), datetime.datetime(2020, 1, 27, 0, 0, tzinfo=tzutc()),....] and confirmedY is a list where each entry is an int representing the amount of infected people like: [1, 3, 4, 1, 1, 4, 4, 1, 1, 2, 4, 3, 4, 2,....]
For some other countries like South Africa it works just fine???
More Code:
import aiohttp
import dateutil.parser #handle iso 8601 time codes
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
async with self.http_session.get(f'https://api.covid19api.com/dayone/country/{data["Slug"]}') as url:
if url.status == 200:
js = await url.json()
confirmedY = []
deathsY = []
recoveredY = []
dateX = []
for i in range(len(js)):
confirmedY.append(js[i]["Confirmed"])
deathsY.append(js[i]["Deaths"])
recoveredY.append(js[i]["Recovered"])
dateX.append(dateutil.parser.parse(js[i]["Date"]))
fig, ax = plt.subplots()
ax.xaxis.set_major_locator(mdates.MonthLocator()) #includes datetime tag at every month
ax.xaxis.set_major_formatter(mdates.DateFormatter("%m/%Y")) #format datetime string
ax.xaxis.set_minor_locator(mdates.MonthLocator()) #splits up intervals by months
plt.plot(dateX, confirmedY)
fig.autofmt_xdate() #rotates tags
plt.show()
(slugs are extracted from https://api.covid19api.com/summary) Replace data["Slug"] with that countries slug from^ (south africa is south-africa for example)
Upvotes: 0
Views: 86
Reputation: 2859
The plot appears filled because Australia reports Covid-19 Data for multiple provinces Rather than a national total. Australia has 7 provinces, so there should be 7 lines showing the spread in each province, but plt.plot()
only shows one line. This causes problems because it tries to connect all the points, making a graph which appears filled. On the other hand, South Africa reports a national total, so there is only one line, hence, why it appears normal. You can visualize Australia's data for the 7 provinces by using plt.scatter()
. Here is the code:
import aiohttp
import dateutil.parser #handle iso 8601 time codes
import matplotlib.pyplot as plt
import matplotlib.lines as Line2D
import matplotlib.dates as mdates
import asyncio
from asyncio.tasks import _asyncio
async def main():
async with aiohttp.ClientSession() as session:
async with await session.get('https://api.covid19api.com/dayone/country/australia') as url:
if url.status == 200:
js = await url.json()
confirmedY = []
deathsY = []
recoveredY = []
dateX = []
for i in range(len(js)):
confirmedY.append(js[i]["Confirmed"])
deathsY.append(js[i]["Deaths"])
recoveredY.append(js[i]["Recovered"])
dateX.append(dateutil.parser.parse(js[i]["Date"]))
fig, ax = plt.subplots()
ax.xaxis.set_major_locator(mdates.MonthLocator()) #includes datetime tag at every month
ax.xaxis.set_major_formatter(mdates.DateFormatter("%m/%Y")) #format datetime string
ax.xaxis.set_minor_locator(mdates.MonthLocator()) #splits up intervals by months
plt.scatter(dateX, confirmedY, facecolors='none', edgecolors='b')
fig.autofmt_xdate() #rotates tags
plt.show()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Here is the output for Australia:
If you use the same code, but switch the country to South Africa, this is the plot:
Upvotes: 1