简体   繁体   English

在x轴上具有日期的子图

[英]Subplots with dates on the x-axis

I'm having trouble using multiple subplots with dates on the x-axis. 我在使用x轴上有多个日期的多个子图时遇到了麻烦。

I'm using the matplotlib example from here . 我在这里使用matplotlib示例。 I've modified it to include another subplot (the data being plotted is the same). 我已将其修改为包含另一个子图(绘制的数据相同)。 This is what I'm getting as output: 这是我得到的输出:

在此输入图像描述

The ticks appear only on the second subplot. 刻度仅出现在第二个子图上。 Why? 为什么? How can I make them appear on both subplots? 如何让它们出现在两个子图上?

Here's my modified source. 这是我修改后的来源。 I've added code to include a new subplot in the if block halfway through the source. 我添加了代码,在源代码中途的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()

I've found the culprit. 我找到了罪魁祸首。 It's the autofmt_xdate function: 这是autofmt_xdate函数:

Date ticklabels often overlap, so it is useful to rotate them and right align them. 日期刻度标签经常重叠,因此旋转它们并右对齐它们很有用。 Also, a common use case is a number of subplots with shared xaxes where the x-axis is date data. 此外,常见的用例是具有共享xax的多个子图,其中x轴是日期数据。 The ticklabels are often long, and it helps to rotate them on the bottom subplot and turn them off on other subplots, as well as turn off xlabels. tick标签通常很长,它有助于在底部子图上旋转它们并在其他子图上关闭它们,以及关闭xlabels。

It's a "feature". 这是一个“功能”。 You can achieve the same effect by inserting this code after each subplot: 您可以通过在每个子图之后插入此代码来实现相同的效果:

plt.xticks(rotation=30)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM