0

How can I plot a timeseries with timestamps on the x-axis and "data" that contains only NAN on the y-axis using matplotlib?

The goal is to get an empty plot with timestamps on the x-axis and an empty y-axis. Why do I want to have an empty plot? Well, I am creating plots in a loop for every day of the year and it can happen that the timeseries has data gaps (no, interpolation of data gaps makes no sence).

    TIMESTAMP
2019-10-21 12:00:00   NaN
2019-10-21 12:01:00   NaN
2019-10-21 12:02:00   NaN
2019-10-21 12:03:00   NaN
2019-10-21 12:04:00   NaN
                       ..
2019-10-22 11:55:00   NaN
2019-10-22 11:56:00   NaN
2019-10-22 11:57:00   NaN
2019-10-22 11:58:00   NaN
2019-10-22 11:59:00   NaN
Freq: T, Name: df_plot.prec, Length: 1440, dtype: float64

DatetimeIndex(['2019-10-21 12:00:00', '2019-10-21 12:01:00',
               '2019-10-21 12:02:00', '2019-10-21 12:03:00',
               '2019-10-21 12:04:00', '2019-10-21 12:05:00',
               '2019-10-21 12:06:00', '2019-10-21 12:07:00',
               '2019-10-21 12:08:00', '2019-10-21 12:09:00',
               ...
               '2019-10-22 11:50:00', '2019-10-22 11:51:00',
               '2019-10-22 11:52:00', '2019-10-22 11:53:00',
               '2019-10-22 11:54:00', '2019-10-22 11:55:00',
               '2019-10-22 11:56:00', '2019-10-22 11:57:00',
               '2019-10-22 11:58:00', '2019-10-22 11:59:00'],
              dtype='datetime64[ns]', name='TIMESTAMP', length=1440, freq='T')

plt.plot(df_plot.index, df_plot.prec)

results in:

ValueError: view limit minimum -0.001 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

timeseries is of type datetime64

eetiaro
  • 342
  • 1
  • 3
  • 14
  • Please post some sample data as well as more of the code that you're using to create the plot. More details can be found [here](https://stackoverflow.com/help/minimal-reproducible-example). – m13op22 Mar 31 '20 at 13:57
  • https://stackoverflow.com/questions/36455083/working-with-nan-values-in-matplotlib check out this – CodeTalker Mar 31 '20 at 14:04
  • @CodeTalker: The link you suggested is about gap filling and interpolation, I don't want to gap fill, I just want to have an empty plot and no ValueError of course – eetiaro Mar 31 '20 at 14:13
  • What is `type(df_plot)` for you? – m13op22 Mar 31 '20 at 14:24
  • @HS-nebula: df_plot is a pandas dataframe with TIMESTAMP as index – eetiaro Mar 31 '20 at 14:28
  • Are you setting limits to the axes? – m13op22 Mar 31 '20 at 18:12
  • @HS-nebula: Yes: plt.ylim(top=1); plt.ylim(bottom=0); I solved this issue now by checking with an "if" if there are only NaN, if that condtion is true it prepares an empty plot, it works, but it is a workaround not a complete solution... – eetiaro Apr 01 '20 at 09:51
  • Have a look at this [question and answer](https://stackoverflow.com/questions/53245199/valueerror-view-limit-minimum-0-0-is-less-than-1-and-is-an-invalid-matplotlib-d). The limits may be giving you that error. – m13op22 Apr 01 '20 at 14:28

0 Answers0