2013-07-18 5 views
3

x 축의 형식이 % Y- % m- % d 인 timeseries의 플롯에 빨간 수직선을 추가하려고합니다. 회선을 추가하려는 날짜는 2013-05-14입니다. 간단하게 "() plt.show"전에 줄을 추가 :matplotlib에서 날짜 형식의 시계열에 세로선 추가하기

plt.axvline(x=2013-05-14) 

나 : 여기

RuntimeError: RRuleLocator estimated to generate 23972 ticks from 0044-05-12 23:59:59.999990+00:00 to 2013-06-07 00:00:00.000010+00:00: exceeds Locator.MAXTICKS * 2 (2000) 

그것으로 잘 작동하는 기능입니다 :

plt.axvline(x='2013-05-14') 

는 오류를 반환 is :

def time_series(self): 
    fig = plt.figure(figsize=(20, 20), frameon = False) 
    ax1 = fig.add_subplot(3, 1, 1) 


    d_dates, d_flux_n2o, d_sem_n2o = np.loadtxt('%stime_series/dynamic.csv' %self.dir['r_directory'], delimiter = ',', unpack=True, converters={0: mdates.strpdate2num('%Y-%m-%d')}) 

    ax1.set_xlabel('Date') 
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) 
    ax1.xaxis.set_major_locator(mdates.MonthLocator()) 
    ax1.xaxis.set_minor_locator(mdates.DayLocator()) 
    plt.gcf().autofmt_xdate() 

    ax1.errorbar(d_dates, d_flux_n2o, yerr=d_sem_n2o, fmt="y-", linewidth=1.5, label = 'Biodynamic') 
    ax1.legend(loc = 0) 

    plt.show() 

답변

6

axvline 메서드는 문자열이 아닌 숫자 값을 처리합니다. 당신은 날짜의 표현을 datenums으로 변환하는 변환기를 정의함으로써이를 달성 할 수 있습니다. 이미 귀하의 np.loadtxt 메서드 호출에 그러한 변환기가 하나 있습니다. 함수로 정의하면 데이터를로드 할 때와 단일 날짜 문자열에 모두 사용할 수 있습니다.

import matplotlib.dates as mdates 
import matplotlib.pyplot as plt 
import numpy as np 


def time_series(self): 
    fig = plt.figure(figsize=(20, 20), frameon = False) 
    ax1 = fig.add_subplot(3, 1, 1) 

    # Converter to convert date strings to datetime objects 
    conv = np.vectorize(mdates.strpdate2num('%Y-%m-%d')) 


    d_dates, d_flux_n2o, d_sem_n2o = np.loadtxt('%stime_series/dynamic.csv' %self.dir['r_directory'], delimiter = ',', unpack=True, converters={0: conv}) 

    ax1.errorbar(d_dates, d_flux_n2o, yerr=d_sem_n2o, fmt="y-", linewidth=1.5, label = 'Biodynamic') 
    ax1.legend(loc = 0) 
    ax1.axvline(conv('2013-05-14'), color='r', zorder=0) 

    ax1.set_xlabel('Date') 
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) 
    ax1.xaxis.set_major_locator(mdates.MonthLocator()) 
    ax1.xaxis.set_minor_locator(mdates.DayLocator()) 
    plt.gcf().autofmt_xdate() 

    plt.show() 

enter image description here