简体   繁体   中英

How can I plot specific points on top of another line plot?

I'm a trader and I want to keep track of all my position entry points along the underlying securitie's price movement. For instance here is the code I have for my BTC position so far. I import my libraries, load my data and I plot the range of the last few months of closing price data. Everything is just how I want it, but I can't figure out how to plot my position entries.

Here is the code and output:

# Libraries
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import yfinance as yf


# Data load
BTC = yf.download(tickers='BTC-USD', start='2020-10-01', end='2021-05-03')
BTC_close = BTC['Close']
print(BTC.head())
BTC_positions = yf.download(tickers='BTC-USD', start='2021-02-01', end='2021-05-03')
MyBTC = BTC_positions['Close']
df_RAW = pd.DataFrame(BTC_positions, columns=['Date','Close'])



# Plot
fig = plt.figure()
f, ax = plt.subplots(1,1)
ax.tick_params(length=3, direction='in', labelsize=10)
fmt = '${x:,.0f}'
tick = mtick.StrMethodFormatter(fmt)
ax.yaxis.set_major_formatter(tick)
ax.xaxis.set_tick_params(direction='in')
MyBTC.plot(figsize=(12, 7))
plt.xticks(rotation=0)
plt.title("Bitcoin Position Entries", fontsize=20)
plt.xlabel('Day',fontsize=13) # or fontsize=10
plt.ylabel('Price $$$',fontsize=13) # or fontsize=10
plt.show()

Output plot: Raw data plot that I want to plot my entries on 原始数据图

I've tried incorporating 2 different dictionaries:

Dictionary 1 with key:value pairs of date-of-entry:amount-of-money-spent

Dict1 = {
    '2021-02-17': 250,
    '2021-02-24': 250,
    '2021-02-28': 50,
    '2021-03-08': 500,
    '2021-04-10': 500,
    '2021-04-14': 120,
    '2021-04-17': 400,
    '2021-04-21': 120,
    '2021-04-22': 875,
    '2021-04-23': 300,
    '2021-04-29': 125
}

Dictionary 2 with key:value pairs of date-of-entry:price-of-underlying-security

Dict2 = {
    'Date': ['2021-02-17','2021-02-24','2021-02-28','2021-03-08','2021-04-10','2021-04-14','2021-04-17','2021-04-21','2021-04-22','2021-04-23','2021-04-29'],
    'Price': [51280, 49426, 44548, 50840, 60251, 62479, 53637, 55946, 51376, 48080, 53955]
}

The problem I'm facing is I purchased $250 worth on Feb.17 but I want that dot to show up at the appropriate price of entry, which was $51,280, so the y-axis is out of scale. I was thinking I could plot the dots and just vary their size or color gradient based on amount purchased (smaller purchase = small/light dot, bigger purchase = big/dark dot)

I tried creating a DataFrame because I thought this would make it easier but I have just confused myself even further.

I have gotten this far and have absolutely no idea how to go about it. I thought I was on the right track utilizing Dict1 and Dict2 but I feel more confused now. Any help at all on how to go about this would be greatly appreciated.

Iterate over the Dict1.items() to get the date and the Close price from the MyBTC dataframe (you can also use annotate to add the price you bought from Dict1 ). Then, apply the scatter function to plot specific points on top of the already existing line plot. This function place the markers at the location given by date in the x-axis and the close price ( closep from MyBTC ) in the y-axis, and, as usual for some matplotlib, you can set the marker shape, size ( linewidths ) and color.

...
...
plt.xlabel('Day',fontsize=13) # or fontsize=10
plt.ylabel('Price $$$',fontsize=13) # or fontsize=10

for date,buy_price  in Dict1.items():
    closep = MyBTC[date]
    plt.annotate(str(buy_price), (date, closep), xytext=(-3, 6),
            textcoords='offset points',  weight='bold', fontsize=12)
    plt.scatter(date, closep, marker='v', linewidths=1, color='r')

plt.show()

plot_with_markers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM