matplotlib plt.show() 和 plt.savefig() 的不同输出

问题描述

保存时生成的图形有 2 个图形和与我设置的不同的 x 轴。当我使用 plt.show() 时,它看起来像我期望的那样,但当我使用 plt.savefig() 时却不是。这是我使用 plt.show() 时得到的

enter image description here

这是我使用 plt.savefig()

enter image description here

这是我的代码

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 (将#修改为@)