Search code examples
pythonplotlyplotly-python

Is it possible to load data into a plotly python graph?


Now the graph shows 3 values: X, Y and color(data_a). Is it possible to load data to such a chart (without drawing a chart based on new data)?

data_a = np.random.rand(100, 1200)
fig = px.imshow(xg, aspect="auto",color_continuous_scale='ice')
fig.show()

data_b = np.random.rand(100, 1200)
data_c = np.random.rand(100, 1200)

The task, when hovering over the graph, showed 5 values. Is this possible in plotly?

An example of a picture that I want to receive enter image description here:

enter image description here


Solution

    • the 3 additional arrays can be compressed into 2D array of strings
    • this can then be assigned to the text property
    • finally update hovertemplate to display
    
    xg = np.random.rand(100, 1200)
    
    data_a = np.random.rand(100, 1200)
    data_b = np.random.rand(100, 1200)
    data_c = np.random.rand(100, 1200)
    
    fig = px.imshow(xg, aspect="auto",color_continuous_scale='ice', )
    
    fig = fig.update_traces(text=np.apply_along_axis(lambda a: ",".join(a.astype(str)), 0, [data_a,data_b,data_c]),
                           hovertemplate='x: %{x}<br>y: %{y}<br>color: %{z}<extra>%{text}</extra>')