Home > Software engineering >  How to mark 2 specific data points on a price action chart using matplotlib on Python?
How to mark 2 specific data points on a price action chart using matplotlib on Python?

Time:05-29

Suppose that I have the following graph:

btcusdt price action from 2022-04-01 05:59:59.99 to 2022-05-13 21:59:59.99

Such graph was created using the following python code:

from binance.client import Client
import pandas as pd
import matplotlib.pyplot as plt

#personal API key and Secret Key from your Binance account

api_key = "your binance api key"
secret_key = "your binance secret key"

client = Client(api_key= api_key, api_secret= secret_key, tld= "com")

klines_btcusdt = client.get_historical_klines(symbol="BTCUSDT", interval="1h", start_str = "1648807200000", end_str="1653667199999")

df_btcusdt = pd.DataFrame(klines_btcusdt)

#drop unnecesary columns
df_btcusdt.drop(5, inplace = True, axis=1)
df_btcusdt.drop(7, inplace = True, axis=1)
df_btcusdt.drop(8, inplace = True, axis=1)
df_btcusdt.drop(9, inplace = True, axis=1)
df_btcusdt.drop(10, inplace = True, axis=1)
df_btcusdt.drop(11, inplace = True, axis=1)

# Rename the column names for best practices
df_btcusdt.rename(columns = { 0 : 'Start Date', 
                          1 : 'Open Price',
                          2 : 'High Price',
                          3 : 'Low Price',
                          4 :'Close Price',
                          6 :'End Date',
                          }, inplace = True)

# Convert Unix Time values to actual dates
df_btcusdt['Start Date'] = pd.to_datetime(df_btcusdt['Start Date'], unit='ms')
df_btcusdt['End Date'] = pd.to_datetime(df_btcusdt['End Date'], unit='ms')
df_btcusdt = df_btcusdt[['End Date','Close Price']]
df_btcusdt = df_btcusdt.set_index('End Date', inplace=False)
df_btcusdt = df_btcusdt.astype({'Close Price': 'float'})

#visualising the price
plt.figure(figsize=(8, 6), dpi=80)
plt.title('BTCUSDT Price')
plt.rc('xtick', labelsize = 8)
plt.plot(df_btcusdt.index[0:], df_btcusdt[0:])

And I'm interested in marking 2 specific data points which are: df_btcusdt[0:1] and df_btcusdt[1024:1025], I mean:

                         Close Price
End Date                            
2022-04-01 05:59:59.999     44646.16

                         Close Price
End Date                            
2022-05-13 21:59:59.999     30046.65

But I don't know how to do so, I tried changing the last line of my code for the following one:

plt.plot(df_btcusdt.index[0:], df_btcusdt[0:], markevery = [44646.16, 30046.65], marker="ro")

But got:

ValueError: markevery=[44646.16, 30046.65] is iterable but not a valid numpy fancy index

It should throw something like this:

desired graph

May I get some help please?

CodePudding user response:

I don't know where raw_data came from in your code, so let's just call the dataframe df. You have a datetime index, so the trick is to look up the dates corresponding to the two integer indices you want to highlight, and use those dates for the plot. So after the command for the line plot, add this:

highlight_dates = df.index[[0, 1024]]
plt.scatter(highlight_dates, df.loc[highlight_dates, 'Close Price'], 
            color='red', marker='o')
  • Related