Consider this example, done in Python 3 via Jupyter notebook - where’s I’d like to use a slider, to drag a text marker along the x axis:
import plotly.graph_objs as go
import numpy as np
from ipywidgets import widgets
from IPython.display import display
XMAX = 10
xs = np.linspace(0, XMAX, 100)
inSlider = widgets.FloatSlider(
value=1.0,
min=0.0,
max=XMAX,
step=0.01,
description='In:',
continuous_update=True
)
fig = go.FigureWidget( layout=go.Layout() )
def TransferFunction(inval):
return inval**2
# transfer function plot
fig.add_trace(go.Scatter(x=xs, y=TransferFunction(xs),
mode='lines',
name='Transfer'))
fig.data[0].line.color="blue"
# input marker
fig.add_trace(go.Scatter(x=np.asarray([0,0]), y=np.asarray([0,0]),
mode='lines+markers+text',
name='Input',
cliponaxis=False,
line={'width': 1.0, 'dash': 'dash'},
marker={
"symbol": "triangle-up",
'color': 'green',
'size': 10, "opacity": 0.6,
"line": {'width': 0.5},
},
text=["", ""],
textposition="bottom center",
textfont=dict(
color="green"
),
))
fig.update_xaxes(range=[0, XMAX])
fig.update_yaxes(range=[0, TransferFunction(XMAX)])
fig.update_layout(margin=go.layout.Margin(l=20,t=10,b=10,pad=4))
def response(change):
with fig.batch_update():
tmpo = TransferFunction(inSlider.value)
fig.data[1].x=np.asarray([inSlider.value, inSlider.value])
fig.data[1].y=np.asarray([0, tmpo])
fig.data[1].text = ["{:.2f}".format(inSlider.value), ""]
inSlider.observe(response, names="value")
myhbox = widgets.HBox([inSlider])
widgets.VBox([fig,
myhbox])
The output looks like this:
So, basically, the code works as intended - except the marker label overlaps with the tick texts on the X axis, which makes it a bit difficult to read.
I really like the specification textposition="bottom center"
- however, I would just want to also specify: “place the label at ‘bottom center’, and push it some 15 pixels down from that location”. Is there anything I can use to do this?!