plt.figure(figsize=(10,8))

sns.regplot(x=data[num],y=data['y'], marker="o",ci=95,
                scatter_kws={"color":"b","alpha":0.2,"s":3},
                fit_reg=False
                )#线性回归   ci=95表示95%置信区间
x = data['x'].to_numpy()
y = data['y'].to_numpy()
    
parameter = np.polyfit(x, y, 1) # n=1为一次函数,返回函数参数
print(parameter)
f = np.poly1d(parameter) # 拼接方程


ax=plt.gca();#获得坐标轴的句柄
ax.text(0,1,'$y=$'+str(round(parameter[0],4))+'$x$'+'+'+str(round(parameter[1],2)),fontsize=15,transform=ax.transAxes)

plt.plot(x, f(x),"--",color='r')
plt.rc('ytick',labelsize=15)
plt.rc('xtick',labelsize=15)
     
plt.savefig(path+ str(num) + '.png', bbox_inches = 'tight')
plt.savefig(path+ str(num) + '.eps', bbox_inches = 'tight')

plt.show()

原因:图像尺寸超出了范围,问题来自plt.text()

解决方法:

plt.text()中添加transform=ax.transAxes

使用 transform=ax.transAxes 整个代码表示坐标是相对于轴边界框给出的,

(0,0)是轴的左下角,(1,1)是右上角。

reference:

https://www.osgeo.cn/matplotlib/tutorials/text/text_props.html

https://blog.csdn.net/xu380393916/article/details/105009617

https://stackoverflow.com/questions/52375207/matplotlib-error-image-size-of-362976x273-pixels-is-too-large

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐