Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting pandas intraday time series only for periods with data

I have a series of intraday measurements. measurements are only taken during daytime on weekdays. when I plot the data, pandas extends the xaxis over the full time horizont, so the plot shows data gaps

dfb.loc[:,("value", "exp_1")].plot()

enter image description here

I can tell pandas/matplotlib to ignore the index and the plot is fine, but I would like to present the dates on the x axis

    dfb.loc[:,("value", "exp_1")].plot(ignore_index=True)

enter image description here

I also tried to define the xticks with my index, but that leads to teh first chart with a cluttered x axis description

    dfb.loc[:,("value", "exp_1")].plot(xticks=dfb.index)

enter image description here

Is there a way to get a plot like the 2nd plot while keeping the dates?

EDIT: Here is a subset of the data and the plot

enter image description here

like image 435
chrise Avatar asked Oct 29 '22 22:10

chrise


1 Answers

It looks a bit like a bug. You may also consider creating an issue on https://github.com/pydata/pandas/

When I tried it with a DatetimeIndex with frequency set to days or business days then the x axis isn't affected. But if the frequency is finer (e.g. hours, business hours), then the axis does get extended - exactly as in your case.

A workaround (not sure if the best possible) would be to convert the index of the data frame to strings. Either setting it directly:

df.index = df.index.astype('str')

or maybe better changing only for printing:

df.set_index(df.index.astype('str')).plot(rot=30)

The argument rot=30 is optional, but without it the labels get cluttered.

like image 91
ptrj Avatar answered Nov 15 '22 06:11

ptrj