Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have referred to the previous query All arguments should have the same length plotly but still not getting answer for my question.

I have a gold price dataset.

 Date          Price
 31-01-1979    1840.8
 28-02-1979    2011.7
 30-03-1979    1940.2
 30-04-1979    2013.1 
 .             .
 .             .
 .             .
 26-02-2021    128073.3
 31-03-2021    123639
 30-04-2021    130934.3
 31-05-2021    137979.1

I have created 12 month moving average:

df['MA12'] = df['Price'].rolling(12).mean()

enter image description here

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 single 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 in single plot.

like image 899
Sabrin Avatar asked Sep 09 '26 01:09

Sabrin


1 Answers

Even though the answer was resolved in the comments, I am adding an answer so people encountering the same issue can find the solution.

Colab's version of Plotly is v4.4.1, and the px.line function in this version of Plotly does not support a multiple element list as an input to the y argument. Upgrading to the newest version of Plotly v4.14.3 and then restarting runtime in Colab resolves this issue.

like image 192
Derek O Avatar answered Sep 10 '26 15:09

Derek O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!