亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用plotly繪制樞軸點?

如何使用plotly繪制樞軸點?

冉冉說 2023-10-31 21:28:51
我正在使用plotly 繪制一些帶有SMA 和MACD 數據的圖表。這很好用。fig = make_subplots(vertical_spacing = 0, rows=3, cols=1, row_heights=[0.6, 0.2, 0.2])fig.add_trace(go.Ohlc(x=data['timestamp'],            open=data['open'],            high=data['high'],            low=data['low'],            close=data['close']))fig.add_trace(go.Scatter(x=data['timestamp'], y=data['sma'], line=dict(color='orange', width=1)), row=1, col=1)fig.add_trace(go.Scatter(x=data['timestamp'], y = data['macd']), row=2, col=1)fig.add_trace(go.Scatter(x=data['timestamp'], y = data['macds']*1.1), row=2, col=1)fig.add_trace(go.Bar(x=data['timestamp'], y = data['volume']), row=3, col=1)fig.update_layout(xaxis_rangeslider_visible=False,                xaxis=dict(zerolinecolor='black', showticklabels=False),                xaxis2=dict(showticklabels=False))fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=False)fig.show()但現在我想添加樞軸:    last_day['Pivot'] = (last_day['High'] + last_day['Low'] + last_day['Close'])/3    last_day['R1'] = 2*last_day['Pivot'] - last_day['Low']    last_day['S1'] = 2*last_day['Pivot'] - last_day['High']    last_day['R2'] = last_day['Pivot'] + (last_day['High'] - last_day['Low'])    last_day['S2'] = last_day['Pivot'] - (last_day['High'] - last_day['Low'])    last_day['R3'] = last_day['Pivot'] + 2*(last_day['High'] - last_day['Low'])    last_day['S3'] = last_day['Pivot'] - 2*(last_day['High'] - last_day['Low'])last_day 的輸出如下所示:                    Timestamp      Open      High       Low     Close  Volume    Pivot        R1        S1        R2        S2        R3        S3499  2020-10-11T14:45:00.000Z  0.000321  0.000321  0.000319  0.000319  886.17  0.00032  0.000321  0.000319  0.000322  0.000318  0.000323  0.000316如何將此數據(樞軸、S1、S2、S3...)添加到我的圖表中?我想要與這里的圖片類似的東西:https://www.fxpivot-points.com/?page =4我試過這個:fig.add_trace(go.Scatter(x=last_day['Timestamp'], y=last_day['Pivot'], line=dict(color='purple', width=1)), row=1, col=1)但它只畫了一個點,因為我只提供了一個時間戳。我怎樣才能把它變成一條水平線?
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

您特別要求僅繪制最后一天的計算點,并在您擁有燭臺圖的整個時間段內顯示它們。如果這實際上是您想要實現的目標,那么下面的代碼片段將生成以下繪圖,其中使用 和fig.add_shapes()分別fig.add_annotations()顯示水平線和樞軸點名稱。

如果有什么地方不太對勁,請告訴我,我們可以討論進一步的調整。

陰謀:

https://img1.sycdn.imooc.com/6541013500013ead06530291.jpg

基于Apple示例數據的完整代碼:

import plotly.graph_objects as go


import pandas as pd

from datetime import datetime


df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv').tail(5)


fig = go.Figure(data=[go.Candlestick(x=df['Date'],

                open=df['AAPL.Open'],

                high=df['AAPL.High'],

                low=df['AAPL.Low'],

                close=df['AAPL.Close'])])


last_day = df.iloc[-1].to_frame().T

last_day = last_day.rename(columns = lambda x: x.replace('AAPL.', ''))



not_pivots = list(last_day.columns)

last_day['Pivot'] = (last_day['High'] + last_day['Low'] + last_day['Close'])/3

last_day['R1'] = 2*last_day['Pivot'] - last_day['Low']

last_day['S1'] = 2*last_day['Pivot'] - last_day['High']

last_day['R2'] = last_day['Pivot'] + (last_day['High'] - last_day['Low'])

last_day['S2'] = last_day['Pivot'] - (last_day['High'] - last_day['Low'])

last_day['R3'] = last_day['Pivot'] + 2*(last_day['High'] - last_day['Low'])

last_day['S3'] = last_day['Pivot'] - 2*(last_day['High'] - last_day['Low'])

last_day


pivots = [n for n in last_day.columns if n not in not_pivots]


pcols = ['green', 'blue', 'blue', 'red', 'red', 'black', 'black']


for i, col in enumerate(pivots):


    # horizontal lines

    fig.add_shape(type="line",

                    x0=df['Date'].iloc[0],

                    y0=last_day[col].iloc[-1],

                    x1=df['Date'].iloc[-1],

                    y1=last_day[col].iloc[-1],

                    line=dict(

                        color=pcols[i],

                        width=1,

                        #dash="dashdot",

                    ),)

    

    # line annotations

    fig.add_annotation(dict(font=dict(color=pcols[i],size=12),

                                        x=df['Date'].iloc[0],

                                        y=last_day[col].iloc[0],

                                        showarrow=False,

                                        text=col,

                                        textangle=0,

                                        xanchor='right',

                                        xref="x",

                                        yref="y"))


fig.show()


查看完整回答
反對 回復 2023-10-31
  • 1 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號