我有一个包含3个月温度数据的大型csv文件。第1列是月份,第2列是日期,第3列是小时,第4列是分钟,第5列是温度。我试图仅用x轴上列出的日/月来绘制所有3个月的温度数据。以下是我到目前为止的情况:
filename ='TemperatureFile.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
month, day, hour, temperature = [], [], [], []
for row in reader:
mon=datetime.strptime(row[0],"%m")
month.append(mon)
date=datetime.strptime(row[1],"%d")
day.append(date)
hourx=datetime.strptime(row[2],"%H:%M")
hour.append(hourx)
temp = float(row[3])
temperature.append(temp)
datefinal=date.isoformat(month, day)
#date.isoformat(months, days).isoformat()==
fig = plt.figure(dpi=128, figsize=(10,6))
plt.plot(datefinal,temperature, c='red')
plt.title("Temperatures", fontsize=20)
plt.xlabel('Date', fontsize=16)
plt.ylabel('Temperatures(F)', fontsize=16)
plt.show()
我无法弄清楚如何结合所有月/日/小时/分钟信息,以便所有数据图和我无法弄清楚如何在x轴上放置月/日。
答案 0 :(得分:1)
好吧,如果你真的需要从部分创建一个日期时间,你可以这样做:
public function actionView($id)
{
$yourModelCountry = Country::find()->where(...)->one();
return $this->render('view', [
'model' => $this->findModel($id),
'modelCountry' => $yourModelCountry ,
]);
}
但是,在您的情况下似乎没有必要。实际上,一次创建一个日期时间对象要容易得多,而不是创建基本日期时间对象并添加新部件:
dtms = []
temps = []
for row in reader:
# split row into its 4 parts
month, day, hour, temperature = row
# create base datetime object with the current date/time
dt = datetime.now()
# replace current month
dt = dt.replace(month=int(month))
# replace current day
dt = dt.replace(day=int(day))
## or, you could replace both at the same time
#dt = dt.replace(month=int(month), day=int(day))
# get date object from datetime
dt = dt.date()
# get time
tm = datetime.strptime(hour, "%H:%M")
# get time object from datetime
tm = tm.time()
# combine date and time
dtm = datetime(dt, tm)
# add datetime to list
dtms.append(dtm)
# add temperature to list
temps.append(float(temperature))
答案 1 :(得分:1)
为了让你提交的代码能够工作,我不得不玩很多东西。
然后我可以从你拥有的3列创建一个日期。
最后,我专注于情节的格式化。
真的是2个问题。
filename ='TemperatureFile.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates_list, temperature = [], []
for row in reader:
#Convert your 3 colums of strings into a sinlge datestring.
datestring = ("{0:}/{1:} {2:}".format(*row))
# Then convert that into a complete date object
date_obj = datetime.datetime.strptime(datestring, "%m/%d %H:%M")
# Get the temperature as before
temp = float(row[3])
# Add new date and temp to appropriate lists
temperature.append(temp)
dates_list.append(date_obj)
# Now have complete lists of date and temp.
# Next focus on the formatting of the plot.
myFmt = mdates.DateFormatter('%m/%d') # set the format to print the dates in.
months = mdates.MonthLocator() # every month
days = mdates.DayLocator() # every Day
fig = plt.figure(dpi=128, figsize=(10,6))
ax = fig.add_axes([0.1, 0.2, 0.85, 0.75])
plt.plot(dates_list,temperature, c='red')
# format the ticks
ax.xaxis.set_major_formatter(myFmt)
ax.xaxis.set_major_locator(months)
ax.xaxis.set_minor_locator(days)
plt.title("Temperatures", fontsize=20)
plt.xlabel('Date', fontsize=16)
plt.ylabel('Temperatures(F)', fontsize=16)
plt.show()
有关如何格式化图表的详细信息,请搜索或搜索您要执行的操作以及单词matplotlib。那是我从中得到我的榜样的地方。