基本上要设置问题,我首先执行以下操作:
import pandas as pd
import numpy as np
import helper
import project_helper
import yfinance as yf
stock_list = ['A', 'AAL', 'AAP', 'AAPL', 'ABBV', 'ABC', 'ABT', 'ACN', 'ADBE', 'ADI', 'ADM', 'ADP', 'ADS', 'ADSK', 'AEE', 'AEP', 'AES', 'AET', 'AFL', 'AGN', 'AIG', 'AIV', 'AIZ', 'AJG', 'AKAM', 'ALB', 'ALGN', 'ALK', 'ALL', 'ALLE', 'ALXN', 'AMAT', 'AMD', 'AME', 'AMG', 'AMGN', 'AMP', 'AMT', 'AMZN', 'ANDV', 'ANSS', 'ANTM', 'AON', 'AOS', 'APA', 'APC', 'APD', 'APH', 'ARE', 'ARNC', 'ATVI', 'AVB', 'AVGO', 'AVY', 'AWK', 'AXP', 'AYI', 'AZO', 'BA', 'BAC', 'BAX', 'BBT', 'BBY', 'BCR', 'BDX', 'BEN', 'BIIB', 'BK', 'BLK', 'BLL', 'BMY', 'BSX', 'BWA', 'BXP', 'C', 'CA', 'CAG', 'CAH', 'CAT', 'CB', 'CBG', 'CBOE', 'CBS', 'CCI', 'CCL', 'CELG', 'CERN', 'CF', 'CFG', 'CHD', 'CHK', 'CHRW', 'CHTR', 'CI', 'CINF', 'CL', 'CLX', 'CMA', 'CMCSA', 'CME', 'CMG', 'CMI', 'CMS', 'CNC', 'CNP', 'COF', 'COG', 'COL', 'COO', 'COP', 'COST', 'COTY', 'CPB', 'CRM', 'CSCO', 'CSRA', 'CSX', 'CTAS', 'CTL', 'CTSH', 'CTXS', 'CVS', 'CVX', 'CXO', 'D', 'DAL', 'DE', 'DFS', 'DG', 'DGX', 'DHI', 'DHR', 'DIS', 'DISCA', 'DISCK', 'DISH', 'DLR', 'DLTR', 'DOV', 'DPS', 'DRE', 'DRI', 'DTE', 'DUK', 'DVA', 'DVN', 'DXC', 'EA', 'EBAY', 'ECL', 'ED', 'EFX', 'EIX', 'EL', 'EMN', 'EMR', 'EOG', 'EQIX', 'EQR', 'EQT', 'ES', 'ESRX', 'ESS', 'ETFC', 'ETN', 'ETR', 'EVHC', 'EW', 'EXC', 'EXPD', 'EXPE', 'EXR', 'F', 'FAST', 'FB', 'FBHS', 'FCX', 'FDX', 'FE', 'FFIV', 'FIS', 'FISV', 'FITB', 'FL', 'FLIR', 'FLR', 'FLS', 'FMC', 'FOX', 'FOXA', 'FRT', 'FTI', 'FTV', 'GD', 'GE', 'GGP', 'GILD', 'GIS', 'GLW', 'GM', 'GOOG', 'GOOGL', 'GPC', 'GPN', 'GPS', 'GRMN', 'GS', 'GT', 'GWW', 'HAL', 'HAS', 'HBAN', 'HBI', 'HCA', 'HCN', 'HCP', 'HD', 'HES', 'HIG', 'HLT', 'HOG', 'HOLX', 'HON', 'HP', 'HPE', 'HPQ', 'HRB', 'HRL', 'HRS', 'HSIC', 'HST', 'HSY', 'HUM', 'IBM', 'ICE', 'IDXX', 'IFF', 'ILMN', 'INCY', 'INFO', 'INTC', 'INTU', 'IP', 'IPG', 'IR', 'IRM', 'ISRG', 'IT', 'ITW', 'IVZ', 'JBHT', 'JCI', 'JEC', 'JNJ', 'JNPR', 'JPM', 'JWN', 'K', 'KEY', 'KHC', 'KIM', 'KLAC', 'KMB', 'KMI', 'KMX', 'KO', 'KORS', 'KR', 'KSS', 'KSU', 'L', 'LB', 'LEG', 'LEN', 'LH', 'LKQ', 'LLL', 'LLY', 'LMT', 'LNC', 'LNT', 'LOW', 'LRCX', 'LUK', 'LUV', 'LVLT', 'LYB', 'M', 'MA', 'MAA', 'MAC', 'MAR', 'MAS', 'MAT', 'MCD', 'MCHP', 'MCK', 'MCO', 'MDLZ', 'MDT', 'MET', 'MGM', 'MHK', 'MKC', 'MLM', 'MMC', 'MNST', 'MO', 'MON', 'MOS', 'MPC', 'MRK', 'MRO', 'MS', 'MSFT', 'MSI', 'MTB', 'MTD', 'MU', 'MYL', 'NAVI', 'NBL', 'NDAQ', 'NEE', 'NEM', 'NFLX', 'NFX', 'NI', 'NKE', 'NLSN', 'NOC', 'NOV', 'NRG', 'NSC', 'NTAP', 'NTRS', 'NUE', 'NVDA', 'NWL', 'NWS', 'NWSA', 'O', 'OKE', 'OMC', 'ORCL', 'ORLY', 'OXY', 'PAYX', 'PBCT', 'PCAR', 'PCG', 'PDCO', 'PEG', 'PEP', 'PFE', 'PFG', 'PG', 'PGR', 'PH', 'PHM', 'PKG', 'PKI', 'PLD', 'PM', 'PNC', 'PNR', 'PNW', 'PPG', 'PPL', 'PRGO', 'PRU', 'PSA', 'PSX', 'PVH', 'PWR', 'PX', 'PXD', 'PYPL', 'QCOM', 'QRVO', 'RCL', 'RE', 'REG', 'REGN', 'RF', 'RHI', 'RHT', 'RJF', 'RL', 'RMD', 'ROK', 'ROP', 'ROST', 'RRC', 'RSG', 'RTN', 'SBAC', 'SBUX', 'SCG', 'SCHW', 'SEE', 'SHW', 'SIG', 'SJM', 'SLB', 'SLG', 'SNA', 'SNI', 'SNPS', 'SO', 'SPG', 'SPGI', 'SPLS', 'SRCL', 'SRE', 'STI', 'STT', 'STX', 'STZ', 'SWK', 'SWKS', 'SYF', 'SYK', 'SYMC', 'SYY', 'T', 'TAP', 'TDG', 'TEL', 'TGT', 'TIF', 'TJX', 'TMK', 'TMO', 'TRIP', 'TROW', 'TRV', 'TSCO', 'TSN', 'TSS', 'TWX', 'TXN', 'TXT', 'UA', 'UAA', 'UAL', 'UDR', 'UHS', 'ULTA', 'UNH', 'UNM', 'UNP', 'UPS', 'URI', 'USB', 'UTX', 'V', 'VAR', 'VFC', 'VIAB', 'VLO', 'VMC', 'VNO', 'VRSK', 'VRSN', 'VRTX', 'VTR', 'VZ', 'WAT', 'WBA', 'WDC', 'WEC', 'WFC', 'WHR', 'WLTW', 'WM', 'WMB', 'WMT', 'WRK', 'WU', 'WY', 'WYN', 'WYNN', 'XEC', 'XEL', 'XL', 'XLNX', 'XOM', 'XRAY', 'XRX', 'XYL', 'YUM', 'ZBH', 'ZION', 'ZTS']
df = yf.download(tickers = ' '.join(stock_list), period = '5y')
close = df['Adj Close']
resampled_close = close.resample('M').last()
因此,project_helper和帮助程序是我编写的外部 python 脚本,其中包含各种功能。因此,我执行以下代码行:
project_helper.plot_resampled_prices(
resampled_close.loc[:, 'A'],
close.loc[:, 'A'],
'{} Stock - Close Vs Monthly Close'.format('A'))
其中"A"是resampled_close中列的列标题,以及关闭中其他列的列标题。以下是project_helper中的相关功能:
import helper
import plotly.graph_objs as go
import plotly.offline as offline_py
def plot_resampled_prices(df_resampled, df, title):
config = helper.generate_config()
layout = go.Layout(title=title)
traces = _generate_traces([
('Resampled Close', df_resampled,
'#2D3ECF'),
('Close', df, '#B6B2CF')])
offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def _generate_traces(name_df_color_data):
traces = []
for name, df, color in name_df_color_data:
traces.append(go.Scatter(
name=name,
x=df.index,
y=df,
mode="lines",
line={'color': color}))
return traces
以下是帮助程序中的相关函数:
import pandas as pd
import os
import tempfile
import zipfile
import glob
from tqdm import tqdm
import math
import requests
def generate_config():
return {'showLink': False, 'displayModeBar': False, 'showAxisRangeEntryBoxes': True}
然而,每当我运行此处重新显示的命令时:
project_helper.plot_resampled_prices(
resampled_close.loc[:, 'A'],
close.loc[:, 'A'],
'{} Stock - Close Vs Monthly Close'.format('A'))
它会抛出此错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-68-ec170e90e66c> in <module>
4 df1.loc['A'],
5 df2.loc['A'],
----> 6 '{} Stock - Close Vs Monthly Close'.format('A'))
~Downloadsproject_helper.py in plot_resampled_prices(df_resampled, df, title)
72 '#2D3ECF'),
73 ('Close', df, '#B6B2CF')])
---> 74
75 offline_py.iplot({'data': traces, 'layout': layout}, config=config)
76
~Downloadsproject_helper.py in _generate_traces(name_df_color_data)
22 y=df,
23 mode="lines",
---> 24 line={'color': color}))
25 return traces
26
~Anaconda3libsite-packagesplotlygraph_objs_scatter.py in __init__(self, arg, cliponaxis, connectgaps, customdata, customdatasrc, dx, dy, error_x, error_y, fill, fillcolor, hoverinfo, hoverinfosrc, hoverlabel, hoveron, hovertext, hovertextsrc, ids, idssrc, legendgroup, line, marker, mode, name, opacity, r, rsrc, selected, selectedpoints, showlegend, stream, t, text, textfont, textposition, textpositionsrc, textsrc, tsrc, uid, unselected, visible, x, x0, xaxis, xcalendar, xsrc, y, y0, yaxis, ycalendar, ysrc, **kwargs)
2083 trace that can be used in various text attributes.
2084 Attributes such as trace `name`, graph, axis and
-> 2085 colorbar `title.text`, annotation `text`
2086 `rangeselector`, `updatemenues` and `sliders` `label`
2087 text all support `meta`. To access the trace `meta`
~Anaconda3libsite-packagesplotlybasedatatypes.py in __setattr__(self, prop, value)
2664 self._in_batch_mode = False
2665
-> 2666 # Apply batch animate
2667 # -------------------
2668 self._perform_batch_animate(
~Anaconda3libsite-packagesplotlygraph_objs_scatter.py in mode(self, val)
828 points along the resulting SVG path is
829 unaffected.
--> 830 smoothing
831 Has an effect only if `shape` is set to
832 "spline" Sets the amount of smoothing. 0
~Anaconda3libsite-packagesplotlybasedatatypes.py in __setitem__(self, prop, value)
2636 duration and easing parameters.
2637
-> 2638 >>> with fig.batch_animate():
2639 ... fig.layout.xaxis.range = [0, 5]
2640 ... fig.layout.yaxis.range = [0, 10]
~Anaconda3libsite-packagesplotlybasedatatypes.py in _set_prop(self, prop, val)
2866
2867 pretty: bool (default False)
-> 2868 True if JSON representation should be pretty-printed, False if
2869 representation should be as compact as possible.
2870
~Anaconda3libsite-packages_plotly_utilsbasevalidators.py in validate_coerce(self, v)
1506 },
1507 """
-> 1508
1509 def __init__(self, plotly_name, parent_name, **kwargs):
1510 super(ColorscaleValidator, self).__init__(
~Anaconda3libsite-packages_plotly_utilsbasevalidators.py in raise_invalid_val(self, v)
214 # ----------
215 class BaseValidator(object):
--> 216 """
217 Base class for all validator classes
218 """
ValueError:
Invalid value of type 'builtins.str' received for the 'mode' property of scatter
Received value: 'line'
The 'mode' property is a flaglist and may be specified
as a string containing:
- Any combination of ['lines', 'markers', 'text'] joined with '+' characters
(e.g. 'lines+markers')
OR exactly one of ['none'] (e.g. 'none')
有人可以帮忙,我不知道如何解决此错误。 小数据示例示例:
'A' 'AB' 'AC'
close = 2018-03-22 3.28 3.48 5.98
2018-03-23 9.00 5.00 8.94
2018-03-24 9.50 5.20 8.04
试试这个:
project_helper.plot_resampled_prices(
df1.loc['A'],
df2.loc['A'],
{} Stock - Close Vs Monthly Close'.format(list(close.columns.values))[0])