我试图用Ticker作为标题(即IVOG)和日期作为x轴来绘制这个多索引的熊猫数据帧。我只想要x轴的月份和年份。我想至少把它去掉显示时间。你知道吗
df[['Adj_Close', 'MA(3)', 'EWMA(3)']]
Adj_Close MA(3) EWMA(3)
Ticker Date
IVOG 2017-04-30 47.0319 47.031900 47.031900
2017-05-31 47.4419 47.236900 47.305233
2017-06-30 46.3553 46.943033 46.762414
2017-07-31 49.6553 47.817500 48.305287
2017-08-31 52.5453 49.518633 50.493681
2017-09-30 55.5353 52.578633 53.054503
2017-10-31 57.3753 55.151967 55.231913
2017-11-30 55.5353 56.148633 55.384201
2017-12-31 59.6556 57.522067 57.524080
2018-01-31 61.7056 58.965500 59.616884
2018-02-28 58.8156 60.058933 59.216046
2018-03-31 58.9600 59.827067 59.087992
2018-04-30 58.9000 58.891867 58.993984
2018-05-31 59.0000 58.953333 58.996992
我已经尝试了很多事情下面的代码是我得到的最接近。我把数据帧转换成dict,这样我就可以把它呈现在这里。这样做时,我丢失了索引头和日期格式。但现在它应该是可以复制的。你知道吗
import datetime as dt
import pandas as pd
import time
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
data = {("IVOG",1493510400000):{"Adj_Close":119.2136,"MA(3)":119.2136,"EWMA(3)":119.2136},
("IVOG",1496188800000):{"Adj_Close":120.8236,"MA(3)":120.0186,"EWMA(3)":120.0454},
("IVOG",1498780800000):{"Adj_Close":120.2736,"MA(3)":120.1036,"EWMA(3)":120.1266},
("IVOG",1501459200000):{"Adj_Close":121.7836,"MA(3)":120.5236,"EWMA(3)":120.5832},
("IVOG",1504137600000):{"Adj_Close":120.3536,"MA(3)":120.4896,"EWMA(3)":120.5309},
("IVOG",1506729600000):{"Adj_Close":124.3336,"MA(3)":121.1303,"EWMA(3)":121.2749}}
df=pd.DataFrame.from_dict(data, orient = 'index')
print(df)
ax = plt.gca() # get current axis
df.plot(kind='line',y='Adj_Close', ax=ax)
df.plot(kind='line',y='MA(3)',ax=ax)
df.plot(kind='line',y='EWMA(3)', color='green', ax=ax)
ax.format_xdata = mdates.DateFormatter('%b-%Y') # Trying to get APR-2018
plt.xlabel(df.index[0]) # Trying to Get the Ticker
_=plt.grid()
_=plt.xticks(rotation=90)
plt.show()
相反,它是这样绘制的: Plot of data
目前没有回答
相关问题 更多 >
编程相关推荐