修正保存图片中的数字标签

问题描述 投票:0回答:1

我想保存一张图片,但保存的图片中没有Xlabels。我试着修改尺寸图,结果成功了,但图像变大了,我想保持图像的大小,并以某种方式显示完整的绘图信息。我想保持图片的大小,并以某种方式显示完整的情节信息。

数据如下。

data = {'neighbourhood': ['Allston-Brighton', 'Jamaica Plain', 'South End', 'Back Bay', 'Fenway/Kenmore', 'South Boston', 'Dorchester', 'Beacon Hill', 'North End', 'East Boston', 'Roxbury', 'Mission Hill', 'Charlestown', 'Chinatown', 'West End', 'Roslindale', 'West Roxbury', 'Theater District', 'Downtown Crossing', 'Hyde Park', 'Mattapan', 'Financial District', 'Somerville', 'Leather District', 'Downtown', 'Brookline', 'Cambridge', 'Chestnut Hill', 'Government Center', 'Harvard Square'], 'latitude': [42.35136861184197, 42.311768379852126, 42.342302581102956, 42.349542747362186, 42.34487223416756, 42.33920029843584, 42.30534371604848, 42.35916242461033, 42.36509251919781, 42.374797033371614, 42.32734681549604, 42.331789902337285, 42.378479634468505, 42.35070490587777, 42.36437167301179, 42.2844848817822, 42.28049169547555, 42.35179317067222, 42.355582834130146, 42.25870128503825, 42.286757258759295, 42.35833367767067, 42.38454210544948, 42.35075084496441, 42.358667722753495, 42.34150237882493, 42.36332342051593, 42.30072382305316, 42.3615193534256, 42.373740039980646], 'longitude': [-71.13980032939199, -71.11023629181607, -71.07406928489343, -71.08014985459491, -71.09639276028551, -71.04673010039227, -71.05980648703859, -71.06722209902804, -71.05435502753697, -71.03063773314375, -71.08533342932748, -71.10338968538669, -71.063939091515, -71.06158442474035, -71.06532504101003, -71.13144270905727, -71.15526404132471, -71.06424189076172, -71.0605365814764, -71.11810073342608, -71.0849389578363, -71.0536411757789, -71.08132624190405, -71.05746407735042, -71.05685306951624, -71.12512826846925, -71.10800606379425, -71.1623118265295, -71.06098895883044, -71.12200210465183], 'dist': [5.217274918890407, 5.117420577499391, 0.9157635862891802, 0.3306405006707467, 1.7610810938591674, 2.726903121754893, 5.1844408677649945, 1.227497247961104, 2.4324110127231613, 4.627284517560583, 2.6651506889743173, 3.0369798718044314, 3.283137336413658, 1.2079270532681, 1.7966687812416413, 8.611569508167042, 10.120811029410335, 1.0015368567582976, 1.4167815087370315, 10.751692511599911, 7.105621710489037, 2.059024017017853, 3.818876664289385, 1.546321543507951, 1.842593010462582, 4.130981378134085, 2.975557190604119, 8.966614609646907, 1.7636863017118727, 4.5639916116404695], 'price': [52.70595169067383, 78.32887268066406, 116.85491943359375, 154.3334503173828, 81.05461883544922, 112.77129364013672, 65.13931274414062, 122.6712646484375, 110.73733520507812, 87.53361511230469, 100.67787170410156, 44.86083984375, 109.29198455810547, 97.78974151611328, 172.12989807128906, 68.67533111572266, 70.07238006591797, 103.89393615722656, 92.42691802978516, 32.06399917602539, 59.09000015258789, 51.484615325927734, 56.128204345703125, 170.1875, 64.60416412353516, 21.53333282470703, 134.6619110107422, 58.25, 262.4222106933594, 0.0], 'count': [364, 314, 298, 291, 249, 216, 195, 174, 125, 117, 116, 103, 79, 78, 68, 50, 35, 33, 26, 25, 20, 13, 13, 8, 8, 8, 7, 4, 3, 2]}

df = pd.DataFrame(data)

dfc_dist_sort = df.sort_values(by=['count'],ascending=False)

我正在使用这段代码。我可以做什么来保存图像,没有头部中断。如果有必要,我可以分享我的图像,只是让我知道。

x = dfc_dist_sort['neighbourhood']
y = dfc_dist_sort['price']
z = dfc_dist_sort['count']
w = dfc_dist_sort['dist']
fig, axs = plt.subplots(3, 1, sharex=True,figsize=(8, 8))
fig.subplots_adjust(hspace=0)

axs[0].set_title('Comparing price, count and distance mean between neighbourhood')
axs[0].bar(x, y)
axs[0].set_ylabel('Price')
axs[1].bar(x, z)
axs[1].set_ylabel('Count')
axs[2].bar(x, w)
axs[2].set_ylabel('Distance (km)')
_ = plt.xticks(rotation=90)
plt.savefig('comp.png', dpi = 300)

谢谢你了。

python matplotlib axis-labels savefig
1个回答
0
投票

在尝试了其他方法后,我发现了这个方法。

plt.savefig('comp2.png', bbox_inches='tight', dpi = 100)

添加param bbox_inches.

© www.soinside.com 2019 - 2024. All rights reserved.