【问题标题】:Matplotlib datetime line graph is shadedMatplotlib 日期时间折线图有阴影
【发布时间】: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


【解决方案1】:

由于澳大利亚报告的是多个省份的 Covid-19 数据,而不是全国总数,因此该图显示为已填充。澳大利亚有7个省,所以每个省应该有7行显示传播,但plt.plot()只显示一行。这会导致问题,因为它试图连接所有点,使图形看起来很填充。另一方面,南非报告的是全国总数,所以只有一条线,因此,为什么它看起来很正常。您可以使用plt.scatter() 可视化澳大利亚 7 个省的数据。代码如下:

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())

这是澳大利亚的输出:

如果您使用相同的代码,但将国家/地区切换为南非,则情节如下:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多