Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何绘制具有3个特征的折线图子图_Python_Pandas_Matplotlib - Fatal编程技术网

Python 如何绘制具有3个特征的折线图子图

Python 如何绘制具有3个特征的折线图子图,python,pandas,matplotlib,Python,Pandas,Matplotlib,我试图从这个数据帧中绘制子图 df: 我想为一周中的每一天(星期一、星期二、星期天)绘制一个子图。x轴应为一天中的每小时。虽然这些线应表示“圆锥体”,但y轴是各“圆锥体”在相应时间和日期内出现的次数 谢谢。我认为最简单的方法是从日期时间中提取一周中的某一天,并将其与您感兴趣的一系列选项一起使用(即,“['car','truck'”]),以累积日期范围内每天每小时的事件。下面是一个演示此方法的示例(使用一些随机生成的数据进行演示) 其输出为 根据描述,您需要7个子批次,每个子批次中有两行来自cOn

我试图从这个数据帧中绘制子图

df:

我想为一周中的每一天(星期一、星期二、星期天)绘制一个子图。x轴应为一天中的每小时。虽然这些线应表示“圆锥体”,但y轴是各“圆锥体”在相应时间和日期内出现的次数


谢谢。

我认为最简单的方法是从
日期时间中提取一周中的某一天,并将其与您感兴趣的一系列选项一起使用(即,“['car','truck'”]),以累积日期范围内每天每小时的事件。下面是一个演示此方法的示例(使用一些随机生成的数据进行演示)

其输出为

根据描述,您需要7个子批次,每个子批次中有两行来自
cOne
的两个类别。但是在这种情况下,
cTwo
cThree
的作用是什么呢。我想清楚地描述期望的输出是有意义的。
cOne:    cTwo:      cThree:     Date:    
car      blue        other     2006-06-12 15:00:00
truck    yellow      other2    2004-05-19 17:00:00
car      red         other3    2012-05-28 09:00:00
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
opts   = [['car', 'truck'],
         ['blue', 'yellow', 'red'],
         ['other', 'other2', 'other3']]
dates = pd.date_range('01-01-2000', '01-01-2001', freq='H')
cOne = list()
cTwo = list()
cThree = list()
n = len(dates)
for ii in range(n):
    cOne.append(opts[0][np.random.randint(0,2)])
    cTwo.append(opts[1][np.random.randint(0,3)])
    cThree.append(opts[2][np.random.randint(0,3)])

df = pd.DataFrame({'cOne': cOne,
                  'cTwo': cTwo,
                  'cThree': cThree,
                  'Date': dates})
df = df.set_index(pd.DatetimeIndex(pd.to_datetime(df['Date'])))
hours = df.index.hour
columnsTitles=["Date", "cOne", "cTwo", "cThree"]
df=df.reindex(columns=columnsTitles)

x = pd.date_range('00:00', '23:00', freq = 'H')
x = range(0,24,1)
rows = len(df.index)
col = df.columns
fig = plt.figure(figsize=(21,3*(len(col)-1)))
fig.suptitle("Hourly Occurence by Day of Week", y = 1)
for mm in range(1, len(col)):
    for ii in range(len(week)):
        y = [[0]*len(x) for i in range(len(opts[mm-1]))]
        for jj in range(rows):
            if dates[jj].dayofweek == ii:
                for kk in range(len(opts[mm-1])):
                    if df[col[mm]][jj] == opts[mm-1][kk]:
                        y[kk][hours[jj]] = y[kk][hours[jj]] + 1
                        break
        ax = fig.add_subplot(len(col)-1, len(week), ii + (mm - 1)*(len(week)) + 1)
        if mm == 1:
            ax.set_title(week[ii])
        for ll in range(len(opts[mm-1])):
            ax.plot(x, y[ll], linewidth=1, linestyle='-', alpha=0.7)
plt.show()