3

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.

9
  • I ran your code with the APPL stock price from Yahoo Finance, and the second code also displayed correctly without error. It's the same number of lines, including the missing values. Commented Jun 13, 2021 at 8:09
  • What version of Plotly are you using? You may want to upgrade if you're not using the latest version Commented Jun 13, 2021 at 8:12
  • @r-beginners, then why I am getting this error. I have saved the price in csv sheet. Note: I have used Gold price from LBMA(London bullion market association). My first code is working fine. Having issue with second code when using y=["Price","MA12"] . Getting plot when using y="Price" with x = "Date" or y="MA12" with x = "Date" separately. Commented Jun 13, 2021 at 8:16
  • @DerekO I am using Colab Notebook for running these commands . Commented Jun 13, 2021 at 8:17
  • 1
    I got the same error when I ran it in google Colab, Colab version is 4.4.1 and I updated it and the graph showed up in 4.14.3. Let's upgrade the version. Commented Jun 13, 2021 at 8:25

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Derek, Just to add a comment - We need to restart runtime in Colab after upgrading plotly.
Oh good point! - I've edited my answer accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.