Python在matplotlib中设置x轴数据帧时出现问题
我当前的图表正确显示了全部数据 但是,我在设置图表上的x轴时遇到问题。我的代码如下:Python在matplotlib中设置x轴数据帧时出现问题,python,datetime,dataframe,matplotlib,Python,Datetime,Dataframe,Matplotlib,我当前的图表正确显示了全部数据 但是,我在设置图表上的x轴时遇到问题。我的代码如下: 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
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()
这给了我一个错误
Blockquote AttributeError:模块“matplotlib.pyplot”没有属性“setxlim”
我真的希望我的图表显示时间戳是从2017-12-17 22:00:00到最晚时间
数据示例如下所示:
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
谢谢您似乎需要:
#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()
我仍然有完整的日期。另外,我只想从2017-12-17 22:00:00到最近的时间(不仅仅是日期?)。很难回答的问题是,
ax.set\u xlim([datetime.date(2017,12,17,22,0,0),df.index.max()])
?