简体   繁体   English

matplotlib:savefig和show之间不一致

[英]matplotlib: inconsistency between savefig and show

I have a main fig and two insets (see eg code below). 我有一个主图和两个插图(请参见下面的代码)。 Why the outcome of savefig is not the same as show() ? 为什么savefig的结果与show()不同? how can I get exactly the same with savefig? 我怎样才能与savefig完全一样? I have put the label size and fonts larger than usual because I have to insert the figure in a two-column article and with smaller size, they are not very easy to read. 我将标签的大小和字体设置为比平常大,因为我必须将图形插入到两栏文章中,并且尺寸较小,因此它们不太容易阅读。 In my real data, the inset figures (a bit similar to here) are shrunk substantially with savefig. 在我的真实数据中,插图(有点类似于此处)被savefig缩小了。

Thanks in advance for your help! 在此先感谢您的帮助!

from numpy import *
from pylab import *
import matplotlib.pyplot as plt

# main fig
arr = arange(0.0, 120, 5)
fig = plt.figure()
ax= fig.add_subplot ( 111)
ax.set_xlabel('x test label',  fontsize = 40)
ax.set_ylabel('y test label', fontsize = 40)

plot(arr,arr,'bo-',lw=2,markersize=20,label="test ")
plt.xlim(0,)
plt.tick_params(labelsize=50)
plt.legend(loc='upper left',numpoints=1,bbox_to_anchor=[0.07, 0.95],)

### inset fig
ax = axes([.2, .5, .2, .2], axisbg='y')

data = np.clip(randn(20, 20), -1, 1)

cax = ax.imshow(data, interpolation='nearest', cmap=cm.coolwarm)
ax.set_title('random 1',fontsize=32)
ax.set_xlabel('i', fontsize = 32)
ax.set_ylabel('j', fontsize = 32)

### inset fig
ax = axes([.6, .2, .2, .2], axisbg='y')
data = np.clip(randn(20, 20), -1, 1)
cax = ax.imshow(data, interpolation='nearest', cmap=cm.coolwarm)
ax.set_title('random 2', fontsize=32)

ax.set_xlabel('i', fontsize = 32)
ax.set_ylabel('j', fontsize = 32)

setp(ax, xticks=[], yticks=[])

# arrow
ax.annotate('', xy=(0, 0), xytext=(-10,-5 ),size=20, 
            arrowprops=dict(facecolor='black', shrink=0.02),
            )

plt.savefig('test.pdf', format='pdf', dpi=100)
plt.show()

Unfortunately, the exact (down to the pixel level) results depend on the backend used. 不幸的是,确切的结果(下降到像素级别)取决于所使用的后端。 If you plot the same images with different backends, you will get different results, as the backends have some freedom when it comes to the smallest details. 如果使用不同的后端绘制相同的图像,则会得到不同的结果,因为在涉及最小的细节时,后端具有一定的自由度。 At least with my setup the fonts are slightly different when drawn with the display backend and saved to the disk. 至少在我的设置中,使用显示后端绘制并保存到磁盘时,字体略有不同。

You may try this quite easily by saving the same image as a PNG and as a PDF. 您可以通过将同一图像另存为PNG和PDF来轻松尝试。 The results are very close to each other, but they are not exactly the same (ie rasterizing the PDF will produce different results). 结果彼此非常接近,但是它们并不完全相同(即光栅化PDF将产生不同的结果)。

So, as Adobe suggests, you should do the smallest fine tuning with the backend you intend to use. 因此,正如Adobe建议的那样,您应该对打算使用的后端进行最小的微调。

If you use raster output, then you might be able to use Agg backends for both viewing and saving, and the results should be very close to each other, I suppose. 如果您使用栅格输出,那么您也许可以使用Agg后端进行查看和保存,并且我认为结果应该非常接近。 If you use vector graphics (as you do with PDF), then you might try using Cairo for both ( GTKCairo for interactive). 如果您使用矢量图形(如使用PDF一样),则可能会尝试同时使用Cairo( GTKCairo用于交互式)。

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

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