Skip to content

[BUG] numpy array not correctly read back from figure data #3307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stdkoehler opened this issue May 26, 2025 · 3 comments
Open

[BUG] numpy array not correctly read back from figure data #3307

stdkoehler opened this issue May 26, 2025 · 3 comments
Assignees
Labels
bug something broken P2 considered for next cycle

Comments

@stdkoehler
Copy link

Please provide us your environment, so we can easily reproduce the issue:

Windows 10, python 3.12

dash                 3.0.4
dash-core-components 2.0.0
dash-html-components 2.0.0
plotly               6.1.1
numpy                2.2.5

Describe the bug

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’}}

As also described in https://community.plotly.com/t/changed-behaviour-in-dash-2-18-2-using-numpy-1-26-4-when-reading-figure-into-callback-on-linux/90614

import dash
from dash import dcc, html, Input, Output, State
import plotly.graph_objs as go
import numpy as np

# Initialize the Dash app
app = dash.Dash(__name__)

# Create some sample data using numpy
x_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 app
app.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')
)
def update_figure(n_clicks):
    # Create a ScatterGL trace
    trace = go.Scattergl(
        x=x_data,
        y=y_data,
        mode='markers',
        marker=dict(size=10, color='blue')
    )
    
    # Create the figure
    fig = go.Figure(data=[trace])
    fig.update_layout(title='ScatterGL Plot', xaxis_title='X Axis', yaxis_title='Y Axis')
    
    return fig

# 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')
)
def print_data(n_clicks, figure):
    if n_clicks > 0 and figure is not None:
        # Extract the x and y data from the figure
        x_values = figure['data'][0]['x']
        y_values = figure['data'][0]['y']
        
        # Print the x and y values
        print("X values:", x_values)
        print("Y values:", y_values)
    
    return "Print Data"

# Run the app
if __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

@gvwilson gvwilson added bug something broken P2 considered for next cycle labels May 28, 2025
@gvwilson
Copy link
Contributor

@T4rk1n is this an issue with Dash or is this lower down the stack?

@T4rk1n
Copy link
Contributor

T4rk1n commented 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
Copy link
Contributor

gvwilson commented 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.

@gvwilson gvwilson self-assigned this Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests

3 participants