[英]Why am I not able to get proper dates in my graph?
I am not able to get the dates in correct form in my output.It is concerned with plottng of 3 stock data (opening price) for 4 days on single axis. 我无法在输出中以正确的形式获取日期,这与3个股票数据(开盘价)在单轴上连续4天的绘制有关。
My code is 我的代码是
# Import matplotlib.pyplot
import matplotlib.pyplot as plt
from datetime import date
from nsepy import get_history
avenue_df=get_history(symbol='DMART',start=date(2018,5,6),end=date(2018,5,10))
avenue_df.Open.plot(color='green', label='DMART')
shriram_df = get_history(symbol='SRTRANSFIN',start=date(2018,5,6),end=date(2018,5,10))
shriram_df.Open.plot(color='red', label='SHRI')
infy_df = get_history(symbol='INFY',start=date(2018,5,6),end=date(2018,5,10))
infy_df.Open.plot(color='blue', label='INFY')
# Add a legend in the top left corner of the plot
plt.legend(loc='upper left')
# Display the plot
plt.show()
My output is 我的输出是
You can use the DayLocator and DateFormatter from matplotlib.dates 您可以使用matplotlib.dates中的DayLocator和DateFormatter
import matplotlib.pyplot as plt
from datetime import date
from nsepy import get_history
avenue_df=get_history(symbol='DMART',start=date(2018,5,6),end=date(2018,5,10))
avenue_df.Open.plot(color='green', label='DMART')
shriram_df = get_history(symbol='SRTRANSFIN',start=date(2018,5,6),end=date(2018,5,10))
shriram_df.Open.plot(color='red', label='SHRI')
infy_df = get_history(symbol='INFY',start=date(2018,5,6),end=date(2018,5,10))
ax = infy_df.Open.plot(color='blue', label='INFY')
# Add a legend in the top left corner of the plot
plt.legend(loc='upper left')
# Display the plot
#Format the xaxis date
from matplotlib.dates import DateFormatter, DayLocator
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_major_formatter(DateFormatter('%Y/%m/%d'))
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.