✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Python: ValueError: All arguments should have the same length plotly

Hello,

I have a gold price dataset.

Date Price
31-01-1979 1840.80
28-02-1979 2011.70
30-03-1979 1940.20
30-04-1979 2013.10
. .
. .
. .
26-02-2021 128073.30
31-03-2021 123639.00
30-04-2021 130934.30
31-05-2021 137979.10

I have created 12 month moving average: df[‘MA12’] = df[‘Price’].rolling(12).mean()

df.info()

image

  1. First I used below command: I got two different plots of Price and Moving Average separately.

    import plotly.express as px
    fig1 = px.line(df, x=“Date”, y=“Price”, template = ‘plotly_dark’)
    fig2 = px.line(df, x=“Date”, y=“MA12”, template = ‘plotly_dark’)
    fig1.show()
    fig2.show()

  2. Now I used below command for plotting Time Series: I want both Price and Moving Average price trend with respect to date on same plot

    import plotly.express as px
    fig = px.line(df, x=‘Date’, y=[“Price”,“MA12”], template = ‘plotly_dark’)
    fig.show()

Getting Error: ValueError: All arguments should have the same length. The length of argument y is 2, whereas the length of previous arguments [‘Date’] is 509.

My query:
a) Why I am getting this error.
b) Please help me in getting plotly command for getting both price and moving average price on single plot.

Hi @sabrin

Wellcome to our Community.

I’m not familiar with Plotly express but I think you need to use two different ‘y’ for example ‘y1=“Price”’ and ‘y2=“MA12”’.

Also you can manage two different legends and scales, one in the left and the other in the right.

See the details in the documentation about using y2. :grinning: