如何使用Pyplot保存图像时保持原始尺寸不变?
问题描述
我正在开展一个项目,需使用Pyplot在地图图像上绘制路线。目前已能在地图图像的正确位置绘制路线,但保存后的图像尺寸与原始图像不一致:原始图像为1024×1024像素,输出图像却为1488×1108像素。请问如何让输出图像保持1024像素的原始尺寸?
基础图像:
结果图像:
当前代码:
import matplotlib.pyplot as plt lng = [-111.63316414564714, -111.63314545953119, -111.63318, -111.63340938396135, -111.63342133145254, -111.63343, -111.63343, -111.63343, -111.63356, -111.63359, -111.63351, -111.6335, -111.63363, -111.63383, -111.63407, -111.6343, -111.63445, -111.63445, -111.63445, -111.63466, -111.6348, -111.63483, -111.63463, -111.63452, -111.6344, -111.63431, -111.63433, -111.63451, -111.634511589384, -111.63451798003926, -111.63462186123186, -111.63464381112912, -111.63473, -111.63492, -111.63525, -111.63581, -111.63608, -111.63621, -111.63651, -111.63677, -111.6369210836072, -111.63693683084689, -111.63725, -111.63738, -111.63738, -111.6373, -111.63733, -111.63774, -111.63786, -111.63802, -111.63852, -111.63856, -111.63858, -111.63841, -111.63844, -111.63847, -111.63858, -111.63864458445171, -111.6386570361554, -111.63883, -111.63885, -111.63883, -111.63875, -111.63867976774262, -111.63867976774262, -111.63859, -111.63824000072073, -111.63824000072073, -111.63824, -111.63813, -111.63796, -111.63714, -111.63651, -111.63635, -111.63614, -111.63569, -111.63549, -111.63537, -111.63524, -111.63516, -111.63507, -111.63495, -111.63477, -111.63477, -111.63477, -111.63465, -111.63444, -111.63429, -111.63414, -111.634, -111.63316, -111.63236, -111.63221, -111.63205, -111.63178913223254, -111.63178913223254, -111.63142, -111.6312, -111.63105, -111.63092, -111.63048, -111.63032, -111.63009, -111.6295, -111.6293, -111.62913, -111.62901, -111.62896366370573, -111.6289653442638, -111.62895, -111.62902, -111.62915, -111.62929, -111.62962, -111.62966, -111.62959, -111.62958, -111.6296, -111.62995, -111.63017, -111.63036, -111.63058, -111.63117, -111.63125, -111.63134, -111.63199, -111.63227, -111.63251, -111.63251525425726, -111.63253704928997, -111.63253725221409, -111.63256, -111.63256, -111.63267, -111.63272, -111.63272, -111.63272, -111.63271, -111.63271, -111.63271, -111.63271, -111.63269447235307, -111.63269, -111.63276795513357] lat = [35.21775843704708, 35.21778026259171, 35.2178, 35.21827711863958, 35.21830196947703, 35.21832, 35.21832, 35.21832, 35.21844, 35.21862, 35.21892, 35.21917, 35.21931, 35.21936, 35.21931, 35.21914, 35.21908, 35.21908, 35.21908, 35.21912, 35.21927, 35.21945, 35.21992, 35.22016, 35.22024, 35.2204, 35.22057, 35.2208, 35.22080134486337, 35.220806752344565, 35.22089465181158, 35.22090266478892, 35.22091, 35.22088, 35.22063, 35.22032, 35.21994, 35.21987, 35.21981, 35.21982, 35.21989554160251, 35.219905550063096, 35.22016, 35.22045, 35.22063, 35.22126, 35.22152, 35.22207, 35.22241, 35.22263, 35.22308, 35.22314, 35.22319, 35.22364, 35.22387, 35.22397, 35.22414, 35.224220084720116, 35.22423552495892, 35.22445, 35.22453, 35.22469, 35.22476, 35.224768779032175, 35.224768779032175, 35.22478, 35.22474000080831, 35.22474000080831, 35.22474, 35.22471, 35.22461, 35.22428, 35.22363, 35.2235, 35.22339, 35.22319, 35.22307, 35.22297, 35.22272, 35.22246, 35.2224, 35.22236, 35.22238, 35.22238, 35.22238, 35.22246, 35.22268, 35.2229, 35.22299, 35.22304, 35.22313, 35.22318, 35.22316, 35.2231, 35.22295507324607, 35.22295507324607, 35.22275, 35.22269, 35.22271, 35.22276, 35.2231, 35.22317, 35.22324, 35.22357, 35.22359, 35.22355, 35.22346, 35.223344159264315, 35.22334836068595, 35.22331, 35.22316, 35.22302, 35.22281, 35.2219, 35.22168, 35.22132, 35.2211, 35.221, 35.22046, 35.2202, 35.22013, 35.22012, 35.22027, 35.22027, 35.22023, 35.2198, 35.2194, 35.21918, 35.2191806052061, 35.219048797151125, 35.219047570056155, 35.21891, 35.21891, 35.2185, 35.21822, 35.21822, 35.21822, 35.21806, 35.21806, 35.21799, 35.21799, 35.217399949416404, 35.2174, 35.21743340934296] bbox = (-111.64081951, -111.62775449, 35.21526317, 35.22593648) img = plt.imread('default_outdoors-v12_z15.75_1024px.png') fig, ax = plt.subplots() ax.plot(lng, lat) ax.set_xlim(bbox[0], bbox[1]) ax.set_ylim(bbox[2], bbox[3]) plt.axis('off') ax.imshow(img, zorder=0, extent=bbox, aspect='auto') plt.savefig('derp.png', bbox_inches='tight', pad_inches=0, dpi=300)
解决方法
问题出在Matplotlib默认会根据画布和DPI计算输出尺寸,加上bbox_inches='tight'会自动调整画布适配内容,导致尺寸偏离原始图像。要保持1024×1024的尺寸,需要从以下几点调整:
1. 匹配原始图像的画布尺寸
在创建子图时,根据原始图像的像素和目标DPI计算画布的英寸大小,确保画布尺寸和原始图像像素对应。
2. 移除bbox_inches='tight'
该参数会自动裁剪画布以适配内容,直接移除即可避免尺寸变化。
3. 完整修改后的代码
import matplotlib.pyplot as plt lng = [-111.63316414564714, -111.63314545953119, -111.63318, -111.63340938396135, -111.63342133145254, -111.63343, -111.63343, -111.63343, -111.63
相关产品推荐
相关产品推荐

