'Facing an error while executing the plotly module
I am running the below-mentioned code to create a candlestick chart with traces but an error is being reflected as Exception has occurred: AttributeError 'dict' object has no attribute 'add_trace'
The code is as follows
import pandas as pd
import plotly.graph_objects as go
import yfinance as yf
import plotly.offline
from plotly.offline import init_notebook_mode, iplot, iplot_mpl
a=input("Enter symbol of the company\n")
tick1=a+'.NS'
HD=yf.Ticker(tick1)
His=HD.history(period='3mo',interval='1d')
His.reset_index(inplace=True)
His['20wma']=His['Close'].rolling(window=140).mean()
fig={"data":[go.Candlestick(x=His['Date'],open=His['Open'],high=His['High'],low=His['Low'],close=His['Close'])]}
fig.add_trace(go.Scatter(x=His['Date'],y=His['20wma'],line=dict(color="e0e0e0")))
plotly.offline.plot(fig)
Solution 1:[1]
You can create figures from dictionaries or from graph objects.
In your example, you create fig
as a standard python dictionary. It only becomes a graph object once you plot it. add_trace
is not a valid python command for a dictionary, that's why it fails.
You want to create a graph object, to which you can apply add_trace
. See here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | KingOtto |