ValientProcess
ValientProcess

Reputation: 1801

Arrange pandas DataFrame for color Plotting

I have a dataframe which looks like this (left column is the index):

YYYY-MO-DD HH-MI-SS_SSS   ATMOSPHERIC PRESSURE (hPa) mean
2016-11-07 14:00:00       1014.028782
2016-11-07 15:00:00       1014.034111
   ....                        ....
2016-11-30 09:00:00       1006.516436
2016-11-30 10:00:00       1006.216156

Now I want to plot a colormap with this data - so I want to create an X (horizontal axis) to be just the dates:

2016-11-07, 2016-11-08,...,2016-11-30

and the Y (Vertical axis) to be the time:

00:00:00, 01:00:00, 02:00:00, ..., 23:00:00

And finally the Z (color map) to be the pressure data for each date and time [f(x,y)].

How can I arrange the data for this kind of plotting ?

Thank you !

Upvotes: 0

Views: 80

Answers (1)

Pablo
Pablo

Reputation: 1003

With test data prepared like so:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

samples = 24 * 365
index = pd.date_range('2017-01-01', freq='1H', periods=samples)
data = pd.DataFrame(np.random.rand(samples), index=index, columns=['data'])

I would do something like this:

data = data.reset_index()
data['date'] = data['index'].apply(lambda x: x.date())
data['time'] = data['index'].apply(lambda x: x.time())

pivoted = data.pivot(index='time', columns='date', values='data')

fig, ax = plt.subplots(1, 1)

ax.imshow(pivoted, origin='lower', cmap='viridis')

plt.show()

Which produces:

enter image description here

To improve the axis labeling, this is a start:

ax.set_yticklabels(['{:%H:%M:%S}'.format(t) for t in data['time'].unique()])
ax.set_xticklabels(['{:%Y-%m-%d}'.format(t) for t in data['date'].unique()])

but you'll need to figure out how to choose how often a label appears with set_xticks() and set_yticks()

Upvotes: 2

Related Questions