【问题标题】:Subplots with dates on the x-axisx 轴上带有日期的子图
【发布时间】:2011-11-04 14:15:19
【问题描述】:

我在使用多个以 x 轴为日期的子图时遇到问题。

我正在使用来自here 的 matplotlib 示例。我已经修改它以包含另一个子图(绘制的数据是相同的)。这就是我得到的输出:

刻度仅出现在第二个子图上。为什么?如何让它们出现在两个子图上?

这是我修改后的来源。我添加了代码以在源代码中途的 if 块中包含一个新的子图。

#!/usr/bin/env python
"""
Show how to make date plots in matplotlib using date tick locators and
formatters.  See major_minor_demo1.py for more information on
controlling major and minor ticks

All matplotlib date plotting is done by converting date instances into
days since the 0001-01-01 UTC.  The conversion, tick locating and
formatting is done behind the scenes so this is most transparent to
you.  The dates module provides several converter functions date2num
and num2date

"""
import datetime
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
import matplotlib.cbook as cbook

years    = mdates.YearLocator()   # every year
months   = mdates.MonthLocator()  # every month
yearsFmt = mdates.DateFormatter('%Y')

# load a numpy record array from yahoo csv data with fields date,
# open, close, volume, adj_close from the mpl-data/example directory.
# The record array stores python datetime.date as an object array in
# the date column
#datafile = cbook.get_sample_data('goog.npy')
datafile = 'goog.npy'
r = np.load(datafile).view(np.recarray)

fig = plt.figure()
ax = fig.add_subplot(211)
ax.plot(r.date, r.adj_close)


# format the ticks
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yearsFmt)
ax.xaxis.set_minor_locator(months)

datemin = datetime.date(r.date.min().year, 1, 1)
datemax = datetime.date(r.date.max().year+1, 1, 1)
ax.set_xlim(datemin, datemax)

# format the coords message box
def price(x): return '$%1.2f'%x
ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.format_ydata = price
ax.grid(True)

second = True
if second:
    years    = mdates.YearLocator()   # every year
    months   = mdates.MonthLocator()  # every month
    yearsFmt = mdates.DateFormatter('%Y')

    ax = fig.add_subplot(212)
    ax.plot(r.date, r.adj_close)

    # format the ticks
    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yearsFmt)
    ax.xaxis.set_minor_locator(months)

    datemin = datetime.date(r.date.min().year, 1, 1)
    datemax = datetime.date(r.date.max().year+1, 1, 1)
    ax.set_xlim(datemin, datemax)

    # format the coords message box
    ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
    ax.format_ydata = price
    ax.grid(True)

# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()

plt.show()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我找到了罪魁祸首。这是autofmt_xdate 函数:

    日期刻度标签经常重叠,因此旋转它们并右对齐很有用。此外,一个常见的用例是许多具有共享 x 轴的子图,其中 x 轴是日期数据。刻度标签通常很长,它有助于在底部子图上旋转它们并在其他子图上关闭它们,以及关闭 xlabels。

    这是一个“功能”。您可以通过在每个子图之后插入此代码来实现相同的效果:

    plt.xticks(rotation=30)
    

    【讨论】:

    • 附加说明:如果您为每个图使用单独的数字,则需要在再次致电plt.figure() 之前完成。
    猜你喜欢
    • 2018-04-13
    • 2020-09-03
    • 1970-01-01
    • 2014-06-02
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多