pandas_datareader无法在x轴上打印日期

时间:2019-01-08 19:14:47

标签: python python-3.x pandas-datareader

在以下绘图中,日期未在x轴上显示怎么显示日期时间戳

import  numpy as np
import pandas as pd
import  matplotlib.pyplot as plt
import datetime
import pandas_datareader as web
start=datetime.datetime(2014,1,8)
end =datetime.datetime(2019,1,8)
Tesla=web.DataReader('TSLA',"iex",start,end)
Ford=web.DataReader('F',"iex",start,end)
Tesla["open"].plot(label="Tesla",title="opening Price",figsize=(16,8))
plt.legend()
plt.show()

1 个答案:

答案 0 :(得分:0)

您的数据框索引创建为dtype对象或字符串,让我们使用pd.to_datetime将索引转换为DatetimeIndex:

import  numpy as np
import pandas as pd
import  matplotlib.pyplot as plt
import datetime
import pandas_datareader as web
start=datetime.datetime(2014,1,8)
end =datetime.datetime(2019,1,8)
Tesla=web.DataReader('TSLA',"iex",start,end)
Ford=web.DataReader('F',"iex",start,end)

#Change Telsa index to datetime dtype
Tesla.index = pd.to_datetime(Tesla.index)

#Let's do Fords too
Ford.index = pd.to_datetime(Ford.index)

Tesla["open"].plot(label="Tesla",title="opening Price",figsize=(16,8))
plt.legend()
plt.show()

输出:

enter image description here