在python中绘制多个价格图表

时间:2017-07-10 10:42:54

标签: python database forex

我在udacity(即新手)学习,我坚持这一点。我coppy代码并运行但没有输出

这是udacity链接https://www.youtube.com/watch?v=vmF6iEQzC2A

*我的csv文件与视频不同

here is my csv link

这是我的代码

    """Slice and plot"""

import os
import pandas as pd
import matplotlib.pyplot as plt

def plot_selected(df, columns, start_index, end_index):
    plot_data(df.ix[start_index:end_index,columns],title="Selected Data")

def symbol_to_path(symbol, base_dir="data"):
    """Return CSV file path given ticker symbol."""
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))


def get_data(symbols, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    if 'EURUSDCSV' not in symbols:  # add EUR for reference, if absent
        symbols.insert(0, 'EURUSDCSV')

    for symbol in symbols:
        df_temp = pd.read_csv(symbol_to_path(symbol), index_col='DateTime',
                              parse_dates=True, usecols=['DateTime', 'Close'], na_values=['nan'])
        df_temp = df_temp.rename(columns={'Close': symbol})
        df = df.join(df_temp)
        if symbol == 'EURUSDCSV':  # drop dates SPY did not trade
            df = df.dropna(subset=["EURUSDCSV"])

    return df


def plot_data(df, title="Stock prices"):
    """Plot stock prices with a custom title and meaningful axis labels."""
    ax = df.plot(title=title, fontsize=12)
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")
    plt.show()


def test_run():
    # Define a date range
    dates = pd.date_range('2015-01-01', '2015-12-31')

    # Choose stock symbols to read
    symbols = ['EURUSDCSV', 'USDJPYcsv']  # SPY will be added in get_data()

    # Get stock data
    df = get_data(symbols, dates)

    # Slice and plot
    plot_selected(df, ['EURUSDCSV', 'USDJPYcsv'], '2015-01-01', '2015-12-31')


if __name__ == "__main__":
    test_run()

1 个答案:

答案 0 :(得分:0)

查看第9行,即报告错误之前的行:

plot_data(df.ix[start_index:end_index,columns],title="Selected Data)"

结束语的位置错误,应该在右括号之前,即

plot_data(df.ix[start_index:end_index,columns],title="Selected Data")