5

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

chrise
  • 4,039
  • 3
  • 39
  • 74

2 Answers2

4

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.

ptrj
  • 5,152
  • 18
  • 31
  • if i set them as string then that loses a lot of information and the xticks become a mess as it tries to present every single one – evan54 Jun 04 '21 at 19:34
0

plt.xticks(x,labels) seems to work but is super slow for large dataset.. Here are the links for something similar to this problem:

Meesam Ali
  • 29
  • 4
  • AFAIK, the OP was asking how to get rid of the gaps when plotting timestamps, not how to rotate tick labels. – Rafael-WO Oct 08 '21 at 07:11