mysql 时间推移_随着时间的推移可视化COVID-19新案例
mysql 時間推移
This heat map shows the progression of the COVID-19 pandemic in the United States over time. The map is read from left to right, and color coded to show the relative numbers of new cases by state, adjusted for population.
該熱圖顯示了美國COVID-19大流行隨著時間的進展。 從左到右讀取地圖,并用顏色編碼顯示按州調整的新病例的相對數量,并根據人口進行調整。
This visualization was inspired by a similar heat map that I saw on a discussion forum thread. I could never locate the source, as it was only a pasted image with no link. The original version was also crafted to make a political point, separating states by predominate party affiliation, which I was not as interested in. I was fascinated by how it concisely showed the progression of the pandemic, so I decided to create a similar visualization myself that I could update regularly.
這種可視化的靈感來自我在討論論壇主題中看到的類似熱圖。 我永遠找不到源,因為它只是一個粘貼的圖像,沒有鏈接。 最初的版本還經過精心設計以提出政治觀點,以政黨之間的支配關系分隔國家,對此我并不感興趣。我可以定期更新。
Source code is hosted on my Github repo. If you are just interested in seeing updated versions of this heat map, I publish them weekly on my Twitter feed. It’s important to note that you should be careful comparing graphs from one week to another to each other, as the color map may change as new data is included. Comparisons are only valid within a given heatmap.
源代碼托管在我的Github存儲庫中 。 如果您只想查看此熱圖的更新版本,我每周都會在Twitter feed上發布它們。 重要的是要注意,您應該謹慎比較一周之間的圖表,因為隨著添加新數據,顏色圖可能會發生變化。 比較僅在給定的熱圖中有效。
The script relies on pandas, numpy, matplotlib, and seaborn.
該腳本依賴于pandas,numpy,matplotlib和seaborn。
The data comes from the New York Times COVID-19 Github repo. A simple launcher script clones the latest copy of the repository and copies the required file, and then launches the Python script to create the heat map. Only one file is really needed, so it could certainly be tightened up, but this works.
數據來自《紐約時報》 COVID-19 Github存儲庫 。 一個簡單的啟動器腳本將克隆存儲庫的最新副本并復制所需的文件,然后啟動Python腳本以創建熱圖。 確實只需要一個文件,因此可以將其收緊,但這是可行的。
echo "Clearing old data..." rm -rf covid-19-data/ rm us-states.csv echo "Getting new data..." git clone https://github.com/nytimes/covid-19-data echo "Done."cp covid-19-data/us-states.csv . echo "Starting..."python3 heatmap-newcases.py echo "Done."The script first loads a CSV file containing the state populations into a dictionary, which is used to scale daily new case results. The new cases are computed for each day from the running total in the NY Times data, and then scaled to new cases per 100,000 people in the population.
該腳本首先將包含州人口的CSV文件加載到字典中,該字典用于擴展每日新個案結果。 根據《紐約時報》數據中的運行總計每天計算新病例,然后將其擴展為人口中每100,000人的新病例 。
We could display the heat map at that point, but if we do, states with very high numbers of cases per 100,000 people will swamp the detail of the states with lower numbers of cases. Applying a log(x+1) transform improves contrast and readability significantly.
我們可以在那時顯示熱圖,但是如果這樣做,每10萬人中案件數量非常多的州將淹沒案件數量較少的州的詳細信息。 應用log(x + 1)變換可顯著提高對比度和可讀性。
Finally, Seaborn and Matplotlib are used to generate the heatmap and save it to an image file.
最后,使用Seaborn和Matplotlib生成熱圖并將其保存到圖像文件中。
That’s it! Feel free to use this as a framework for your own visualization. You can customize it to zero in on areas of interest.
而已! 隨意使用它作為您自己的可視化框架。 您可以在感興趣的區域將其自定義為零。
Full source code is below. Thanks for reading, and I hope you found it useful.
完整的源代碼如下。 感謝您的閱讀,希望您覺得它有用。
import numpy as np import seaborn as sns import matplotlib.pylab as plt import pandas as pd import csv import datetimereader = csv.reader(open('StatePopulations.csv'))statePopulations = {} for row in reader:key = row[0]if key in statePopulations:passstatePopulations[key] = row[1:]filename = "us-states.csv" fullTable = pd.read_csv(filename) fullTable = fullTable.drop(['fips'], axis=1) fullTable = fullTable.drop(['deaths'], axis=1)# generate a list of the dates in the table dates = fullTable['date'].unique().tolist() states = fullTable['state'].unique().tolist()result = pd.DataFrame() result['date'] = fullTable['date']states.remove('Northern Mariana Islands') states.remove('Puerto Rico') states.remove('Virgin Islands') states.remove('Guam')states.sort()for state in states:# create new dataframe with only the current state's datepopulation = int(statePopulations[state][0])print(state + ": " + str(population))stateData = fullTable[fullTable.state.eq(state)]newColumnName = statestateData[newColumnName] = stateData.cases.diff()stateData[newColumnName] = stateData[newColumnName].replace(np.nan, 0)stateData = stateData.drop(['state'], axis=1)stateData = stateData.drop(['cases'], axis=1)stateData[newColumnName] = stateData[newColumnName].div(population)stateData[newColumnName] = stateData[newColumnName].mul(100000.0)result = pd.merge(result, stateData, how='left', on='date')result = result.drop_duplicates() result = result.fillna(0)for state in states:result[state] = result[state].add(1.0)result[state] = np.log10(result[state])#result[state] = np.sqrt(result[state])result['date'] = pd.to_datetime(result['date']) result = result[result['date'] >= '2020-02-15'] result['date'] = result['date'].dt.strftime('%Y-%m-%d')result.set_index('date', inplace=True) result.to_csv("result.csv") result = result.transpose()plt.figure(figsize=(16, 10)) g = sns.heatmap(result, cmap="coolwarm", linewidth=0.05, linecolor='lightgrey') plt.xlabel('') plt.ylabel('')plt.title("Daily New Covid-19 Cases Per 100k Of Population", fontsize=20)updateText = "Updated " + str(datetime.date.today()) + \". Scaled with Log(x+1) for improved contrast due to wide range of values. Data source: NY Times Github. Visualization by @JRBowling"plt.suptitle(updateText, fontsize=8)plt.yticks(np.arange(.5, 51.5, 1.0), states)plt.yticks(fontsize=8) plt.xticks(fontsize=8) g.set_xticklabels(g.get_xticklabels(), rotation=90) g.set_yticklabels(g.get_yticklabels(), rotation=0) plt.savefig("covidNewCasesper100K.png")翻譯自: https://towardsdatascience.com/visualization-of-covid-19-new-cases-over-time-in-python-8c6ac4620c88
mysql 時間推移
總結
以上是生活随笔為你收集整理的mysql 时间推移_随着时间的推移可视化COVID-19新案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到什么代表桃花运
- 下一篇: 学习sql注入:猜测数据库_面向数据科学