【发布时间】:2014-05-21 03:28:49
【问题描述】:
我正在尝试从 csv 文件中获取日期以绘制图表,并且难以获得用于比较数据的方法。日期格式为 MM/DD/YYYY HH:MM:SS 。几天来,我一直在努力寻找一种方法来执行这项任务,但我的导师坚持使用在线资源,而不是为我们的最终项目提供直接帮助。
我认为我需要使用 for 循环来创建“现在”变量,但我不知道如何比较“现在”变量下的所有日期。我也不知道如何将参数插入到“then”变量中,因为 now[n] 给出了 csv 文件中日期的第 n 个条目。
提前感谢您的帮助,我会尽量整理我的帖子以澄清!
"""
Demo of scatter plot with varying marker colors and sizes.
"""
import numpy as np
import matplotlib.pyplot as plt
import os.path
import random
import datetime
from datetime import timedelta
import time
# Load a numpy record array from yahoo csv data with fields date,
# open, close, volume, adj_close from the mpl-data/example directory.
# The record array stores python datetime.date as an object array in
# the date column
directory = os.path.dirname(os.path.abspath(__file__))
datafile = os.path.join(directory, 'v2vcomm.csv')
data = np.recfromcsv(datafile, delimiter=',', filling_values=np.nan, case_sensitive=True, deletechars='', replace_space=' ')
#start = datetime.datetime(np.data(data.date)
#datetime.datetime.time.strftime("%y-%m-%d-%H-%M")
time = data.date
now = time.replace('/', ' ').replace(':', ' ').split() #having problems turning this into a useable list. It outputs a list containing each individual date as a element, so it isn't helpful to me
then = datetime.datetime(int(now[2]), int(now[0]), int(now[1]), int(now[3]), int(now[4]), int(now[5])) #trying to input int values for each item in one date, but the formatting is tricky to me
length = len(np.diff(data.msg_id))
#these random values are from an earlier assignment, going to eliminate them when I get datetime working
x= []
z= []
for v in range(1,length+1):
x.append(random.randint(0,300))
z.append(random.randint(1,100))
y = np.diff(data.msg_id)
count = 1
fig, ax = plt.subplots()
ax.scatter(x, y, c=z, s=30, alpha=0.5)
ax.set_xlabel(r'Total Time Elasped since - Seconds (300=5 min)', fontsize=12)
ax.set_ylabel(r'Impediments', fontsize=12)
ax.set_title('Traffic Flow Impediment Frequency')
ax.grid(True)
fig.tight_layout()
plt.show()
【问题讨论】:
-
你的导师是对的——这是你应该能够使用在线资源自己解决的问题。如果您在此处需要帮助,请提供自包含可编译示例。
标签: python csv matplotlib