I would like to be able to retrieve the size (height, width) of a rectangle (or other shapes) I have drawn onto a figure with the drawrect
modebar tool.
My example is the following
import plotly.graph_objects as go
import numpy as np
N = 100
M = 50
xx = np.arange(float(N))
yy = np.arange(float(M))
x, y = np.meshgrid(xx, yy)
z = np.random.random([M, N])
h = go.Heatmap(x=xx, y=yy, z=z)
fig = go.FigureWidget(data=h)
fig.update_layout(
autosize=False,
width=700, height=500)
fig.update_layout(modebar_remove="editinchartstudio")
fig.update_layout(modebar_add=['drawline',
'drawopenpath',
'drawclosedpath',
'drawcircle',
'drawrect',
'eraseshape'
])
fig
After that, I draw a rectangle and a circle with the tools to obtain
If I print the fig.layout
, I can see in the string repr of the layout that it has two shapes, a rectangle and a circle
fig.layout
Layout({
'autosize': False,
'dragmode': 'drawcircle',
'height': 500,
'modebar': {'add': [drawline, drawopenpath, drawclosedpath, drawcircle,
drawrect, eraseshape],
'remove': 'editinchartstudio'},
'shapes': [{'editable': True,
'fillcolor': 'rgba(0,0,0,0)',
'fillrule': 'evenodd',
'layer': 'above',
'line': {'color': '#444', 'dash': 'solid', 'width': 4},
'opacity': 1,
'type': 'rect',
'x0': 14.216981132075471,
'x1': 45.160377358490564,
'xref': 'x',
'y0': 39.8125,
'y1': 24.8125,
'yref': 'y'},
{'editable': True,
'fillcolor': 'rgba(0,0,0,0)',
'fillrule': 'evenodd',
'layer': 'above',
'line': {'color': '#444', 'dash': 'solid', 'width': 4},
'opacity': 1,
'type': 'circle',
'x0': 83.81740584790728,
'x1': 58.20146207662103,
'xref': 'x',
'y0': 16.609769580772138,
'y1': 32.07773041922786,
'yref': 'y'}],
'template': '...',
'width': 700
})
However, I would like to access their properties, but the tuple fig.layout.shapes
is just empty.
Maybe there is something that doesn’t get synchronized between the python and the javascript?
Thanks for any help!