Image size of 512922x322 pixels is too large. It must be less than 2^16 in each direction

本文介绍了如何在Matplotlib中解决图像尺寸超出范围的问题,重点在于使用`transform=ax.transAxes`调整`plt.text()`函数的坐标,确保文本在适当位置显示。通过实例展示了如何进行一次线性回归并添加坐标轴上的文字说明。
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.youkuaiyun.com/xu380393916/article/details/105009617

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

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值