Skip to content

Instantly share code, notes, and snippets.

@nv-h
Last active December 1, 2020 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nv-h/bb27e55e5f0b79de33248cb57f6e7c74 to your computer and use it in GitHub Desktop.
Save nv-h/bb27e55e5f0b79de33248cb57f6e7c74 to your computer and use it in GitHub Desktop.
In Plotly, place an image of any size at any position on the X axis in datetime format.
import plotly.graph_objects as go
from datetime import datetime, timedelta
# Create figure
fig = go.Figure()
now = datetime.now()
print(now)
# Increment 1 hour
x = [(now + timedelta(hours=i)) for i in range(5)]
# Add trace
fig.add_trace(
go.Scatter(x=x, y=[1.23, 2.5, 0.0, 3, 1, 2])
)
# msec based
hour_width_on_timestamp = 1000*3600
# Add images
fig.add_layout_image(
dict(
source="https://images.plot.ly/language-icons/api-home/python-logo.png",
xref="x",
yref="y",
x=now, # Image position x is datetime
y=1,
# Sizes are selected smaller one if hold aspect ratio
sizex=hour_width_on_timestamp, # sizex is must be integer or float format
# the format is msec based
sizey=2,
opacity=0.7,
# sizing="stretch", # Comment out if want to hold aspect ratio
layer="above" # above or below
)
)
# Set templates
fig.update_layout(template="plotly_white")
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment