简体   繁体   English

我怎样才能在另一条线 plot 的顶部 plot 特定点?

[英]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.我是一名交易员,我想跟踪基础证券价格走势的所有 position 切入点。 For instance here is the code I have for my BTC position so far.例如,到目前为止,这是我的 BTC position 的代码。 I import my libraries, load my data and I plot the range of the last few months of closing price data.我导入我的库,加载我的数据,然后我 plot 最近几个月收盘价数据的范围。 Everything is just how I want it, but I can't figure out how to plot my position entries.一切都是我想要的,但我不知道如何 plot 我的 position 条目。

Here is the code and output:这是代码和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 Output plot:原始数据 plot 我想要 Z32FA6E1B78A9D4028953E 53E 上的 5 个条目原始数据图

I've tried incorporating 2 different dictionaries:我试过合并2个不同的字典:

Dictionary 1 with key:value pairs of date-of-entry:amount-of-money-spent带有键的字典 1:输入日期的值对:花费的金额

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字典 2 的键:输入日期的值对:底层证券的价格

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.我面临的问题是我在 2 月 17 日购买了价值 250 美元的商品,但我希望该点以合适的入场价格出现,即 51,280 美元,因此 y 轴超出比例。 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)我在想我可以 plot 点并根据购买的数量改变它们的大小或颜色渐变(较小的购买 = 小/亮点,较大的购买 = 大/暗点)

I tried creating a DataFrame because I thought this would make it easier but I have just confused myself even further.我尝试创建一个 DataFrame 因为我认为这会更容易,但我只是让自己更加困惑。

I have gotten this far and have absolutely no idea how to go about it.我已经走到了这一步,完全不知道如何 go 关于它。 I thought I was on the right track utilizing Dict1 and Dict2 but I feel more confused now.我以为我在使用 Dict1 和 Dict2 时走在正确的轨道上,但现在我感到更加困惑。 Any help at all on how to go about this would be greatly appreciated.任何有关如何 go 的帮助将不胜感激。

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 ).遍历Dict1.items()以从MyBTC dataframe 获取dateClose价(您也可以使用annotate添加您从Dict1购买的价格)。 Then, apply the scatter function to plot specific points on top of the already existing line plot.然后,将scatter function 应用到 plot 特定点上已经存在的线 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.此 function 将标记放置在 x 轴上由date给出的位置,y 轴上放置收盘价(来自closepMyBTC ),并且像往常一样对于某些 matplotlib,您可以设置标记形状、大小( linewidths )和颜色。

...
...
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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