I have a 3D plot which traces prices over a number of days. it works fine as a Scatter3d.
I’m trying to introduce a sphere, to be drawn around one of the points. I’m using the first part of Empet’s code as a reference
https://community.plotly.com/t/adding-wireframe-around-a-sphere/37661
The problem I’m facing is having dates in one of the axes. When I uncomment the createSphere statement, a sphere is drawn but at y=0, i.e. some time in the year 1970.
It’s also changing the axis labeling.
What can I do to manipulate a date/datetime in the y-axis? I’ve tried converting the date into a .timestamp() but that doesn’t seem to work.
Code is below, with my thanks in advance for any suggestions.
import plotly.graph_objects as go
import datetime
prices=[26269, 26281, 27110, 27572, 27272, 26989, 25128, 25605]
base = datetime.datetime(2022, 2, 1)
dates = [base + datetime.timedelta(days=x) for x in range(8)]
counts = [x+1 for x in range(8)]
fig = go.Figure()
fig.add_scatter3d(x=counts, y=dates, z=prices,
marker=dict(size=2))
def createSphere(fig, prices, dates, counts):
from numpy import pi, sin, cos
import numpy as np
theta = np.linspace(0, 2*pi, 120)
phi = np.linspace(0, pi, 60)
u , v = np.meshgrid(theta, phi)
xs = 3*cos(u)*sin(v) + counts[5]
ys = sin(u)*sin(v) #??? how to apply a date/datetime value here?
zs = 1000*cos(v) + prices[5]
fig.add_surface(x=xs, y=ys, z=zs,
colorscale='Greens',
showscale=False, opacity=0.5) # or opacity=1
#createSphere(fig, prices, dates, counts)
fig.show()