Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 problem in plotting intraday results using matplotlib
#1
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.

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')
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Matplotlib problem ift38375 0 153 Jul-02-2019, 02:55 PM
Last Post: ift38375
  Matplotlib animation problem Potatoez 1 398 May-21-2019, 03:58 PM
Last Post: Potatoez
  Problem installing numpy and matplotlib achondrite 1 448 Jan-16-2019, 11:43 PM
Last Post: snippsat
  Stack problem, repeating results that i dont want YapYL 1 745 Dec-10-2017, 04:14 PM
Last Post: hshivaraj

Forum Jump:


Users browsing this thread: 1 Guest(s)