x 轴上带有日期的子图 [英] Subplots with dates on the x-axis

查看:51
本文介绍了x 轴上带有日期的子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用多个在x轴上带有日期的子图时遇到麻烦.

我正在使用 函数:

<块引用>

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

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

plt.xticks(rotation=30)

I'm having trouble using multiple subplots with dates on the x-axis.

I'm using the matplotlib example from here. 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.

#!/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:

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. 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.

It's a "feature". You can achieve the same effect by inserting this code after each subplot:

plt.xticks(rotation=30)

这篇关于x 轴上带有日期的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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