如何使用Python中的DataFrame绘制烛台图?

时间:2019-01-26 00:28:50

标签: matplotlib finance

我无法弄清楚如何用python绘制烛台OHLC图表。自从不推荐使用matplotlib.finance以来,我就遇到了这个问题...感谢您的帮助!

DataFrame的“引号”是一个excel(无法在此处粘贴),但具有以下列:

索引([“日期”,“打开”,“高”,“低”,“关闭”],dtype =“对象”)

我也有默认索引。 “日期”列是 pandas._libs.tslibs.timestamps.Timestamp

运行代码时,出现以下错误:

文件“”,第30行,在     Candlestick_ohlc(ax,zip(mdates.date2num(quotes.index.to_pydatetime()), AttributeError:“ RangeIndex”对象没有属性“ to_pydatetime”

这是我的代码:

import datetime

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.dates import MONDAY, DateFormatter, DayLocator, 
WeekdayLocator

from mpl_finance import candlestick_ohlc

date1 = "2004-2-1"
date2 = "2004-4-12"


mondays = WeekdayLocator(MONDAY)
alldays = DayLocator()
weekFormatter = DateFormatter('%b %d')
dayFormatter = DateFormatter('%d')


fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)

candlestick_ohlc(ax, zip(mdates.date2num(quotes.index.to_pydatetime()), 
quotes['Open'], quotes['High'],
quotes['Low'], quotes['Close']),
width=0.6)

ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, 
horizontalalignment='right')

plt.show()

1 个答案:

答案 0 :(得分:0)

如果在构建DataFrame时未指定索引,它将默认为function setValue(cellname, value) { SpreadsheetApp.getActiveSpreadsheet().getRange(cellname).setValue(value); } function getValue(cellname) { return SpreadsheetApp.getActiveSpreadsheet().getRange(cellname).getValue(); } function getNextRow(){ return SpreadsheetApp.getActiveSpreadsheet().getLastRow() + 1; } function setTeam1(){ addRecord(getValue('b2'), new Date(), '1'); } function addRecord(a, b, c) { var row = getNextRow(); setValue('a'+ row, a); setValue('b'+ row, b); setValue('c'+ row, c); } function finished() { addRecord1(getValue('f1'), getValue('f2'), getValue('f3')); } function addRecord1(j, k, l) { var row = getNextRow(); setValue('j'+ row, j); setValue('k'+ row, k); setValue('l'+ row, l); } ,该索引仅连续对行进行编号。这个RangeIndex显然不能转换为日期-因此是错误。 RangeIndex函数将read_excel作为参数来指定将哪一列用作索引。您可能还必须提供index_col