阴谋地,策划烛台<每天,有人试图

2024-09-30 06:24:36 发布

您现在位置:Python中文网/ 问答频道 /正文

有人试图使用plotly在python上绘制蜡烛吗?我在尝试,但你基本上得到了所有的烛台上的时间绘制在彼此的基础上的日期。是否有一种方法来绘制输入日期和时间的小时数,以停止任何重叠?代码我从下面开始

fivedaydata.tail()

import plotly.graph_objects as go

#create 1 week graph 5 days = 120hrs 

fig = go.Figure(data=[go.Candlestick(x=fivedaydata.Time,
                open=fivedaydata['Open'],
                high=fivedaydata['High'],
                low=fivedaydata['Low'],
                close=fivedaydata['Close'])])

fig.show()

enter image description here


Tags: 方法代码importgo时间fig绘制plotly
1条回答
网友
1楼 · 发布于 2024-09-30 06:24:36
  • 我们收集了一些OHLC样本数据,然后将其重新模拟为每小时一次。在这个模拟中,我认为只需要交易时间
  • 如果你用你的代码绘制了这张图,它就可以工作了。没有交易的时间间隔和没有交易的天数(周末)
  • 已再次绘制,但使用数据帧索引作为xaxis。使用时间列中的值更新轴
  • 在这两种情况下,蜡烛都不会相互重叠。我只能假设您的Time列是一个字符串,在交易日内重复
import plotly.graph_objects as go

# get some data
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")

# remap to 5 days of hourly data... hence only 8 hrs a day
fivedaydata = (
    df.head(8 * 5)
    .assign(Time=np.array([pd.date_range(d + pd.Timedelta(hours=9), freq="H", periods=8)
                           for d in pd.bdate_range(df.loc[0, "Date"], periods=5, freq="B")
                          ]).flatten()
    )
    .rename(columns={c: c.split(".")[1] for c in df.columns if "AAPL" in c})
)

go.Figure(
    data=[
        go.Candlestick(
            x=fivedaydata.Time,
            open=fivedaydata["Open"],
            high=fivedaydata["High"],
            low=fivedaydata["Low"],
            close=fivedaydata["Close"],
        )
    ]
).show()

go.Figure(
    data=[
        go.Candlestick(
            x=fivedaydata.index,
            open=fivedaydata["Open"],
            high=fivedaydata["High"],
            low=fivedaydata["Low"],
            close=fivedaydata["Close"],
        )
    ]).update_layout(xaxis = {"tickmode":'array',"tickvals" : fivedaydata.index,
                              "ticktext" : fivedaydata["Time"].dt.strftime("%H%p %d %b")})


enter image description hereenter image description here

相关问题 更多 >

    热门问题