Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib not setting minimum y-axis

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.

like image 282
John Avatar asked Jan 19 '26 16:01

John


1 Answers

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

like image 163
JohanC Avatar answered Jan 22 '26 06:01

JohanC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!