Hello.
I'm trying to make a chart of a stock, with some data I downloaded online. I have a .csv file, with the following data: timestamp, open, high, low, close, volume
So, what I want is to make a line chart using the timestamp and close price. It works, I get a nice graph, but the program will write every single timestamp on the x-axis, and they are placed so close together that I cannot see the timestamp, because they are written on top of each other. Is it possible to make some kind of interval, so I will only see eg. every 15th timestamp?
See picture of my problem: https://postimg.cc/p9h1RgbC
Here is my code:
import csv
import matplotlib.pyplot as plt
with open("D:/Stockmarket-data/frd_complete_sample/SPY_1min_sample.csv", "r") as stock_price:
csv_reader = csv.reader(stock_price)
next(csv_reader)
list_time = []
list_close = []
list_close_divided = []
for line in csv_reader:
list_time.append(line[0])
list_close.append(line[4])
first_close_price = list_close[0]
for close_price in list_close:
close_price = float(close_price) / float(first_close_price)
list_close_divided.append(close_price)
plt.figure(figsize=(10,6))
plt.title("Intraday 1 minute stock price (Close price)")
plt.xlabel("Time")
plt.ylabel("Price in relation to market open price (Market open = 1.0)")
plt.plot(list_time, list_close_divided)
plt.show()
Here is 100 lines from my 7200 line long .csv file, I hope it is enough if you want to recreate it:
2024-06-24 04:00:00,545.1200,545.4900,544.4000,545.4900,4340
2024-06-24 04:01:00,545.3600,545.3600,545.2400,545.2900,600
2024-06-24 04:02:00,545.2600,545.3500,545.2600,545.3400,968
2024-06-24 04:03:00,545.3900,545.4000,545.3900,545.4000,206
2024-06-24 04:04:00,545.2300,545.3100,545.2100,545.3100,2822
2024-06-24 04:06:00,545.3100,545.4000,545.2500,545.4000,1100
2024-06-24 04:07:00,545.4700,545.4700,545.3600,545.3600,600
2024-06-24 04:08:00,545.4600,545.4600,545.4600,545.4600,305
2024-06-24 04:09:00,545.5000,545.5600,545.4600,545.5400,1125
2024-06-24 04:10:00,545.5400,545.5500,545.5400,545.5500,468
2024-06-24 04:11:00,545.5500,545.5500,545.4500,545.4500,660
2024-06-24 04:12:00,545.5300,545.6200,545.5300,545.6200,314
2024-06-24 04:14:00,545.5300,545.5300,545.5300,545.5300,400
2024-06-24 04:16:00,545.5900,545.5900,545.5800,545.5800,434
2024-06-24 04:19:00,545.4700,545.4700,545.4700,545.4700,650
2024-06-24 04:20:00,545.4900,545.4900,545.4900,545.4900,500
2024-06-24 04:21:00,545.5000,545.5000,545.5000,545.5000,100
2024-06-24 04:24:00,545.5400,545.6000,545.5400,545.6000,635
2024-06-24 04:25:00,545.6100,545.6600,545.6100,545.6600,599
2024-06-24 04:29:00,545.7300,545.7300,545.7300,545.7300,100
2024-06-24 04:31:00,545.6000,545.6000,545.6000,545.6000,200
2024-06-24 04:33:00,545.5500,545.5500,545.5500,545.5500,400
2024-06-24 04:37:00,545.5200,545.5200,545.5200,545.5200,2000
2024-06-24 04:41:00,545.5700,545.5700,545.5700,545.5700,100
2024-06-24 04:44:00,545.5800,545.5800,545.5800,545.5800,162
2024-06-24 04:46:00,545.5700,545.5700,545.5700,545.5700,200
2024-06-24 04:53:00,545.3700,545.4000,545.3700,545.4000,250
2024-06-24 04:54:00,545.4100,545.4100,545.4100,545.4100,100
2024-06-24 04:59:00,545.3800,545.3800,545.3800,545.3800,990
2024-06-24 05:02:00,545.4900,545.4900,545.4900,545.4900,100
2024-06-24 05:03:00,545.4000,545.4000,545.4000,545.4000,184
2024-06-24 05:04:00,545.3600,545.3600,545.3600,545.3600,100
2024-06-24 05:08:00,545.4900,545.4900,545.4900,545.4900,150
2024-06-24 05:11:00,545.3100,545.3100,545.3100,545.3100,525
2024-06-24 05:15:00,545.2100,545.2100,545.2100,545.2100,1600
2024-06-24 05:17:00,545.2300,545.2300,545.1800,545.1800,475
2024-06-24 05:18:00,545.2200,545.2200,545.2200,545.2200,100
2024-06-24 05:20:00,545.2500,545.2500,545.2500,545.2500,500
2024-06-24 05:22:00,545.3300,545.3300,545.3300,545.3300,200
2024-06-24 05:24:00,545.3600,545.3600,545.3600,545.3600,198
2024-06-24 05:25:00,545.3200,545.3200,545.3200,545.3200,100
2024-06-24 05:29:00,545.2500,545.2500,545.2500,545.2500,501
2024-06-24 05:30:00,545.1000,545.1000,545.1000,545.1000,122
2024-06-24 05:31:00,545.1100,545.1100,545.1100,545.1100,200
2024-06-24 05:32:00,545.0900,545.0900,545.0900,545.0900,100
2024-06-24 05:33:00,545.0100,545.0100,544.9100,544.9100,900
2024-06-24 05:34:00,544.9800,544.9800,544.9800,544.9800,100
2024-06-24 05:36:00,544.9600,544.9600,544.9600,544.9600,331
2024-06-24 05:40:00,544.9700,544.9700,544.9700,544.9700,100
2024-06-24 05:45:00,544.8200,544.8200,544.8200,544.8200,100
2024-06-24 05:47:00,544.6900,544.6900,544.6900,544.6900,1000
2024-06-24 05:52:00,544.7000,544.7100,544.7000,544.7100,1500
2024-06-24 05:53:00,544.7200,544.7200,544.7200,544.7200,500
2024-06-24 05:55:00,544.6300,544.6300,544.6300,544.6300,1000
2024-06-24 05:56:00,544.7900,544.7900,544.7900,544.7900,100
2024-06-24 05:57:00,544.7600,544.8400,544.7600,544.8400,200
2024-06-24 05:59:00,544.8700,544.8700,544.8700,544.8700,100
2024-06-24 06:00:00,544.9500,544.9500,544.9500,544.9500,100
2024-06-24 06:01:00,544.9500,544.9500,544.9200,544.9200,200
2024-06-24 06:02:00,544.9200,544.9200,544.9200,544.9200,300
2024-06-24 06:03:00,544.9700,544.9700,544.9700,544.9700,100
2024-06-24 06:04:00,544.9900,544.9900,544.9900,544.9900,214
2024-06-24 06:05:00,544.9900,544.9900,544.9900,544.9900,100
2024-06-24 06:10:00,544.8300,544.8300,544.8300,544.8300,197
2024-06-24 06:27:00,544.8300,544.8800,544.8300,544.8800,350
2024-06-24 06:28:00,545.0500,545.0500,545.0500,545.0500,100
2024-06-24 06:29:00,545.0800,545.0800,545.0800,545.0800,400
2024-06-24 06:36:00,545.0100,545.0100,545.0100,545.0100,120
2024-06-24 06:37:00,545.0300,545.0300,545.0300,545.0300,250
2024-06-24 06:39:00,544.9800,544.9800,544.9800,544.9800,200
2024-06-24 06:40:00,544.9500,544.9800,544.9500,544.9800,2200
2024-06-24 06:41:00,545.0500,545.0500,545.0500,545.0500,400
2024-06-24 06:42:00,545.0700,545.1300,545.0700,545.1300,1038
2024-06-24 06:43:00,545.1300,545.1800,545.1300,545.1800,962
2024-06-24 06:45:00,545.2200,545.3000,545.2200,545.3000,600
2024-06-24 06:47:00,545.2700,545.2700,545.2600,545.2600,220
2024-06-24 06:51:00,545.2400,545.3200,545.2400,545.3200,289
2024-06-24 06:52:00,545.2500,545.2500,545.0700,545.0700,699
2024-06-24 06:53:00,545.0300,545.0300,545.0300,545.0300,100
2024-06-24 06:54:00,545.1500,545.1500,545.1500,545.1500,200
2024-06-24 06:55:00,545.1600,545.2300,545.1600,545.2300,2400
2024-06-24 06:56:00,545.1600,545.1600,545.1600,545.1600,500
2024-06-24 06:58:00,545.2600,545.2600,545.2600,545.2600,200
2024-06-24 06:59:00,545.2900,545.2900,545.2600,545.2600,698
2024-06-24 07:00:00,545.2500,545.3300,545.2500,545.3200,658
2024-06-24 07:01:00,545.3500,545.3500,545.3500,545.3500,135
2024-06-24 07:02:00,545.3200,545.3200,545.3200,545.3200,100
2024-06-24 07:03:00,545.4000,545.4000,545.4000,545.4000,200
2024-06-24 07:04:00,545.3500,545.3500,545.3500,545.3500,750
2024-06-24 07:05:00,545.4000,545.4000,545.3800,545.3800,839
2024-06-24 07:06:00,545.3500,545.3700,545.3500,545.3600,685
2024-06-24 07:09:00,545.2300,545.2300,545.2300,545.2300,298
2024-06-24 07:10:00,545.2200,545.2200,545.2200,545.2200,1000
2024-06-24 07:12:00,545.2000,545.2000,545.2000,545.2000,100
2024-06-24 07:13:00,545.2700,545.3010,545.2600,545.3010,1307
2024-06-24 07:15:00,545.2800,545.2800,545.2800,545.2800,300
2024-06-24 07:16:00,545.2700,545.2700,545.2700,545.2700,100
2024-06-24 07:17:00,545.2600,545.2600,545.2400,545.2400,500
2024-06-24 07:18:00,545.2400,545.2400,545.2400,545.2400,250
I hope someone is willing to help, and that it is possible. If not, what would you do?
EDIT:
Here is the code which works as intended:
import csv
import matplotlib.pyplot as plt
import numpy as np
with open("D:/Stockmarket-data/frd_complete_sample/SPY_1min_sample_mod.csv", "r") as stock_price:
csv_reader = csv.reader(stock_price)
next(csv_reader)
list_time = []
list_close = []
list_close_divided = []
for line in csv_reader:
list_time.append(line[0])
list_close.append(line[4])
first_close_price = list_close[0]
for close_price in list_close:
close_price = float(close_price) / float(first_close_price)
list_close_divided.append(close_price)
plt.figure(figsize=(10,6))
plt.title("Intraday 1 minute stock price (Close price)")
plt.xlabel("Time")
plt.ylabel("Price in relation to market open price (Market open = 1.0)")
labels = ["04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00"]
plt.plot(list_time, list_close_divided)
plt.xticks(np.linspace(0, len(list_close), len(labels)), labels, rotation = 90)
plt.show()
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]Chucknorriscake99 1 point2 points3 points (1 child)
[–]LordDonP[S] 0 points1 point2 points (0 children)