我正在繪製燭臺圖表(ohlc +音量),不幸的是我無法弄清楚如何以日期時間格式顯示日期。有幾種方法不起作用。Matplotlib日期時間格式的日期
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick_ohlc
from matplotlib import style
import pandas as pd
style.use('classic')
def graph(candlestick_list):
df = pd.DataFrame(candlestick_list)
ohlc = df[['date', 'open', 'high', 'low', 'close']]
ax1 = plt.subplot2grid((6,1), (0,0), rowspan=5, colspan=1)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan=5, colspan=1, sharex=ax1)
candlestick_ohlc(ax1, ohlc.values, width=0.5, colorup='g', colordown='r')
ax2.bar(df['date'], df['volume'])
plt.show()
不幸的是,以下的方法並沒有爲我工作:
df['date'] = pd.to_datetime(df['date'], unit='s')
TypeError: unsupported operand type(s) for -: 'Timestamp' and 'float'
或者這一個:
ValueError: year 4095909 is out of range
有沒有人有想法? 非常感謝提前!
PS:下面的列表可用於測試代碼
candlestick_list = [{'date': 1496332800, 'high': 215.3209, 'low': 200.3, 'open': 211.18, 'close': 206.47999999, 'volume': 2432435.9201853, 'quoteVolume': 11813.26856836, 'weightedAverage': 205.90710404}, {'date': 1496340000, 'high': 212.0001, 'low': 205.75400533, 'open': 206.47999992, 'close': 207.85, 'volume': 1301024.6514137, 'quoteVolume': 6250.47612412, 'weightedAverage': 208.14808753}, {'date': 1496347200, 'high': 212.44999999, 'low': 206.9813717, 'open': 208.49981874, 'close': 209.99811003, 'volume': 1062083.1773221, 'quoteVolume': 5056.26107738, 'weightedAverage': 210.05307302}, {'date': 1496354400, 'high': 213.20999999, 'low': 209.61231001, 'open': 210, 'close': 211.44, 'volume': 705968.23009208, 'quoteVolume': 3335.35026592, 'weightedAverage': 211.66239639}, {'date': 1496361600, 'high': 219, 'low': 210.81410968, 'open': 211.43999994, 'close': 215.41715998, 'volume': 972040.25543603, 'quoteVolume': 4500.001815, 'weightedAverage': 216.00885852}]
嗨,我很抱歉,但'df ['date'] = pd.to_datetime(df ['date'],unit ='s')'工作正常,它會正確更改列dtype。至於其他線我不知道誰mdates或mticker是如此不能真正檢查出來^^' –
'df ['date'] = pd.to_datetime(df ['date'],unit ='s' )'也適用於我 –
嗯奇怪..我正在使用'Python 3.6','matplotlib 2.0.2','熊貓0.18.1'和你? – saitam