Matplotlib - 值错误 - DateFormatter发现值x = 0,这是一个非法日期

时间:2016-04-04 22:21:15

标签: python django python-3.x matplotlib graph

我正在尝试将一个形状绘制到matplotlib生成的图形上,但是会抛出错误。这是我的全部功能......

 def graphData(self, stock):
    stock_file = Company.objects.get(ticker_name=stock)
    stock_file = stock_file.data
    # Formatting data using find replace and regular expressions(Regex).
    stock_file = stock_file.replace("[","").replace("]","").replace("'","")
    stock_file = re.sub("(([^,]*,){5}[^,]*),\s*","\\1\n",stock_file)
    file_name = "/home/philip/PycharmProjects/stockmarket/static/stock_data/data_file.txt"
    file = open(file_name, "w")
    file.write(stock_file)
    file.close()
    date, closep, highp, lowp, openp, volume = np.loadtxt(file_name, delimiter=",", unpack=True, converters={ 0: date_converter })
    fig = plt.figure()
    ax1 = plt.subplot(1,1,1)
    ax1.plot(date, openp)
    ax1.plot(date, highp)
    ax1.plot(date, lowp)
    ax1.plot(date, closep)
    ax1.grid(True)
    ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
    for label in ax1.xaxis.get_ticklabels():
        label.set_rotation(90)
    plt.subplots_adjust(left=.10, bottom=.22, right=.93, top=.95)
    plt.xlabel('Date')
    plt.ylabel('Stock Price')
    plt.suptitle(stock+': Stock Price')


    matplotlib.pyplot.axvspan(20122015, 20122015, facecolor='0.5', alpha=0.5)   


    plt.show()
    plt.savefig("/home/philip/PycharmProjects/stockmarket/static/graph.svg")

请原谅我正在尝试修复此错误的代码的混乱性质,因此它被拆开了。与其他线分开的线是我添加的线,用于尝试添加垂直跨度。

但是,当我运行此代码时,它会出现此错误...

ValueError at /stocks/

DateFormatter found a value of x=0, which is an illegal date.  This usually occurs because you have not informed the axis that it is plotting dates, eg with ax.xaxis_date()

当我尝试将代码更改为...时

matplotlib.pyplot.axvspan(ax1.xaxis_date(20122015), ax1.xaxis_date(20122015), facecolor='0.5', alpha=0.5)

发生另一个错误......

TypeError at /stocks/

tzinfo argument must be None or of a tzinfo subclass, not type 'int'

有谁知道如何修复此错误。

非常感谢。

0 个答案:

没有答案