gpt4 book ai didi

python - Matplotlib 在前两个子图中未显示 xlabel

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:15 25 4
gpt4 key购买 nike

我编写了一个函数来在此处显示一些图表:

def plot_price_series(df, ts1, ts2):
# price series line graph
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax1.plot(df.index, df[ts1], label=ts1)
ax1.plot(df.index, df[ts2], label=ts2)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
ax1.set_xlim(df.index[0], df.index[-1])
ax1.grid(True)

fig.autofmt_xdate()
ax1.set_xlabel('Month/Year')
ax1.set_ylabel('Price')
ax1.set_title('%s and %s Weekly Prices' % (ts1, ts2))
plt.legend()

# Spread
ax2 = fig.add_subplot(222)
ax2.plot(df.index, df[ts2] - df[ts1], label=ts2 + " vs " + ts1 + " spread")
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
ax2.set_xlim(df.index[0], df.index[-1])
ax2.grid(True)
fig.autofmt_xdate()

ax2.set_xlabel('Month/Year')
ax2.set_ylabel('Spread')
ax2.set_title('%s and %s Weekly Spread' % (ts1, ts2))

# Scatter w/ line of least square
ax3 = fig.add_subplot(223)
m, b = np.polyfit(df[ts1], df[ts2], 1)
ax3.plot(df[ts1], m * df[ts1] + b, '-k')
ax3.scatter(df[ts1], df[ts2])
ax3.grid(True)
ax3.set_xlabel(ts1)
ax3.set_ylabel(ts2)
ax3.set_title('%s and %s Scatter Plot' % (ts1, ts2))

ax4 = fig.add_subplot(224)
corr = pd.rolling_corr(df[ts1], df[ts2], window=10)
ax4.plot(df.index, corr)
ax4.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
ax4.set_xlim(df.index[0], df.index[-1])
ax4.grid(True)
fig.autofmt_xdate()

ax4.set_xlabel('Month/Year')
ax4.set_ylabel('Price ($)')
ax4.set_title('Rolling 10-week Correlation')

plt.show()

但是,当我使用有效数据运行此函数时,前两个图 ax1ax2xlabel 没有出现,也没有我需要显示的任何日期值。图表如下:

Current Subplots

关于如何解决此问题以便我可以看到 xlabels 和 x 轴值的任何想法?我尝试了使用 figure.tight_layout() 建议的许多其他答案,但无济于事。

最佳答案

我认为问题出在 fig.autofmt_xdate() 上。您的日期 x 轴在不同的子图中都是相同的,fig.autofmt_xdate() 倾向于使用 sharex 格式,以便日期 x 轴确实显示只有一个子图。

关于python - Matplotlib 在前两个子图中未显示 xlabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32215084/

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