我正在尝试使用 Python 2.7 从 PyScripter 将 Matplot 生成的图像插入到来自 Python 的 Google Sheet 中。这是来自 csv 的起始数据:
x Date orcl
0 10/17/2016 37.685646
20 11/14/2016 38.679703
40 12/13/2016 40.116657
60 1/12/2017 38.732384
80 2/10/2017 40.303417
100 3/13/2017 42.072063
120 4/10/2017 43.662434
140 5/9/2017 45.131077
160 6/7/2017 45.051693
180 7/6/2017 48.47522
200 8/3/2017 50.032654
220 8/31/2017 50.132263
240 9/29/2017 48.160038
260 10/27/2017 50.880001
280 11/27/2017 48.880001
以下是进口:
import pandas as pd
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
从文件中获取数据并生成图:
file = pd.read_csv('orcl.csv')
x = file.iloc[ :, -3]
y = file.iloc[ :, -1]
m, b = np.polyfit(x, y, 1)
results = sm.OLS(y,x).fit()
print ('results are: ', (results))
fig, ax = plt.subplots()
plt.plot(x, y, '.', label = None)
plt.plot(x, m*x + b, '-',label='Regression Line')
ax.scatter(x, y)
ax.set_xlabel(r'days_from_start', fontsize=15)
ax.set_ylabel(r'stock_price', fontsize=15)
ax.grid(True)
fig.tight_layout()
plt.legend()
plt.savefig("orcl.png", bbox_inches='tight')
plt.show()
Pyscripter 在调试会话中挂断,即使我在 > 工具 > 选项 > IDE 选项 > 异常事后关闭了调试,直到我关闭绘图窗口。此代码不会关闭情节:
plt.close()
我在使用 ExecJS 时遇到问题:
import execjs
# execjs from here: https://pypi.python.org/pypi/PyExecJS
# javascript from here: https://stackoverflow.com/questions/
#43664483/insert-image-into-google-sheets-cell-using-google-sheets-api
ctx = execjs.compile( """ {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var formulaSheet = ss.getSheetByName("orcl"); '''tab 'orcl' is present.
var formulaCell = formulaSheet.getRange("B5");
formulaCell.setFormula('=IMAGE("orcl.png",4,100,200)')
} #http://finviz.com/fut_chart.ashx?t=ES&p&p=m5&s=m
""")
import execjs.runtime_names
jscript = execjs.get(execjs.runtime_names.JScript)
jscript.eval(ctx)
Python说“异常,属性错误,上下文没有属性'strip'”。
任何帮助表示赞赏。