2016-07-28 3 views
3

matplotlib 및 pyplot을 사용하여 CSV 파일에서 그래프를 만듭니다. 선 그래프는 아무 문제없이 만들 수 있지만 막대 그래프를 만드는 데는 많은 어려움이 있습니다.datetime을 사용하여 막대 그래프 만들기

나는이 게시물 matplotlib bar chart with dates 내 작업을 쉽게 수행 해야하는 것 같아 몇 가지 사이에 언급했지만 datetimes 내 목록과 함께 작동하도록 얻을 수 없습니다. 위의 포스트에서 정확한 코드를 실행

예상 그래프를 생성하지만 내 CSV 파일에서 내 자신에 대한 우리 자신의 x와 y 값을 바꿀 때 :

:

import matplotlib.pyplot as plt 
import matplotlib 
import numpy as np 
from datetime import datetime 
import csv 


columns="YEAR,MONTH,DAY,HOUR,PREC,PET,Q,UZTWC,UZFWC,LZTWC,LZFPC,LZFSC,ADIMC,AET" 

data_file="FFANA_000.csv" 

list_of_datetimes = [] 
skipped_header = False 
with open(data_file, 'rt') as f: 
    reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE) 
    for row in reader: 
     if skipped_header: 
      date_string = "%s/%s/%s %s" % (row[0].strip(), row[1].strip(), row[2].strip(), row[3].strip()) 
      dt = datetime.strptime(date_string, "%Y/%m/%d %H") 
      list_of_datetimes.append(dt) 
     skipped_header = True   


UZTWC = np.genfromtxt(data_file, delimiter=',', names=columns, usecols=("UZTWC")) 

x = list_of_datetimes 
y = UZTWC 

ax = plt.subplot(111) 
ax.bar(x, y, width=10) 
ax.xaxis_date() 

plt.show() 

는 실행이 오류를 제공합니다

Traceback (most recent call last): 
File "graph.py", line 151, in <module> 
ax.bar(x, y, width=10) 
File "C:\Users\rbanks\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\__init__.py", line 1812, in inner 
return func(ax, *args, **kwargs) 
File "C:\Users\rbanks\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\_axes.py", line 2118, in bar 
if h < 0: 
TypeError: unorderable types: numpy.ndarray() < int() 

내 선 그래프 플로팅에 필요한 날짜 시간 NumPy와 변환을 실행하는 경우 :

list_of_datetimes = matplotlib.dates.date2num(list_of_datetimes) 

동일한 오류가 발생합니다.

누군가 통찰력을 제공 할 수 있습니까? FFANA_000.csv에서

발췌 :

%YEAR,MO,DAY,HR,PREC(MM/DT),ET(MM/DT),Q(CMS), UZTWC(MM),UZFWC(MM),LZTWC(MM),LZFPC(MM),LZFSC(MM),ADIMC(MM), ET(MM/DT) 
2012, 5, 1, 0,  0.000,  1.250,  0.003,  2.928,  0.000,  3.335,  4.806,  0.000,  6.669,  1.042 
2012, 5, 1, 6,  0.000,  1.250,  0.003,  2.449,  0.000,  3.156,  4.798,  0.000,  6.312,  0.987 
2012, 5, 1, 12,  0.000,  1.250,  0.003,  2.048,  0.000,  2.970,  4.789,  0.000,  5.940,  0.929 
2012, 5, 1, 18,  0.000,  1.250,  0.003,  1.713,  0.000,  2.782,  4.781,  0.000,  5.564,  0.869 
2012, 5, 2, 0,  0.000,  1.250,  0.003,  1.433,  0.000,  2.596,  4.772,  0.000,  5.192,  0.809 
2012, 5, 2, 6,  0.000,  1.250,  0.003,  1.199,  0.000,  2.414,  4.764,  0.000,  4.829,  0.750 
2012, 5, 2, 12,  0.000,  1.250,  0.003,  1.003,  0.000,  2.239,  4.756,  0.000,  4.478,  0.693 
2012, 5, 2, 18,  0.000,  1.250,  0.003,  0.839,  0.000,  2.072,  4.747,  0.000,  4.144,  0.638 
2012, 5, 3, 0,  0.000,  1.250,  0.003,  0.702,  
+0

파일에서 몇 줄의 예제 라인을 제공 할 수 있습니까? –

+1

@MaximilianPeters 수정 된 원래 질문 –

답변

0

나는 완전히 데이터와 코드를 사용하여 문제를 재현 할 수 있습니다. 나는

> UZTWC = np.genfromtxt(data_file, delimiter=';', names=columns, 
> usecols=("UZTWC")) File 
> "C:\Python34-64bit\lib\site-packages\numpy\lib\npyio.py", line 1870, 
> in genfromtxt 
>  output = np.array(data, dtype) ValueError: could not convert string to float: b'UZTWC(MM)' 

를 얻을 그러나

UZTWC = np.genfromtxt(data_file, delimiter=',', usecols=(7), skip_header=1) 

UZTWC = np.genfromtxt(...)을 변경하려고하면 그래프를 얻어야한다. 문제는 어떤 이유로 numpy 배열이 문자열로되어 있고 수레가 아닙니다.

관련 문제