This question is more of a conceptual question than a coding question.
I’m trying to draw a “tunnel” around the price of a stock (Apple in this example). The upper boundary on any particular day is the highest price achieved in the n days before (20 in the example). The lower boundary is the lowest price. I begin by adding columns to the pandas dataframe for the Highest High (HH) and Lowest Low (LL).
I did manage to code this using scatter3d, but the chart looks awfully “busy”. Comment out line 8 and and the issue becomes even more pronounced.
I also had another go using Surface, with four “walls” around the price. But still, a lot of traces in the chart!
So my question is: is there a more elegant way of visualizing this data? Plotly’s mesh3d perhaps? What would a data scientist have used to?
import plotly.graph_objects as go import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv') df["HH"] = df["AAPL.High"].rolling(20).max() #the highest price in the 20 days before each trading day df["LL"] = df["AAPL.Low"].rolling(20).min() #the lowest price df = df.tail(50) #comment out this line and the issue becomes more evident fig = go.Figure() #close price as a lines scatter in the middle of the chart fig.add_scatter3d(x = df.index*0 + len(df)/2, #just so the price centers in mid-chart y = df["Date"], z = df["AAPL.Close"], mode = "lines", line = dict(width = 4), ) #for each date, drawing a rectangle around the price showing (HH) and lower boundaries for i in range(0, len(df)): xs = [ 0, len(df), len(df), 0, 0 ] ys = [ df.iloc[i]['Date'], df.iloc[i]['Date'], df.iloc[i]['Date'], df.iloc[i]['Date'], df.iloc[i]['Date'] ] zs = [ df.iloc[i]['LL'], df.iloc[i]['LL'], df.iloc[i]['HH'], df.iloc[i]['HH'], df.iloc[i]['LL'] ] fig.add_scatter3d(x=xs, y=ys, z=zs, mode = "lines", line=dict(color = "red")) fig.show()
Thanks in advance