Reputation: 669
I have values (np.array
) I want to plot against index (DateTimeIndex
) with ax.plot_date
. Both have the same length.
>>>index
DatetimeIndex(['1997-12-30', '1997-12-31', '1998-01-01', '1998-01-02',
'1998-01-05', '1998-01-06', '1998-01-07', '1998-01-08',
'1998-01-09', '1998-01-12',
...
'2015-12-29', '2015-12-30', '2015-12-31', '2016-01-01',
'2016-01-04', '2016-01-05', '2016-01-06', '2016-01-07',
'2016-01-08', '2016-01-11'],
dtype='datetime64[ns]', length=4705, freq='B')
>>>values
array([ nan, nan, nan, ..., 1.40211106,
1.46409254, 1.36151557])
Now ax.plot_date(index, values)
works fine except it will just clip the line at the beginning (the nan's) where I want to have the gap instead. I have no idea how to achieve this.
Upvotes: 3
Views: 1061
Reputation: 2847
You need to set the xtick labels manually, here's an example with only 4 items in the index, and 2 values in values:
import pandas as pd
import pylab as plt
import numpy as np
#my made up data
index = pd.DatetimeIndex(['1997-12-30', '1997-12-31', '1998-01-01',
'1998-01-02'])
values = np.array([np.nan, np.nan, 1.402, 1.464])
#we are actually using this array as the x values in our graph, then replacing the labels with dates
x = np.arange(len(index))
#create plot
fig, ax = plt.subplots()
ax.set_xlim([0,len(index)])
#create axes with dates along the x axis
ax.set(xticks=x, xticklabels = index)
#plot values with the corresponding dates
ax.plot_date(x, values)
#show plot
plt.show()
Upvotes: 2