简体   繁体   English

使用日期时间创建条形图

[英]Create a bar graph using datetimes

I am using matplotlib and pyplot to create some graphs from a CSV file. 我正在使用matplotlib和pyplot从CSV文件创建一些图形。 I can create line graphs no problem, but I am having a lot of trouble creating a bar graph. 我可以创建线图没问题,但是我在创建条形图时遇到了很多麻烦。

I referred to this post matplotlib bar chart with dates among several others that seemed like they should easily accomplish my task, but I can't get it to work with my list of datetimes. 我提到了这个帖子matplotlib条形图,其中几个日期似乎他们应该很容易完成我的任务,但我不能让它与我的日期时间列表一起工作。

Running the exact code from the above post generates the expected graph, but when I swap our their x and y values for my own from my CSV file: 从上面的帖子运行确切的代码会生成预期的图形,但是当我从我的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()

Running this gives the error: 运行此命令会出错:

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

When I run the datetime numpy conversion that is necessary for plotting my line graphs: 当我运行绘制线图所需的datetime numpy转换时:

list_of_datetimes = matplotlib.dates.date2num(list_of_datetimes)

I get the same error. 我犯了同样的错误。

Could anyone offer some insight? 有人能提供一些见解吗?

excerpt from FFANA_000.csv: 摘自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,     

I could not fully reproduce your problem with your data and code. 我无法完全重现您的数据和代码问题。 I get 我明白了

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

But try changing UZTWC = np.genfromtxt(...) to 但是尝试将UZTWC = np.genfromtxt(...)更改为

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

and you should get a graph. 你应该得到一个图表。 The problem is that for some reason your numpy array is a made of strings and not floats. 问题是由于某种原因你的numpy数组是由字符串而不是浮点数组成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM