時隔一個多月,一起來看看疫情的大數據可視化

本文內用到的數據來源:疫情狀況的時間序列數據倉庫
(https://github.com/BlankerL/DXY-COVID-19-Data)

如果GitHub上下載速度過慢,也可前往碼雲下載:
https://gitee.com/zheng_bo_pu/DXY-COVID-19-Data

疫情已經過去一個多月了,在這一個多月的努力裏,我們取得了很多中國第一!GitHub上也出現了很多關於此次疫情的項目,其中一位程序員在GiHub上創建了名爲wuhan2020的倉庫,能夠在該項目上查到與疫情相關的各類最新信息,包括醫院、酒店、物流、生產以及捐贈物資與款項等信息。

今天,我們用大數據可視化來回顧一下疫情的發展過程:

繪製新冠病毒曲線動畫

視頻中所用的數據截止到2020年2月27日,黃色那條線是疑似感染人數統計,目前已經越來越少了,而綠色那條線是治癒人數,這條線在漸漸上升,於此同時,累計確診人數和死亡人數在漸漸趨近於某一個數值,這說明我國的疫情在逐漸轉好。

下面我們來看看這樣的四條曲線要怎麼繪製:

  1. 獲取繪圖所需數據
  2. 清洗數據
  3. 編寫程序

數據的獲取方式已經放在文章的開頭了,大家可以自行下載,得到數據以後,會發現有些數據不是我們想要的:
在這裏插入圖片描述
我們根據需要,選幾個就好了,這裏我選擇了:

  • confirmedCount 累計感染人數
  • suspectedCount 疑似感染人數
  • curedCount 治癒人數
  • deadCount 死亡人數
  • updateTime 更新時間

在這裏插入圖片描述
另外,這裏我做了倒序處理,時間的格式我也做了調整,到這一步,數據就算是處理好了。

接下來是從這裏面提取數據:

confirmedCount = []
suspectedCount = []
curedCount = []
deadCount = []
updateTime = []

在提取之前先定義5個空列表分別存放數據

	with open(r'F:\csdn\新冠病毒動畫曲線\data1.csv','r') as csvfile:
        plots = csv.reader(csvfile, delimiter=',')
        next(plots) #從第二行開始讀取
        for row in plots:
            confirmedCount.append(int(row[0]))
            suspectedCount.append(int(row[1]))
            curedCount.append(int(row[2]))
            deadCount.append(int(row[3]))
            updateTime.append(row[4])

提取出來之後,我們輸出一下:
在這裏插入圖片描述
這樣看來,數據提取出來的格式都沒問題,接下來就是繪圖,之前我們講過了繪製靜態圖的方法:數據可視化之新型冠狀病毒肺炎疫情地圖(python+MySQL)

今天我們來學一學怎麼繪製動畫的折線圖,原理非常簡單,就是將數據放入數組,然後每次往數組裏面增加一個數,清除之前的圖,重新畫出圖像

而python可視化庫matplotlib的顯示模式默認爲阻塞(block)模式,即在plt.show()之後,程序會暫停到那兒,並不會繼續執行下去。如果需要繼續執行程序,就要關閉圖片

因此,我們就要使用plt.ion()這個函數,使matplotlib的顯示模式轉換爲交互(interactive)模式

在交互模式中,如果沒有使用ioff()關閉的話,則圖像會一閃而過,並不會常留,因此,我們要給他個延遲關閉的效果:

plt.pause(2) #暫停2s

2秒後程序又會進入下一個循環,因此窗口得以保留,但循環結束以後,窗口還是會自動關閉,要想防止這種情況,需要在plt.show()之前加上ioff()命令:

plt.ioff()
plt.show()

下面是初始化:

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

add_subplot(1,1,1)中有三個參數,參數1和2分別代表子圖的行數和列數,參數3代表子圖的個數,這裏我們生成一個子圖就好了

下面我們設置一下橫縱座標軸的標籤,以及標題和圖例:

plt.xlabel('Date') #橫座標
plt.ylabel('count') #縱座標
plt.title('COVID-19 statistical data from China') #標題
ax1.legend() #圖例

看一下最終的函數:

def draw():
    with open(r'F:\csdn\新冠病毒動畫曲線\data1.csv','r') as csvfile:
        plots = csv.reader(csvfile, delimiter=',')
        next(plots) #從第二行開始讀取
        for row in plots:
            confirmedCount.append(int(row[0]))
            suspectedCount.append(int(row[1]))
            curedCount.append(int(row[2]))
            deadCount.append(int(row[3]))
            updateTime.append(row[4])
        # print(confirmedCount)
        # print(suspectedCount)
        # print(curedCount)
        # print(deadCount)
        # print(updateTime)
            ax1.clear()
            ax1.plot(updateTime,confirmedCount, label='confirmedCount',color="black")
            ax1.plot(updateTime,suspectedCount, label='suspectedCount',color="yellow")
            ax1.plot(updateTime,curedCount, label='curedCount',color="green")
            ax1.plot(updateTime,deadCount, label='deadCount',color="red")
            plt.xlabel('Date')
            plt.ylabel('count')
            plt.title('COVID-19 statistical data from China')
            ax1.legend()
            plt.pause(0.1)
        plt.ioff()
        plt.show()

draw()

當然,這樣的寫法不是最優的,我們稍微改寫一下:

import matplotlib.pyplot as plt
import csv

class DRAW_nCoV(object):
    def __init__(self):
        self.confirmedCount = []
        self.suspectedCount = []
        self.curedCount = []
        self.deadCount = []
        self.updateTime = []

    def draw(self):
        fig = plt.figure()
        #creating a subplot
        ax1 = fig.add_subplot(1,1,1)
        plt.ion()
        with open(r'F:\csdn\新冠病毒動畫曲線\data1.csv','r') as csvfile:
            plots = csv.reader(csvfile, delimiter=',')
            next(plots) #從第二行開始讀取
            for row in plots:
                self.confirmedCount.append(int(row[0]))
                self.suspectedCount.append(int(row[1]))
                self.curedCount.append(int(row[2]))
                self.deadCount.append(int(row[3]))
                self.updateTime.append(row[4])
                ax1.clear()
                ax1.plot(self.updateTime,self.confirmedCount, label='confirmedCount',color="black")
                ax1.plot(self.updateTime,self.suspectedCount, label='suspectedCount',color="yellow")
                ax1.plot(self.updateTime,self.curedCount, label='curedCount',color="green")
                ax1.plot(self.updateTime,self.deadCount, label='deadCount',color="red")
                plt.xlabel('Date')
                plt.ylabel('count')
                plt.title('COVID-19 statistical data from China')
                ax1.legend()
                plt.pause(0.1)
            plt.ioff()
            plt.show()

def main():
    draw_nCoV = DRAW_nCoV()
    draw_nCoV.draw()

if __name__ == '__main__':
    main()

在這裏插入圖片描述
最後,大家對於此次疫情,有什麼想說的呢?無論是疫情之後想做的事,還是有什麼心裏話,都可以告訴我們噢!工作人員會從中抽取優秀回答送上精美禮品!

參與方法:
關注公衆號"情感衛生間"後,直接回復內容即可,公衆號接有智能對話機器人,歡迎大家跟"小柒"交流哦!
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章