简体   繁体   English

plt.show 和 plt.savefig 给出不同的结果

[英]plt.show and plt.savefig give different result

This is my code, and i get different result from plt.savefig and plt.show这是我的代码,我从 plt.savefig 和 plt.show 得到不同的结果

I have trid to use the bbox_inches='tight', but that makes the picture size become not exactly 700*400.我已经尝试使用 bbox_inches='tight',但这会使图片大小变得不完全是 700*400。

x=np.linspace(1*13,20*13,20)
ys=[[9.429781134779862, 5.29464711177, 3.607607723166614, 2.899937651340496, 2.191804841696895, 1.9843671555485882, 1.7248100121771586, 1.3670895074513212, 1.3698780260605596, 1.2462146230823874, 1.1289892836337199, 1.032841253332204, 0.9543441746831577, 0.7484693801176594, 0.8376893177858197, 0.7849478402160176, 0.7434280956345902, 0.7000987139186625, 0.665212069607791, 0.6300403225806451], [126.98106331402796, 53.50512044002611, 30.074826167504753, 24.227858160427186, 16.471886607532593, 14.13705307473836, 11.449887848348526, 8.72965989245059, 8.840716805318575, 7.263008047412916, 6.293385023002322, 5.634501177610746, 4.6241919224615495, 3.9431399223201433, 3.937845053672828, 3.578854694920532, 3.3610282057487026, 2.947140095251568, 2.629807287721956, 2.5260498894853174]]
plt.figure(figsize=(7,4))
plt.plot(x, ys[0], marker='o',markersize=10,  label='original method')
plt.plot(x, ys[1], marker='s',markersize=10,   label='our method')

plt.xlabel('Number of trajectories(×1000)',fontsize=20)
plt.ylabel("QPS",fontsize=20)  
plt.yscale('log')
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.legend(fontsize=20)
plt.savefig("../res/dense.png",dpi=plt.gcf().dpi)
plt.show()

This is the two pictures I got from savefig() and show()这是我从 savefig() 和 show() 得到的两张图片

savefig 给了我这个

节目给了我这个

You can use:您可以使用:

plt.show() before plt.show()之前

plt.savefig("../res/dense.png",dpi=plt.gcf().dpi)

It's just a matter of convention.这只是一个约定俗成的问题。 When we call plt.savefig the reference of all the variable point till plt.savefig after that any statement it corresponds to new plot.当我们调用plt.savefig时,所有变量点的引用直到plt.savefig之后的任何语句都对应于新的 plot。

You can accept the answer.你可以接受答案。 If it helps.如果有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM