1

I have multiple time series. Each with common time stamp. I have plotting subplot for each time series. Now I want to add vertical line in each subplot. I have tried for loop. But it does not work.

url_jobs="https://assets.datacamp.com/production/repositories/1259/datasets/1c6b4a977a3c14f2a00c2d74694b208d9ac86443/ch5_employment.csv"
jobs=pd.read_csv(url_jobs)
jobs['datestamp']=pd.to_datetime(jobs['datestamp'])
jobs=jobs.set_index('datestamp')
plot2=jobs.plot(subplots=True,layout=(4,4),figsize=(20,16),sharex=True,sharey=False)
for each in plot2:
    each.axvline('2008-09-01',color='red',linestyle='--')
plt.show()

1 Answer 1

1

Since you have a 4 x 4 layout DataFrame.plot will return a 4 x 4 numpy array. You can call flatten on it in order to turn it into a 1D array:

url_jobs="https://assets.datacamp.com/production/repositories/1259/datasets/1c6b4a977a3c14f2a00c2d74694b208d9ac86443/ch5_employment.csv"
jobs=pd.read_csv(url_jobs)
jobs['datestamp']=pd.to_datetime(jobs['datestamp'])
jobs=jobs.set_index('datestamp')
plot2=jobs.plot(subplots=True,layout=(4,4),figsize=(20,16),sharex=True,sharey=False)
for each in plot2.flatten():
    each.axvline('2008-09-01',color='red',linestyle='--')
plt.show()

enter image description here

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

Comments

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.