【问题标题】:How to plot graph from my input relative with CSV file如何从我的输入相对于 CSV 文件绘制图形
【发布时间】:2020-04-07 12:05:14
【问题描述】:

我想绘制图表来比较我通过字符串输入的每个国家/地区的情况。我从here导入数据

这是我所期待的一个例子:

从那张照片。我为要输入的国家范围输入 N = 3。我一步一步输入每个国家的名字。现在起。如果我想绘制输入我的国家/地区的图表以比较一张图表中的病例数,我该怎么办?

这是我的代码:

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

这个我试过了。

【问题讨论】:

标签: python pandas matplotlib


【解决方案1】:

现在我可以解决我的问题了。如果我想按列表中的每个字符串绘制图形,我可以这样做。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多