plot all columns from dataframe without having to define them in plotly

would like to plot in PLOTLY all columns from dataframe without having to define them.

The required is the same functionality in Plotly as here in matplotlib.

import glob
import pandas as pd

df = pd.DataFrame({
    'A': ['15','21','30'],
    'M': ['12','24','31'],
    'I': ['28','32','10']})


%matplotlib inline
from matplotlib import pyplot as plt

df=df.astype(float)
df.plot()

Here is my code for plotly, but as i said, i have no idea how to plot all the columns automatically. The once i have noticed is also, that in plotly the X-axis needs to be defined, but with this restriction i can live.

import plotly.express as px 
import pandas as pd 
import numpy as np 
import os

# data

df = pd.DataFrame({
    'ID': ['1','2','3'],
    'A': ['15','21','30'],
    'M': ['12','24','31'],
    'I': ['28','32','10']})

df_long=pd.melt(df , id_vars=['ID'], value_vars=['A', 'M' , 'I'])

fig = px.line(df_long, x='ID', y='value', color='variable')
fig.show()

Can someone please help me out to define, how to plot in plotly all the columns automatically?

You donโ€™t have to list your value_vars in the melt() call, itโ€™s optional if you want all the columns.

Also: the next version of Plotly Express will support wide-form data like this natively! Stayed tuned for an announcement next week :slight_smile:

With version 4.8 of Plotly.py, you can now plot this kind of data directly from PX:

import plotly.express as px 
import pandas as pd 

df = pd.DataFrame({
    'ID': ['1','2','3'],
    'A': ['15','21','30'],
    'M': ['12','24','31'],
    'I': ['28','32','10']})


fig = px.line(df, x='ID', y=df.columns)
fig.show()

Thank you for the update.
It works great!

1 Like

the code does not work anymore.
ValueError: All arguments should have the same length. The length of argument y is 4, whereas the length of previous arguments [โ€˜IDโ€™] is 3

Is there way how to fix it?

Could you please check also my another post: Plotly express plot differently as before

It looks to me like the functionality of Plotly Express is identical to the previous version of Plotly express (before 4.8.)

Are you sure youโ€™re using 4.8? This definitely works in 4.8 but in 4.7 or below you will see the โ€œall arguments should be the same lengthโ€ error.

The other possibility is that youโ€™re passing something into y which is not a valid column name.