1

My current graph is displaying the full range of data correctly.

I am having problems setting the x-axis on my graph though. My code is the following:

prices['Timestamp'] = pd.to_datetime(prices['Timestamp'], format='%Y-%m-%d %H:%M:%S')
prices['Timestamp'] = pd.DatetimeIndex(prices['Timestamp'])
prices.index = pd.to_datetime(prices['Timestamp'], format='%Y-%m-%d %H:%M:%S')


prices['Close'].plot()

plt.set_xlim([datetime.date(2017, 12, 17), datetime.date(2017, 12, 18)])


plt.legend()
plt.show()

This gives me the error

Blockquote AttributeError: module 'matplotlib.pyplot' has no attribute 'set_xlim'

I really want my graph to show the timestamp to be from 2017-12-17 22:00:00 to latest time.

Sample of the data looks like:

print(prices['Close']

2017-12-18 07:13:00    3.030000e-06
2017-12-18 07:14:00    3.020000e-06
2017-12-18 07:15:00    3.030000e-06
2017-12-18 07:16:00    3.040000e-06

Thanks

Dave Sev
  • 35
  • 2
  • 7

1 Answers1

1

It seems you need:

#set column to datetime
prices['Timestamp'] = pd.to_datetime(prices['Timestamp'], format='%Y-%m-%d %H:%M:%S')
#set index from column
prices = prices.set_index('Timestamp')
#plot column, return matplotlib AxesSubplot object
ax = prices['Close'].plot()
#set xlim
ax.set_xlim([datetime.date(2017, 12, 17), datetime.date(2017, 12, 18)])

plt.show()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I still get the full range of dates. also I only want from 2017-12-17 22:00:00 to latest time (not just dates?). – Dave Sev Dec 18 '17 at 15:50
  • Hard question, what about `ax.set_xlim([datetime.date(2017, 12, 17, 22, 0, 0), df.index.max()])` ? – jezrael Dec 18 '17 at 15:52