Python Forum
problem in plotting intraday results using matplotlib
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  plotting based on the results of mathematical formulas Timur 1 295 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  Yfinance - Intraday stock data with yf.download diogo_80 2 5,870 Apr-29-2022, 05:07 AM
Last Post: processingclouds
  Floor division problem with plotting x-axis tick labels Mark17 5 2,047 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Matplotlib Problem DaveG 0 1,564 Mar-14-2022, 04:05 AM
Last Post: DaveG
  matplotlib Plotting smooth line with nans mikisDeWitte 4 3,034 Mar-11-2022, 02:40 PM
Last Post: mikisDeWitte
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,161 Mar-11-2021, 10:52 AM
Last Post: buran
  How to plot intraday data of several days in one plot mistermister 3 2,854 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  Problem with matplotlib ncm1234 1 1,462 Sep-01-2020, 02:16 AM
Last Post: bowlofred
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,188 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 3,072 Jan-02-2020, 12:11 PM
Last Post: Killertjuh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020