简体   繁体   English

在现有图形上添加一个没有 y 轴值的点(标记)

[英]Add a dot (marker) without y-axis value on the existing graph

I have a graph of my stocks portflio over some period of time, which looks like this:我有一张我的股票投资组合在一段时间内的图表,如下所示:

在此处输入图像描述

I have a price for every 5 minutes of time, so x data is timestamps and y data is just numbers.我有每 5 分钟时间的价格,所以 x 数据是时间戳,y 数据只是数字。

I also have a dataframe with times of operations, which contains a time of operation and its type (bought or sold) and looks like this:我还有一个带有操作时间的 dataframe,其中包含操作时间及其类型(买入或卖出),如下所示:

在此处输入图像描述

I want to add a dot or some kind of a marker for every operation on my graph, but i don't know how to do this, I don't have y-values for it.我想为图表上的每个操作添加一个点或某种标记,但我不知道该怎么做,我没有它的 y 值。 And timestamps x-values are different from operations x-values, so i can't just take y-values from an existing graph.时间戳 x 值不同于操作 x 值,因此我不能只从现有图形中获取 y 值。 This is how I imagine it ideally, but for starters i just want to understand how to add my points on the graph:这就是我理想中的想象,但对于初学者来说,我只想了解如何在图表上添加我的点:

在此处输入图像描述 . . I'm using plotly, but i don't care if the solution requries matplotlib or anything else.我正在使用 plotly,但我不在乎解决方案是否需要 matplotlib 或其他任何东西。

This is definitely doable in Plotly using annotations .这在 Plotly 中使用注释绝对可行。 There don't have to be y-values for the operations DataFrame because you can use the corresponding y-value from the stock data at the operations x-values.操作 DataFrame 不必有 y 值,因为您可以在操作 x 值处使用股票数据中的相应 y 值。 To plot the red markers, you can plot the operations_df and set the marker attributes as you like.对于 plot 红色标记,您可以 plot operations_df 并根据需要设置标记属性。

Then you can loop through the operations_df and place an annotation on the scatterplot based on the date of the entry, and its corresponding y-value on the stocks portfolio.然后,您可以遍历 operations_df 并根据条目日期及其在股票投资组合上的相应 y 值在散点图上放置注释。 Here is an example with some made up data, so you may need to tweak this code for your DataFrames.这是一个包含一些合成数据的示例,因此您可能需要为您的 DataFrame 调整此代码。

import numpy as np
import pandas as pd

import plotly.graph_objs as go

## create some random data
np.random.seed(42)

df = pd.DataFrame(
  data=500*np.random.randint(0,1000,24), 
  columns=['price'], 
  index=pd.date_range(start='12/1/2020', end='12/1/2020 23:00:00', freq='H')
)

operations_df = pd.DataFrame(
  data=['Buy','Sell','Buy'], 
  columns=['Operation_type'], 
  index=pd.to_datetime(['12/1/2020 08:00:00', '12/1/2020 12:00:00', '12/1/2020 16:00:00'])
)

fig = go.Figure(data=[go.Scatter(
  x=df.index,
  y=df.price
  )])

fig.add_trace(go.Scatter(
  x=operations_df.index,
  y=[df.loc[date, 'price'] for date in operations_df.index],
  mode='markers',
  marker=dict(
    size=16,
    color="red")
  ))

for date, row in operations_df.iterrows():
  # print(date, df.loc[date, 'price'], row['Operation_type'])
  fig.add_annotation(
    x=pd.to_datetime(date),
    y=df.loc[date, 'price'],
    xref="x",
    yref="y",
    font=dict(
      size=16,
      color="red"
      ),
    text=row['Operation_type'],
    bordercolor="red",
    width=80,
    height=60,
    arrowcolor="red",
    ax=0,
    ay=-150
    )

fig.update_layout(showlegend=False)

fig.show()

在此处输入图像描述

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

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