When should we use xsrc vs x in traces?

Hi, I typically create my go.Figure / go.Bar / go.Scatter etc using x, y, and other params.

I have a case where i was to be able to create a single “go.Figure” and then keep switching the data that is bound to it.
I have seen documentation about xsrc and ysrc - but I could not find any good examples on how to use them

Could you help me understand when should xsrc/ysrc be used vs x/y ?

Ideally I am trying to create a single “template” and switch the data by switching the pd.DataFrames that is bound to that Figure

Approaches I have tried: I can create a python function to take a df and generate a new figure every time. But I would prefer avoiding this as it gets complicated (and my chart is pretty complex)

I understand your goal of creating a single “template” figure and switching the data bound to it. Using xsrc and ysrc can be very helpful in this scenario. Here’s a brief explanation of when to use xsrc/ysrc versus x/y:

Using x and y:

  • Direct Assignment: When you want to directly assign specific data to the x and y parameters of your plotly graph objects (e.g., go.Bar(x=[1, 2, 3], y=[4, 5, 6])).
  • Static Data: Ideal for static plots where the data doesn’t change frequently.

Using xsrc and ysrc:

Example:

Here’s an example of how you can use xsrc and ysrc to bind data from a DataFrame to a go.Figure:

import plotly.graph_objects as go
import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
    'x_data': [1, 2, 3],
    'y_data': [4, 5, 6]
})

# Create a figure with xsrc and ysrc
fig = go.Figure(
    data=go.Scatter(
        xsrc=df['x_data'],
        ysrc=df['y_data'],
        mode='markers'
    )
)

# Update the figure with new data
new_df = pd.DataFrame({
    'x_data': [10, 20, 30],
    'y_data': [40, 50, 60]
})

fig.update_traces(
    xsrc=new_df['x_data'],
    ysrc=new_df['y_data']
)

fig.show()

In this example, the xsrc and ysrc parameters are used to bind the x and y axes to columns in the DataFrame for Interactive Plotting - PyTutorial](Mastering Plotly go.Figure() for Interactive Plotting). When you want to update the data, you can simply update the DataFrame and use update_traces to refresh the plot without recreating it.

Does this help clarify things for you?

Regards,
Sarah
FloridaBlue

Hi Sarah, thanks for the reply. When I try your example code out with plotly 6.0.0 I get the error:

    Invalid value of type 'pandas.core.series.Series' received for the 'xsrc' property of scatter
        Received value: 0    1
1    2
2    3
Name: x_data, dtype: int64

    The 'xsrc' property must be specified as a string or
    as a plotly.grid_objs.Column object