如何解决matplotlib plt.show() 和 plt.savefig() 的不同输出
保存时生成的图形有 2 个图形和与我设置的不同的 x 轴。当我使用 plt.show() 时,它看起来像我期望的那样,但当我使用 plt.savefig() 时却不是。这是我使用 plt.show()
时得到的
这是我的代码:
def linear_regression(name,s,e):
# df=pd.DataFrame(web.get_data_yahoo("ibm","2009-01-03","2020-12-23"))
df = pd.DataFrame(web.get_data_yahoo(name,start=s,end=e))
# The closing price
closing = list(df["Adj Close"])
df.reset_index(inplace=True,drop=False)
dates = list(df["Date"])
plt.ylabel("Price")
plt.xlabel("Days")
x = list(range(len(dates)))
slope,intercept,r,p,std_err = stats.linregress(x,closing)
def eq_of_a_ln(x):
return slope * x + intercept
mymodel = list(map(eq_of_a_ln,x))
plt.plot(x,closing,color="red",label="Current Stock Price")
plt.plot(x,mymodel,label="Linear Regression")
plt.savefig("{}_regression.png".format(name))
我做错了什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。