4

This is probably a simple question, but setting the bottom y=axis simply isn't working. I believe it is occurring as a result of me using the area chart, but I'm not sure how to resolve it. Here is my current code.

fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)
fig.suptitle('SPY Chart')
ax1.set_ylim(bottom=150, top=450)
spyData['Close'].plot(kind='area',ax=ax1)
spyData['Close'].plot(ax=ax2)

Changing the value of the top works perfectly well, but the bottom won't.

2
  • with sharey=True the y-axes of both plots are shared. Changing one will automatically change the other one. If this isn't what you want, you can set sharey=False (the default). Commented Jan 23, 2021 at 19:11
  • @JohanC I would like it to be shared, but for 150 to be the bottom for both. Is this possible with sharey enabled? Commented Jan 23, 2021 at 19:17

1 Answer 1

1

The plot commands called after setting the ylims changes these limits again. Calling set_ylim() after the plot commands solves this:

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

spyData = pd.DataFrame({'Close': np.random.normal(0.1, 10, 200).cumsum() + 200})
spyData.set_index(pd.date_range('20200101', periods=len(spyData)), inplace=True)

fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)
spyData['Close'].plot(kind='area', ax=ax1)
spyData['Close'].plot(ax=ax2)
ax1.set_ylim(bottom=150, top=450)
ax1.margins(x=0) # suppress white space left and right
plt.show()

example plot

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.