来自 Pandas Dataframe 的多个时间序列图 [英] Multiple timeseries plots from Pandas Dataframe

查看:86
本文介绍了来自 Pandas Dataframe 的多个时间序列图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pandas 编写我的第一个 python 脚本.我有 10 年的风数据(1 分钟读数),我需要创建月度图,并在每个图上绘制速度和方向.

I am attempting to write my first python script using pandas. I have 10 years of wind data (1min readings) that i need to create monthly plots with the speed and direction plotted on each plot.

输入的 csv 数据如下所示:

The input csv data looks like this:

Date,Speed,Dir,
2014-01-01 00:00:00, 13, 179,
2014-01-01 00:01:00, 13, 178,
2014-01-01 00:02:00, 11, 169,
2014-01-01 00:03:00, 11, 178,
2014-01-01 00:04:00, 11, 181,

到目前为止,我已经写了下面的内容,这将创建一个在日期范围内设置的月份的图.除了我需要修复 x 轴标签外,我通常对这个图的外观感到满意.

So far i have written the below, this creates a plot for a month set in the date range. I am generally happy with how this plot looks except i need to fix the x axis labels.

我想遍历整个数据集并为每个月创建一个 pdf 图.对此的任何帮助将不胜感激!

I would like to loop through the whole dataset and create a pdf plot for each month. Any help with doing this would be appreciated!

import glob, os
import pandas as pd
from pandas import Series, DataFrame, Panel
import numpy as np
import matplotlib.pyplot as plt

wind = pd.read_csv('2014.csv')

wind['Date']=pd.to_datetime(wind['Date'])
wind=wind.set_index('Date')

dates = pd.date_range('2014-01', '2014-2', freq='1min')

janwin = Series(wind['Speed'], index=dates)
jandir = Series(wind['Dir'], index=dates)

plt.figure(1)
plt.subplot(211)
plt.plot(dates, janwin)

plt.ylabel("Km/hr")
plt.rcParams.update({'font.size': 4})
plt.grid(which='major', alpha = .5)


plt.subplot(212)
plt.plot(dates, jandir)
plt.ylabel("Degrees")
plt.rcParams.update({'font.size': 4})
plt.grid(which='major', alpha = 5)
plt.ylim(0,360)
plt.axis(minor=True) 

plt.savefig('test.pdf', dpi=900)

示例图

推荐答案

欢迎使用 Stackoverflow.通常,当您就此类问题寻求帮助时,最好先工作,直到遇到特定实例/问题,然后再寻求帮助.很难告诉您如何做如此广泛的事情,而且通常您不会得到很好的回应,因为看起来您只是懒惰并寻求帮助,而不是一直尝试解决问题.我看到了许多您需要解决的问题,但总的来说,您需要设置一个循环并弄清楚如何启动/停止循环,以及如何仅绘制您当前感兴趣的月份的数据.

Welcome to Stackoverflow. Typically when you're asking for assistance with this kind of problem it's best to work until you get stuck at a particular instance/issue and then ask for help. It's very hard to tell you how to do something this broad, and often you won't get a good response, as it seems like you're just being lazy and asking for help instead of trying all the way through to a problem. I see a number of issues you need to tackle, but broadly you need to setup a loop and figure out how to start/stop the loop, and how to only plot the data for the month you're currently interested in.

下面是我从记忆中快速编写的一些示例代码(尚未运行),我确信有更好的方法来做到这一点,但希望它能让您走上正轨.将来,如果您可以将帖子提炼为基本部分,您将获得最佳回复.在这种情况下,每天两个月的示例数据帧将有助于进行迭代/绘图.然后,您可以获取工作代码并调整为分钟.

Below is some sample code I wrote quickly from memory (hasn't been run), I'm sure there is a better way to do this, but hopefully it will get you on the right track. In the future, you'll get the best responses if you can distill your post down to the basic parts. In this case, a sample dataframe of two months daily would have been helpful to get the iteration/plotting down. You can then take the working code and adjust to minute.

如果这有帮助,请点赞并努力确保此处列出的最终代码对关注您的人有用.

If this is helpful please thumbs up and work to make sure the final code listed here is useful to those that follow you.

import pandas as pd
import matplotlib.pyplot as plt
import datetime
from dateutil.relativedelta import relativedelta
import calendar

#wind = pd.read_csv('2014.csv')
data = [['2014-01-01 00:00:00', 13, 179],
        ['2014-01-01 00:01:00', 13, 178],['2014-01-01 00:02:00', 11, 169],['2014-01-01 00:03:00', 11, 178], 
        ['2014-01-01 00:04:00', 11, 181]]

rawDf = pd.DataFrame(data, columns = ['Date','Speed','Dir'])

rawDf['Date']=pd.to_datetime(rawDf['Date'])

#Define beginning and end of loop - start at first month, end at last month
currDate = datetime.date(rawDf['Date'].min().year, rawDf['Date'].min().month, 1)
endDate = datetime.date(rawDf['Date'].max().year, rawDf['Date'].max().month, 1)


#loop
while currDate <= endDate:

    currMoEnd = datetime.date(currDate.year, currDate.month, calendar.monthrange(currDate.year,currDate.month)[1])
    wind = rawDf[(rawDf['Date']>= currDate) & (rawDf['Date']<= currMoEnd)]
    wind.set_index('Date', inplace = True)

    dates = pd.date_range(currDate, currMoEnd, freq='1min')

    janwin = pd.Series(wind['Speed'], index=dates)
    jandir = pd.Series(wind['Dir'], index=dates)

    plt.figure(1)
    plt.subplot(211)
    plt.plot(dates, janwin)

    plt.ylabel("Km/hr")
    plt.rcParams.update({'font.size': 4})
    plt.grid(which='major', alpha = .5)


    plt.subplot(212)
    plt.plot(dates, jandir)
    plt.ylabel("Degrees")
    plt.rcParams.update({'font.size': 4})
    plt.grid(which='major', alpha = 5)
    plt.ylim(0,360)
    plt.axis(minor=True) 

    plt.show()
    plt.savefig('{0}_output.pdf'.format(datetime.stftime(currDate,'%Y-%m')),  dpi=900)

    currDate = currDate + relativedelta(months = 1)

这篇关于来自 Pandas Dataframe 的多个时间序列图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆