gpt4 book ai didi

python - Matplotlib 未设置最小 y 轴

转载 作者:行者123 更新时间:2023-12-04 03:42:03 24 4
gpt4 key购买 nike

这可能是一个简单的问题,但设置底部 y=axis 根本行不通。我相信它是由于我使用面积图而发生的,但我不确定如何解决它。这是我当前的代码。

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)

改变顶部的值非常有效,但底部不会。

最佳答案

设置 ylim 后调用的绘图命令再次更改这些限制。在 plot 命令 之后调用 set_ylim() 解决了这个问题:

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

关于python - Matplotlib 未设置最小 y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65863110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com