[英]How to plot graph from my input relative with CSV file
我想 plot 圖表來比較我通過字符串輸入的每個國家/地區的情況。 我從這里導入數據
這是我所期待的一個例子:
從那張照片。 我為要輸入的國家范圍輸入 N = 3。 我一步一步輸入每個國家的名字。 現在起。 如果我想要 plot 圖表,我輸入了哪些國家/地區來比較一張圖表中的病例數,我該怎么辦?
這是我的代碼:
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#read file
dfi = pd.read_csv('time_series_covid19_confirmed_global.csv', sep=',')
df = dfi.drop(['Province/State','Lat','Long'], axis=1)
df
#sum all the cases of each province/State to 1 row.
df_gr = df.groupby('Country/Region').sum()
time = df_gr.columns.tolist()
df_gr.columns = pd.to_datetime(time)
df_gr.reset_index(inplace = True)
countriesx = df_gr['Country/Region'].unique() #keep each country into list.
countries_list = [x for x in countriesx] #clean the data.
df = df_gr.T
new_header = df.iloc[0]
df = df[1:]
df.columns = new_header
#input the number of countries to compare.
while True:
N = int(input('The number of countries to compare: '))
if N < 1 or N > 185:
print('The value must between1 and 185. Try again.\n')
continue
else:
print('Correct inout.\n')
break
#input name of countries to compare.
cnt = len(countries_list)
choose = [] #For the name of each countries.
for i in range(N):
print(f'Country {i+1} of {N}')
while True:
my_countries = input('Enter the full country name: ')
my_countries = my_countries.capitalize()
countries_list.append(my_countries)
set_checker = set(countries_list)
if len(set_checker) == cnt:
print(f'Country {i+1} confirmed as \'{my_countries}\'.\n')
choose.append(my_countries)
break
else:
print(f'There is no country named \'{my_countries}\'. Try again\n')
countries_list.remove(my_countries)
continue
這是我試過的。
現在我可以解決我的問題了。 如果我想按列表中的每個字符串繪制 plot 圖形,我可以這樣做。
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(#file.csv)
countries = ['Italy','Japan','Germany']
for i in countries:
plt.plot(df[i]) #This is data from CSV file
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.