【发布时间】:2021-01-14 01:37:36
【问题描述】:
我正在尝试在 Matploblib 中绘制折线图以制作 covid 图表(刮掉 x 轴的日期并绘制 y 轴的感染情况),虽然它大部分都有效,但对于某些国家(如澳大利亚)图表是shaded???
dateX 是一个日期时间对象列表,如下所示: [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()),....] 和 confirmY 是一个列表,其中每个条目都是一个 int,表示受感染的人数,例如: [1, 3, 4, 1, 1, 4, 4, 1, 1, 2, 4, 3, 4, 2,....]
对于像South Africa 这样的其他一些国家,它工作得很好???
更多代码:
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()
(从https://api.covid19api.com/summary 中提取slug) 将 data["Slug"] 替换为来自^的国家(例如,南非是南非)
【问题讨论】:
-
我们需要查看更多代码,并查看此代码与南非代码之间的区别。
-
@DapperDuck 我编辑了我的原始帖子!
-
我看到了你的代码,但是它没有运行。请包括您的进口声明
-
@DapperDuck 完成!我的坏
-
我查看了您的代码并尝试了一些方法,例如
fillstyle='none',但它们不起作用
标签: python datetime matplotlib