python - Plotting an awkward pandas multi index dataframe -
i have awkward dataframe looks this:
+----+------+-------+-------+--------+----+--------+ | | | hour1 | hour2 | hour 3 | … | hour24 | +----+------+-------+-------+--------+----+--------+ | id | date | | | | | | | 1 | 3 | 4 | 0 | 96 | 88 | 35 | | | 4 | 10 | 2 | 54 | 42 | 37 | | | 5 | 9 | 32 | 8 | 70 | 34 | | | 6 | 36 | 89 | 69 | 46 | 78 | | 2 | 5 | 17 | 41 | 48 | 45 | 71 | | | 6 | 50 | 66 | 82 | 72 | 59 | | | 7 | 14 | 24 | 55 | 20 | 89 | | | 8 | 76 | 36 | 13 | 14 | 21 | | 3 | 5 | 97 | 19 | 41 | 61 | 72 | | | 6 | 22 | 4 | 56 | 82 | 15 | | | 7 | 17 | 57 | 30 | 63 | 88 | | | 8 | 83 | 43 | 35 | 8 | 4 | +----+------+-------+-------+--------+----+--------+
for each id
there list of dates
, each date
hour columns represent full day's worth of data broken out hour full 24hrs.
what plot (using matplotlib) full hourly data each of ids
, can't think of way this. looking possibility of creating numpy matrices, i'm not sure if right path go down.
clarification: essentially, each id want concatenate hourly data in order , plot that. have days in proper order, imagine it's matter finding way put of hourly data each id 1 object
any thoughts on how best accomplish this?
here sample data in csv format: http://www.sharecsv.com/s/e56364930ddb3d04dec6994904b05cc6/test1.csv
here 1 approach:
for groupid, data in d.groupby(level='id'): fig = pyplot.figure() ax = fig.gca() ax.plot(data.values.ravel()) ax.set_xticks(np.arange(len(data))*24) ax.set_xticklabels(data.index.get_level_values('date'))
ravel
numpy method string out multiple rows 1 long 1d array.
beware running interactively on large dataset, creates separate plot each line. if want save plots or like, set noninteractive matplotlib backend , use savefig
save each figure, close before creating next one.
Comments
Post a Comment