Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %matplotlib inline
- import matplotlib.pyplot as plt
- import numpy as np
- 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(name_of_file):
- date_data = []
- stock_data_interim = []
- file = open(name_of_file, "r")
- file_list = list(file)
- # date
- for element in file_list:
- if " \"2020" in element:
- date_string = element[9:28]
- date_data.append(date_string)
- # stock prices/volumes
- for element in file_list:
- for x in ["1. open", "2. high", "4. close", "5. volume"]:
- if "\"" + x in element:
- end_of_line = element.rfind("\"")
- stock_data_interim.append(element[24:end_of_line])
- # separately coding "3. low", because it is somehow formatted differently in the text file
- for x in ["3. low"]:
- if "\"" + x in element:
- end_of_line = element.rfind("\"")
- stock_data_interim.append(element[23:end_of_line])
- # remove parantheses and spaces
- stock_data_output = []
- for element in stock_data_interim:
- if "\"" or " " in element:
- new_string = element.replace("\"", "").replace(" ", "")
- stock_data_output.append(new_string)
- else:
- stock_data_output.append(element)
- file.close()
- date, closep, highp, loewp, openp, volume = np.loadtxt(stock_data_output,
- delimiter=",",
- unpack=True,
- converters={0: bytespdate2num("%Y-%m-%d")})
- plt.plot_date(date, closep)
- plt.xlabel("Date")
- plt.ylabel("Price")
- plt.title("Interesting Graph\nCheck it out!")
- plt.legend()
- plt.show()
- graph_data("Boring stock market price.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement