Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matplotlib timestamp
#1
I'm not able to attach my code here. 
This is about converting standard date formats to matplotlib's own kind of format (something gregorian calendar related), I tried to solve it by reading about it in the official documentation but that was too much to swallow. If anyone here has a basic concept about it, please help with a healthy discussion down there. Thank you!

http://pastebin.com/u2Yk3EGD  My code.
import matplotlib.pyplot as plt
import numpy as np
import urllib
import matplotlib.dates as mdates

def bytespdate2num(fmt, encoding='utf-8'):
   strconverter = mdates.strpdate2num(fmt)
   def bytesconverter(b):
       s = b.decode(encoding)
       return strconverter(s)
   return bytesconverter
   

def graph_data(stock):

   stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'
   source_code = urllib.request.urlopen(stock_price_url).read().decode()
   stock_data = []
   split_source = source_code.split('\n')
   for line in split_source:
       split_line = line.split(',')
       if len(split_line) == 6:
           if 'values' not in line and 'labels' not in line:
               stock_data.append(line)

   date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
                                                         delimiter=',',
                                                         unpack=True,
                                                         # %Y = full year. 2015
                                                         # %y = partial year 15
                                                         # %m = number month
                                                         # %d = number day
                                                         # %H = hours
                                                         # %M = minutes
                                                         # %S = seconds
                                                         # 12-06-2014
                                                         # %m-%d-%Y
                                                         converters={0: bytespdate2num('%Y%m%d')})

   plt.plot_date(date, closep,'-', label='Price')

   plt.xlabel('Date')
   plt.ylabel('Price')
   plt.title('Interesting Graph\nCheck it out')
   plt.legend()
   plt.show()


graph_data('TSLA')
Even if you are able to make me understand the working of 'bytespdate2num' function in the above code will be helpful.
Reply
#2
You can code your own converter.
Then it is easier to understand what is going on:

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

# convert "yyyymmdd" to float
def yyyymmdd2num(s):
    return mdates.date2num(datetime.datetime(int(s[0:4]), int(s[4:6]), int(s[6:8])))

def graph_data(stock):
   stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'
   source_code = urllib.request.urlopen(stock_price_url).read().decode()
   stock_data = []
   split_source = source_code.split('\n')
   for line in split_source:
       split_line = line.split(',')
       if len(split_line) == 6:
           if 'values' not in line and 'labels' not in line:
               stock_data.append(line)

   date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
                                                         delimiter=',',
                                                         unpack=True,
                                                         converters={0: yyyymmdd2num}) # converter for column 0

   plt.plot_date(date, closep,'-', label='Price')

   plt.xlabel('Date')
   plt.ylabel('Price')
   plt.title('Interesting Graph\nCheck it out')
   plt.legend()
   plt.show()

graph_data('TSLA')
Reply
#3
What i know is that matplotlib doesn't supports the standard dates, it has some other formats. Have tried running it in the interpreter?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in timestamp Led_Zeppelin 3 3,201 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,006 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,220 Mar-11-2021, 10:52 AM
Last Post: buran
  Timestamp is undefined ErnestTBass 7 7,928 Feb-16-2019, 08:27 PM
Last Post: snippsat
  timestamp not updating bowen73 3 7,182 Aug-20-2017, 11:13 PM
Last Post: bowen73

Forum Jump:

User Panel Messages

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