Sabrin
Sabrin

Reputation: 61

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.

Upvotes: 3

Views: 4631

Answers (1)

Derek O
Derek O

Reputation: 19590

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.

Upvotes: 5

Related Questions