1

ax = fig.gca()

pplt.grid(True)

pplt.plot(df['Date'], pivot, label="Pivot")

plt.show()

I'm drawing a graph with their code. df['Date'] returns 160 rows of data. the number from the pivot is still equal to

I want to add one more data to this.

plt.plot(df['Date'], ema, label="EMA", linestyle='dashed')

but the problem is that 160 rows are coming from df['Date'] but 100 rows of data is coming from ema and naturally it gives an error and does not generate the graph. The first 60 lines of ema are nan . what I want to do is pass the first 60 (or how many are empty) blanks and add them to the chart.

How can I do that?

Thanks

2 Answers 2

1

I would add nan values to the front of your ema list based on the difference in length of the ema list and the dataframe df. Then you can just plot both the pivot and the ema plots on the same graph:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

### Create mock data
df = pd.DataFrame({"Date":np.arange(0,16)})
ema = [10, 11, 12, 13, 14, 15]
pivot = np.arange(0, 16)[::-1]
##############

# fill the ema lsit with nan values based on the length of df
ema = [np.nan] * (len(df) - len(ema)) + ema 

plt.plot(df["Date"], ema, label = "ema")
plt.plot(df["Date"], pivot, label = "pivot")
plt.legend()
plt.show()

Output:

both

If you just want the ema list plotted but want the range of x axis to be the same as the plot for the pivot table, then pass the xlim argument using the min value and max value of df["Date"]:

plt.plot(df["Date"], ema, label = "ema")
plt.xlim(df["Date"].min(), df["Date"].max())
plt.legend()
plt.show()

Output:

single

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

4 Comments

For some reason, I couldn't adapt it to my own codes. You can see all the codes below. ema=[] ema.append(ta.EMA(df['Close'], timeperiod = 7)) print(ema) When I run the code, I get the following result. [0 NaN 1 NaN 2 NaN 3 NaN ... 16 10.099867 17 10.252569 18 10.634427 dtype: float64] I couldn't edit it as you mentioned.
The problem is we are working with limited information. If you could update your question with minimal working information, we could better answer this. As it stands, I don't know what ta is or its column EMA. You need to provide us with an example of all your code so we can replicate it. See my question where I provide all needed examples to help others answer my question.
I fixed it after some work on it. Thank you very much for your help.
Great! No problem
1

If I understand your question correctly I would create an array with the correct dimension and fill it with the right values. Soimething like

ema_arr = np.zeros_like(df['Data'])
ema_arr.fill(np.nan)
ema_arr[-ema.size:] = ema
plt.plot(df['Date'], ema_arr, label="EMA", linestyle='dashed')

The first line creates an array the same length of df['Data'] and fills it with 0s The second line sets the last values of said array to the values of ema. Finally the third line plots it.

3 Comments

I think you got it right. ema_arr[-ema.size:] = ema AttributeError: 'list' object has no attribute 'size' gave the error. in my codes ema goes like this: ema=[] ema.append(ta.EMA(df['Close'], timeperiod = 100)) When I run print(ema) [101 rows x 7 columns] [0 NaN 1 NaN 2 NaN 3 NaN 4 NaN ... 96 9.263071 97 9.283982 98 9.316198 99 9.361918 100 9.441200 Length: 101, dtype: float64]
If ema is a list then you can use len(ema) instead of ema.size
When I add ema_arr[len(ema)] = ema, ema_arr[len(ema)] = ema ValueError: Could not convert object to NumPy datetime error.

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.