You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] numpy array not correctly read back from figure data #3307
When I create a go.Figure and add a trace with x and/or y being numpy arrays, the plot is created correctly.
However, when I want to read back the data, some representation of the numpy array is provided as dictionary: {‘dtype’: ‘f8’, ‘bdata’: ‘AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAUQA==’, ‘_inputArray’: {‘0’: 1, ‘1’: 2, ‘2’: 3, ‘3’: 4, ‘4’: 5, ‘bdata’: ‘AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAUQA==’, ‘dtype’: ‘f8’, ‘shape’: ‘5’}}
importdashfromdashimportdcc, html, Input, Output, Stateimportplotly.graph_objsasgoimportnumpyasnp# Initialize the Dash appapp=dash.Dash(__name__)
# Create some sample data using numpyx_data=np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y_data=np.array([2.0, 1.5, 3.5, 2.0, 5.0])
# Define the layout of the appapp.layout=html.Div([
dcc.Graph(id='scatter-plot'),
html.Button('Generate Plot', id='generate-button', n_clicks=0),
html.Button('Print Data', id='print-button', n_clicks=0)
])
# Callback to generate the ScatterGL plot@app.callback(Output('scatter-plot', 'figure'),Input('generate-button', 'n_clicks'))defupdate_figure(n_clicks):
# Create a ScatterGL tracetrace=go.Scattergl(
x=x_data,
y=y_data,
mode='markers',
marker=dict(size=10, color='blue')
)
# Create the figurefig=go.Figure(data=[trace])
fig.update_layout(title='ScatterGL Plot', xaxis_title='X Axis', yaxis_title='Y Axis')
returnfig# Callback to print the x and y data from the figure@app.callback(Output('print-button', 'children'),Input('print-button', 'n_clicks'),State('scatter-plot', 'figure'))defprint_data(n_clicks, figure):
ifn_clicks>0andfigureisnotNone:
# Extract the x and y data from the figurex_values=figure['data'][0]['x']
y_values=figure['data'][0]['y']
# Print the x and y valuesprint("X values:", x_values)
print("Y values:", y_values)
return"Print Data"# Run the appif__name__=='__main__':
app.run_server(debug=True)
Expected behavior
A) Throw an Exception when numpy arrays are not supported
B) If numpy arrays are supported return the numpy array or the numpy array converted to a valid list
This is from plotly.py typed array, I think this will keep on coming back so we might have to do something. The data get serialized to those for optimized transfer and end up as an encoded string in the renderer state.
There should be a plotly.py function to transform that back into readable or we should add one.
Activity
gvwilson commentedon May 28, 2025
@T4rk1n is this an issue with Dash or is this lower down the stack?
T4rk1n commentedon May 28, 2025
This is from plotly.py typed array, I think this will keep on coming back so we might have to do something. The data get serialized to those for optimized transfer and end up as an encoded string in the renderer state.
There should be a plotly.py function to transform that back into readable or we should add one.
gvwilson commentedon Jun 3, 2025
@marthacryan is there already a function in plotly.py to do the required transformation? if not, I'll create an issue for getting it written.