Python 3.x 使用numpy使用python进行数据分析。使用Matplotlib打印图形
使用Python 3.x 使用numpy使用python进行数据分析。使用Matplotlib打印图形,python-3.x,numpy,matplotlib,Python 3.x,Numpy,Matplotlib,使用matplotlib和numpy熊猫不可用 包含数据的文件位于网站的右上角。如果在.xslx中,另存为UTF-8 CSV。如果可能,更容易使用numpy、matplotlib模块 一个特定州和一个月中一天的死亡人数。必须能够选择另一天和另一个状态,并重复该过程,直到有离开程序的请求 显示一天和另一天的不同 在新文件中保存前两项的信息(状态和百分比) 用这些信息创建两个图表。x月差异和x月死亡人数。 我曾想过这样的事情,但它不会成功。我将文件转换为.txt 谢谢特伦顿,谢谢特伦顿。 imp
matplotlib
和numpy
熊猫
不可用
包含数据的文件位于网站的右上角。如果在.xslx中,另存为UTF-8 CSV。如果可能,更容易使用numpy、matplotlib模块
- 一个特定州和一个月中一天的死亡人数。必须能够选择另一天和另一个状态,并重复该过程,直到有离开程序的请求
- 显示一天和另一天的不同
- 在新文件中保存前两项的信息(状态和百分比)
- 用这些信息创建两个图表。x月差异和x月死亡人数。 我曾想过这样的事情,但它不会成功。我将文件转换为.txt
谢谢特伦顿,谢谢特伦顿。
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
# 1: Opening the file and saving the data in the matrix (OK?)
arq=open('arq_COVID.csv','r',encoding='UTF-8')
C=arq.readline()
matriz=[]
for line in arq:
matriz.append(line[:-1].split(';'))
arq.close
#2: Numpy array storage (not OK?)
print('Linhas: ',len(matriz),'\nColunas: ',len(matriz[0]))
matrizn=np.zeros((len(matriz),len(matriz[0])),dtype=object)
for i in range(matrizn.shape[0]):
for j in range(matrizn.shape[1]):
if j==7:
matrizn[i,j]=datetime.strptime(matriz[i][j],'%d/%m/%Y').date()
elif j==0 or j==1 or j==2 or j==6:
matrizn[i,j]=matriz[i][j]
else:
matrizn[i,j]=float(matriz[i][j]) # -> ValueError: could not convert string to float:''
#Probably have some empty value (''). How to solve?
# 3: Search for elements (No have idea...HELP!).
continue = 'Y'
while (continue == 'Y'):
md=[];me=[];tdeath=[]
print('='*30)
day=input('Choose a day of the month: ')
for x in range(len(matrizn)):
if x==day:
md=matrizn.append([x,7])
print('='*30)
state=input('Choose a state: ')
for y in range(len(matrizn)):
if y==state:
ms=matrizn.append([y,1])
for w in range(len(matrizn)):
if w==tdeath:
tdeath=matrizn.append([w,12])
else:
print('='*30)
continuar = input('Do you wish to continue [Y/N]? ')
# 4:Subtraction of elements (No have idea... HELP! )
matrizn1=np.zeros((len(md),len(tdeaths[0])),dtype=object)
for i in range(matrizn1.shape[0]):
for j in range(matrizn1.shape[1]):
if j==0:
matrizn1[i,j]=matrizn[i][12]-tdeaths
else:
matrizn1[i,j]=0
# 5: Write the data to another file (OK?)
arq1=open('Covid.csv','w')
for i in range(matrizn1.shape[0]):
line=''
for j in range(matrizn1.shape[1]):
line=line+str(matrizn1[i,j])+'\t'
line=line[:-1]+'n'
arq1.write(linha)
arq1.close
# 6: Plot graphs (OK?)
plt.subplot(221)
plt.plot(matrizn[:,7],matrizn[:,12],'-b',label='Number of deaths')
plt.title('Evolution COVID-19')
plt.ylabel('Month')
plt.xlabel('Number of deaths')
plt.legend()
plt.grid(True)
plt.savefig('cov1.png')
plt.tight_layout()
plt.subplot(222)
plt.plot(matrizn1[:,0],matrizn1[:,1],'-r',label='Percentage')
plt.title('Percentage')
plt.ylabel('Month')
plt.xlabel('Percentage')
plt.legend()
plt.grid(True)
plt.savefig('cov2.png')
plt.tight_layout()
plt.show