Matplotlib在前两个子图中未显示xlabel [英] Matplotlib not showing xlabel in top two subplots

查看:508
本文介绍了Matplotlib在前两个子图中未显示xlabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编写的函数可以在此处显示一些图形:

I have a function that I've written to show a few graphs here:

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都不会显示,也不需要显示任何日期值显示.该图如下:

However, when I run this function with valid data, the xlabel for both of the top two graphs, ax1 and ax2 does not show up, nor do any of the date values that I need to be showing. The graph is below:

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

Any ideas on how I can fix this so that I can see the xlabels and x-axis values? I tried what many other answers suggested with figure.tight_layout() to no avail.

推荐答案

我认为问题出在fig.autofmt_xdate().在不同的子图中,您的日期x轴都相同,并且fig.autofmt_xdate()倾向于使用sharex格式,因此日期x轴确实仅显示在一个子图中.

I think the problem is with fig.autofmt_xdate(). Your date x-axis are all the same across different sub figures, and fig.autofmt_xdate() tends to use a sharex format so that the date x-axis does show up in only one sub figure.

这篇关于Matplotlib在前两个子图中未显示xlabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆