0

I have an application I'm developing where returning a Pandas dataframe of np.nan's with a DatetimeIndex, is a valid outcome. There's no data for the query, and I need the plot to form an axis with a DatetimeIndex with no data points. This breaks matplotlib's idea of the bounding box which gets it's limits from the data cast into floats.

I'm thinking there should be a work around that isn't ugly, like a call to set the bbox limits, but so far I haven't figured it out.

For the time being, I'm joining the dataframe with another that has zeros, then looping over the columns to set line style to None for the dummy column.

# dummy must also be moved to the end of the dataframe to not
# not break the legend in my app.
ax.plot(df)
dummy_line = ax.get_lines()[-1].set_linestyle('None')

This is really ugly hack that makes the code more complex and forces special functions to abstract this.

Is there an easier work around more in line with Matplotlib's built in functionality ?

Example

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

index = pd.date_range('2019-01-01T12:00:00', '2019-01-2T12:00:00', freq='3T')
data = [np.nan for i in index]
df = pd.DataFrame({'test':data}, index=index)
fig, ax = plt.subplots(nrows=1)
ax.plot(df)
plt.show()
# Ultimately I want to use date Locators which offer a different
# error b/c there's too many ticks.

Version Info

  • matplotlib 2.0.2 np113py36_0
  • Python 3.x
wbg
  • 866
  • 3
  • 14
  • 34
  • `ax.set_xlim()` and proper [datetime formatting](https://stackoverflow.com/a/14946444/2454357) for the ticks? – Thomas Kühn Feb 03 '19 at 18:48
  • That doesn't work unfortunately. There has to be valid x limits which are picked off the bbox. I'll try again and confirm though, I'm kinda lost in my test variations. – wbg Feb 03 '19 at 20:43

0 Answers0