Aug-20-2018, 11:32 AM
Hi All,
I try to plot the intraday price for a certain stock.
I have a problem to set the x-axis results. I highlighted the line that cause a problem in comment below.
If I comment this line of code , the program works well but the x-axis has wrong values.
My target is to plot the results and have the x-axis to show the hour-min-sec.
I try to plot the intraday price for a certain stock.
I have a problem to set the x-axis results. I highlighted the line that cause a problem in comment below.
If I comment this line of code , the program works well but the x-axis has wrong values.
My target is to plot the results and have the x-axis to show the hour-min-sec.
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
from mpl_finance import candlestick_ohlc
import json
import urllib
import datetime
def graph_data(stock):
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
myURL = "https://query1.finance.yahoo.com/v8/finance/chart/"+stock+"?symbol="+stock+"&interval=5m"
print(myURL)
with urllib.request.urlopen(myURL) as url:
data = json.loads(url.read().decode())
x = 0
y = len(data['chart']['result'][0]['timestamp'])
ohlc = []
while x < y:
append_me = x, data['chart']['result'][0]['indicators']['quote'][0]['open'][x], data['chart']['result'][0]['indicators']['quote'][0]['high'][x], data['chart']['result'][0]['indicators']['quote'][0]['low'][x], data['chart']['result'][0]['indicators']['quote'][0]['close'][x], data['chart']['result'][0]['indicators']['quote'][0]['volume'][x]
ohlc.append(append_me)
x+=1
candlestick_ohlc(ax1, ohlc, width=0.4, colorup='green', colordown='red')
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) # Problem exists in this line
ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
ax1.grid(True)
plt.xlabel('Date')
plt.ylabel('Price')
plt.title(stock)
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()
graph_data('MU')
