2015-08-14 48 views
0

請參閱下面的圖表。我想添加水平誤差線,在第一條垂直線周圍延伸+/- 20天,爲第二條垂直線延伸+/- 30天,但是,我一直無法弄清楚如何去做。代碼看起來像下面,然而,我不知道要爲xerr術語指定什麼。Matplotlib錯誤欄,日期之間的距離作爲xerr參數

ax.errorbar(x_loc, y_loc, xerr = [2, 3]) 

enter image description here
這裏是做圖形

import pandas as pd 
import numpy as np 
import matplotlib as mp 
import matplotlib.pylab as plt 

np.random.seed(10) 
dates = pd.date_range("1/1/2001", "1/5/2004") 
y = np.random.randn(len(dates)).cumsum() 

fig, ax = plt.subplots() 
ax.plot(dates, y) 

x_loc = pd.to_datetime(["6/5/2001", "7/1/2003"]) 
ymin, ymax = ax.get_ylim() 
ax.vlines(x_loc, ymin, ymax, lw = 2, linestyle = "--") 

回答

1

代碼這是我做到了。使用熊貓抵消功能pandas.tseries.offsets導入日。

import pandas as pd 
import numpy as np 
import matplotlib as mp 
import matplotlib.pylab as plt 
from pandas.tseries.offsets import Day 
np.random.seed(10) 
dates = pd.date_range("1/1/2001", "1/5/2004") 
y = np.random.randn(len(dates)).cumsum() 

fig, ax = plt.subplots() 
ax.plot(dates, y) 

x_loc = pd.to_datetime(["6/5/2001", "7/1/2003"]) 
ymin, ymax = ax.get_ylim() 
ax.vlines(x_loc, ymin, ymax, lw = 2, linestyle = "--") 
for x, y, z in zip(x_loc, [10,15], np.array([Day(20),Day(30)])): 
    ax.errorbar(x, y, xerr = z) 

enter image description here