Matplotlib为工作日创建烛台图表

时间:2015-12-05 16:05:20

标签: python pandas matplotlib

我在生成烛台图时有以下代码。我想问一下如何操纵它只能获得工作日的图表。目前我已经使用了以下stackexhange问​​题求助(How do I plot only weekdays using Python's matplotlib candlestick?),但我一直遇到索引错误。

import os
import numpy as np
import pandas as pd
from pandas.io.data import DataReader
import mysql.connector
from pandas.io.sql import frame_query
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.finance import candlestick_ohlc

df = pd.DataFrame()
symbols = ['GOOG','AAPL']

for symbol in symbols:
    ClosingPrice = DataReader(symbol, 'yahoo', datetime(2015,11,1), datetime(2015,11,30))
    ClosingPrice = ClosingPrice.reset_index()
    ClosingPrice['Symbol'] = symbol
    df = df.append(ClosingPrice)

def createchart(name):

    df4 = (df.loc[df['Symbol']==name])
    df4['date2num']=df4['Date'].apply(lambda date: mdates.date2num(date.to_pydatetime()))
    cols = df4.columns.tolist()
    cols = cols[-1:] + cols[:-1]
    df4 = df4[cols]
    df4 = df4.drop('Date',1)
    quotes = np.array(df4)

    fig, (ax1,ax2)=plt.subplots(2,1,sharex=True,figsize=(8,6),gridspec_kw=dict(height_ratios=[3.236,1]))
    candlestick_ohlc(ax1, quotes, width = 0.6, colorup = 'g', colordown = 'r')
    ax1.set_ylabel('Stock Price')
    ax1.set_title(name+ " Stock Price")
    ax1.grid(True)
    ax1.xaxis_date()

    ax2.bar(quotes[:,0]-0.25,quotes[:,5],width = 0.5)
    ax2.set_ylabel('Volume')
    ax2.axes.yaxis.set_ticklabels([])
    ax2.grid(True)
    ax2.autoscale_view()
    plt.subplots_adjust(left=.09, bottom =.15, right = .94, top = 0.94, wspace = .2, hspace = 0.0)
    plt.setp(plt.gca().get_xticklabels(),rotation = 45)
    plt.show()
    return

Stockname = str(raw_input("Please enter the stock you want to examine: "))
createchart(Stockname)

1 个答案:

答案 0 :(得分:0)

如果没有mysql.connectorframe_query行,并且使用注释的x标签钓鱼线,我就可以生成此图:

enter image description here

这是你追求的吗?如果是这样,我建议某些事情已经过时或破裂。我正在使用:

蟒: 3.5.0 |Anaconda 2.4.0 (x86_64)| (default, Oct 20 2015, 14:39:26) [GCC 4.2.1 (Apple Inc. build 5577)]

熊猫: 0.17.0

matplotlib: 1.5.0