Plotly express > All arguments should have the same length

I have a dataframe composed by 3 columns, and my index is larger than 3.
And It seem to be problematic … ?

Please find the python code and the screenshot of the error :

        fig = go.Figure()
        fig = px.line(last_df, y=var_plotting, x =last_df.index)
        fig.update_layout(
                title= str(var_plotting[0]) +' | 🔄 Last signature : ',
                yaxis=dict(title='Value'),
                height=550,
                width=1100,
                xaxis_tickangle=45)

Hi @notafk the problem here is that the var_plotting object is of length 2 (is it a numpy array? or a list? or a dataframe) while last_df.index is of length 11. However, the x and y arguments need to have the same length in px.line.

1 Like

var_plotting contain a list of string > names of my dataframe columns. How can I plot more than one column please? The difficulty is that this list of columns has not the same length each time !

This error message needs to be changed, but it can occur when some of the items of var_plotting aren’t column names in the data frame… It’s OK for the lengths to be different so long as every item in var_plotting is in fact the name of the column in last_df

1 Like

I found this solution but I’am limited in the number of column to plot. My string list ‘var_plotting’ contain the good names of my dataframe. If I can’t with px.line(), can you tell me the best pratice to loop with .add_trace() method ?

        if  var_plotting :
                
            fig = go.Figure()
                
            if len(var_plotting) >=0 :
    
                fig.add_trace(go.Scatter(
                    x=last_df.index,
                    y=last_df[var_plotting[0]],
                    name=str(var_plotting[0]),
                    marker_color='red',
                    text = str(var_plotting[0]),
                ))

                if len(var_plotting) > 1 :
            
                    fig.add_trace(go.Scatter(
                        x=last_df.index,
                        y=last_df[var_plotting[1]],
                        name=str(var_plotting[1]),
                        marker_color='lightsalmon',
                        text = str(var_plotting[1]),
                    ))
                
                    if len(var_plotting) > 2 :
            
                        fig.add_trace(go.Scatter(
                            
                            x=last_df.index,
                            y=last_df[var_plotting[2]],
                            name=str(var_plotting[2]),
                            marker_color='MediumPurple',
                            text = str(var_plotting[2]),
                        ))
                
                    else :
                        pass

if var_plotting does contain the list of columns, are you sure you’re using version 4.8 or later of plotly?

1 Like

@nicolaskruchten

plotly==4.6.0
plotly-express==0.4.1

Ah that explains it: if you upgrade to 4.8 you should see your original code work :slight_smile:

Here’s the 4.8 announcement that explains the new features, which are basically what you’re trying to use 📣 Announcing Plotly.py 4.8: Plotly Express Support for Wide- and Mixed-Form Data, plus a Pandas backend

1 Like

Hello, I’m currently using plotly 4.9 and I’m facing the same issue with the lenght mismatch error.

However, on my side this error only occurs when column names are floats.

Indeed, after reading the @nicolaskruchten commentary about some of the items of ‘var_plotting’ not in the data frame column names, I tried both to change my column names with integers or string (simply using [str(i) for i in mydf.columns] when it was full of floats) and it worked! (see code below)

mydf = pd.DataFrame(np.zeros((10,3)))
mydf.columns = [1,2,3] #works
mydf.columns = [0.5,1.5,2.5] #doesn't work
mydf.columns = [round(i,1) for i in [0.5,1.5,2.5]] #doesn't work
mydf.columns = [str(i) for i in [0.5,1.5,2.5]] #works
fig = px.scatter(mydf, x=mydf.index, y=mydf.columns)
fig.show()

As it only failed for floats I guessed it was because of accuracy mismatches.

Would you have another solution than passing the equivalent string for floats?

Thanks!

@JeremieRabiller indeed, wide-form detection fails at the moment when column names are floats… in this case you’ll have to melt() the dataframe yourself first and feed it in in long-form.

1 Like

@nicolaskruchten Does this error still exist in the latest version of plotly for python as of Feb 2024? I am getting this error trying to reference a 2-level column where the top level column name is a string, and the 2nd level is a float:

fig1 = px.line(vix_basis, x=vix_basis.index, y=(‘VIX_Basis’, 1.0), title=‘VIX basis’)
fig.show()

ValueError: All arguments should have the same length. The length of argument y is 2, whereas the length of previously-processed arguments [‘Trade Date’] is 5007