0

Get stock aaa via web api:

import requests,json,time
import pandas as pd
from pandas import json_normalize
ticker = 'aaa'
start_date='2020-01-01'
end_date='2023-05-10'
fd = int(time.mktime(time.strptime(start_date, "%Y-%m-%d")))
td = int(time.mktime(time.strptime(end_date, "%Y-%m-%d")))
data = requests.get('https://apipubaws.tcbs.com.vn/stock-insight/v1/stock/bars-long-term?ticker={}&type=stock&resolution=D&from={}&to={}'.format(ticker, fd, td)).json()
df = json_normalize(data['data'])
df['tradingDate'] = pd.to_datetime(df.tradingDate.str.split("T", expand=True)[0])
df.columns = ['open', 'high', 'low', 'close', 'volume', 'date']

Prepare x,y data for plotting:

x = df['date'][0:30]
y = df['close'][0:30]

Draw the graph:

plt.plot(x,y)
plt.xticks(rotation=90)
plt.show()

There are 30 dates in the raw data (x,y),plt.show() only draw part of date,how can draw all the 30 dates in x axis?

>>> min(x)
Timestamp('2020-01-02 00:00:00')
>>> max(x)
Timestamp('2020-02-19 00:00:00')
>>> len(x)
30

I want to write every date from 2020-01-02 till 2020-02-19 ,30 dates shown on the x axis.

showkey
  • 482
  • 42
  • 140
  • 295
  • Your data doesn't have a row for every calendar day (no data for maybe Saturday and Sunday)... are you expecting x-axis labels for all dates (every day from Jan 2nd to Feb 19th) or only the 30 entries for which data is available? – Redox May 18 '23 at 13:04
  • I want to write every date from 2020-01-02 till 2020-02-19 ,30 dates shown on the x axis. – showkey May 18 '23 at 13:07
  • If there is a ticklabel for **every day**, It will be ~50 ticklabels, not 30.... that was the question – Redox May 18 '23 at 13:09

2 Answers2

0

Convert the timestamp into string and extract part of xxxx-xx-xx.

x=x.apply(lambda x:str(x)[0:10])
plt.plot(x,y)
plt.xticks(rotation=90)
plt.show()
showkey
  • 482
  • 42
  • 140
  • 295
  • You can use above or use the python inbuilt function `strftime()`, which might be easier and safer. An example is available [here](https://stackoverflow.com/questions/28694025/converting-a-datetime-column-to-a-string-column) – Redox May 22 '23 at 10:27
0

If you want every calendar date to be shown, you can add these lines at the bottom. This will show each and every calendar date (50 entries)...

import matplotlib.dates as mdates
from matplotlib import dates
plt.gca().xaxis.set_major_locator(dates.DayLocator(interval=1))   #to get a tick every day
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))     #optional formatting 
plt.show()

enter image description here

However, if you want to ONLY see the dates for which you have data in x, you need to add this line before plt.show() along with the code above. This will show the 30 entries....

plt.gca().set_xticks(x)

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26
  • Maybe it is better to convert the data type into string ,then show on x axis. – showkey May 18 '23 at 13:59
  • You can... but they would be equally spaced... here, the spaces between date will be set as per calendar dates – Redox May 18 '23 at 14:02