簡體   English   中英

Plotly:我無法使用趨勢線

[英]Plotly: I cannot use Trendline

fig = px.scatter(companies_dates["AAPL"], x="date", y=["SMA_fast", "SMA_slow"], trendline="ols")
fig.update_traces(mode = "lines")
fig.show()

錯誤消息是:

ValueError: Could not convert value of 'x' ('date') into a numeric type.
If 'x' contains stringified dates, please convert to a datetime column.

我的df的例子:

date    open    close   SMA_fast    SMA_slow    RSI

0 2021-12-31 178.089996 177.570007 162.098 143.954 52.8347

在此處輸入圖像描述

謝謝!

已嘗試使用plotly示例數據集並使其與您的代碼兼容。 date as datetime適用於線條和趨勢線。 隨機順序不會影響您在圖像中提供的效果。

import plotly.express as px
import pandas as pd
import numpy as np

df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv"
).rename(columns={"AAPL_x": "date", "AAPL_y": "SMA_fast"})
df["date"] = pd.to_datetime(df["date"])
df["SMA_slow"] = df["SMA_fast"] * np.random.uniform(0.85, 0.9, len(df))
# partially randomize order,  just messes up line not trandline
# df = df.iloc[np.random.permutation(25).tolist() + df.index.tolist()[25:]]
companies_dates = {"AAPL": df}

fig = px.scatter(
    companies_dates["AAPL"], x="date", y=["SMA_fast", "SMA_slow"], trendline="ols"
)
fig.update_traces(mode="lines")
fig.show()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM