Python Forum
numpy.float64 and plot_date in matplotlib
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numpy.float64 and plot_date in matplotlib
#1
Hi,

I am trying to draw the data of IBM stock and I have problems with date formatting.
Format of raw data is as follows:

COLUMNS=DATE,CLOSE,HIGH,LOW,OPEN,VOLUME
a1496928660,151.0583,151.23,151,151,67556
1,151.08,151.12,150.92,151.0552,10547
2,151.2964,151.3,151,151.08,17150
...

Raw that starts with 'a' is actually UNIX time for which I made converter. Variable
'source_data' is tuple

I am having problem to plot data with matplotlib function plot_date(source_data,closep)
I create 'date' variable with numpy.loadtxt function and then function plot_date can not accept it.
Error that I get is:

ValueError: year 55224 is out of range

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import urllib.request as r
from datetime import datetime

import tzlocal


def bytespdate2num(fmt, encoding='utf-8'):
   str_converter = mdates.strpdate2num(fmt)

   def bytes_converter(b):
       s = b.decode(encoding)
       return str_converter(s)

   return bytes_converter


def graph_data(stock):
   print('Currently pulling:', stock)
   url = 'URL string here'
   url1 = url.encode('utf-8')
   print(url1)

   csv = r.urlopen(url).readlines()
   source_data = []
   stock_data = []

   for bar in range(7, len(csv)):
       # print(csv[bar],'\n')
       tt = csv[bar].decode()
       # print(tt, '\n')


       if tt.count(',') != 5: continue
       offset, close, high, low, open_, volume = tt.split(',')

       if offset[0] == 'a':
           day = float(offset[1:])
           offset = 0
           dt = datetime.fromtimestamp(day + (240 * offset))
           # print(dt.strftime("%Y-%m-%d %H:%M:%S.%f%z (%Z)"), close, high, low, open_, volume)
           # source_data=dt.strftime("%Y-%m-%d %H:%M:%S.%f%z (%Z)"), close, high, low, open_, volume
           source_data = dt.strftime('%Y%m%d'), close, high, low, open_, volume
       else:
           offset = float(offset)
           open_, high, low, close = [float(x) for x in [open_, high, low, close]]
           dt = datetime.fromtimestamp(day + (240 * offset))
           # print(dt.strftime("%Y-%m-%d %H:%M:%S.%f%z (%Z)"),close, high, low, open_, volume)
           # source_data = dt.strftime("%Y-%m-%d %H:%M:%S.%f%z (%Z)"), close, high, low, open_, volume
           source_data = dt.strftime('%Y%m%d'), close, high, low, open_, volume


       #date = np.loadtxt(source_data, delimiter=',', unpack=True,
        #       converters={0: bytespdate2num('%Y%m%d')})


       date, closep, highp, lowp, openp, volumep = np.loadtxt(source_data, delimiter=',', unpack=True)
       print(type(date))


   plt.plot_date(date, closep)
   plt.show()
    
graph_data('IBM')
Reply


Messages In This Thread
numpy.float64 and plot_date in matplotlib - by iv20023 - Jun-21-2017, 06:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 893 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,766 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  numpy.float64 object is not iterable jcdr91 1 13,344 Apr-22-2020, 04:09 PM
Last Post: jefsummers
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 3,089 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  ValueError: Input contains infinity or a value too large for dtype('float64') Rabah_r 1 12,991 Apr-06-2019, 11:08 AM
Last Post: scidam
  Numpy and matplotlib in incorporated Python. CSKir 7 5,717 Jan-15-2019, 10:44 AM
Last Post: CSKir
  ???Can not convert from <U25 to float64.??? damsel_pie 6 7,033 Aug-23-2018, 04:49 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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