user1367204
user1367204

Reputation: 4797

Is there a way for iPython to generate these kinds of charts given a dataframe?

This picture

Please ignore the background image. The foreground chart is what I am interested in showing using pandas or numpy or scipy (or anything in iPython).

I have a dataframe where each row represents temperatures for a single day. This is an example of some rows:

            100   200   300   400   500   600 ...... 2300
10/3/2013  53*C  57*C  48*C  49*C  54*C  54*C        55*C
10/4/2013  45*C  47*C  48*C  49*C  50*C  52*C        57*C

Is there a way to get a chart that represents the changes from hour to hour using the first column as a 'zero'

Upvotes: 1

Views: 119

Answers (2)

askewchan
askewchan

Reputation: 46530

You could just plot lines one at a time for each row with an offset:

nrows, ncols = 12, 30

# make up some fake data:
d = np.random.rand(nrows, ncols)
d *= np.sin(2*np.pi*np.arange(ncols)*4/ncols)
d *= np.exp(-0.5*(np.arange(nrows)-nrows/2)**2/(nrows/4)**2)[:,None]

#this is all you need, if you already have the data:
for i, r in enumerate(d):
    plt.fill_between(np.arange(ncols), r+(nrows-i)/2., lw=2, facecolor='white')

Overlapping lines

You could do it all at once if you don't need the fill color to block the previous line:

d += np.arange(nrows)[:, None]
plt.plot(d.T)

Not filled

Upvotes: 0

ely
ely

Reputation: 77424

Something quick and dirty that might get you most of the way there, assuming your data frame is named df:

import matplotlib.pyplot as plt
plt.imshow(df.T.diff().fillna(0.0).T.drop(0, axis=1).values)

Since I can't easily construct a sample version with your exact column labels, there might be slight additional tinkering with getting rid of any index columns that are included in the diff and moved with the transposition. But this worked to make a simple heat-map-ish plot for me on a random data example.

Then you can create a matplotlib figure or axis object and specify whatever you want for the x- and y-axis labels.

Upvotes: 2

Related Questions