简体   繁体   中英

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.

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM