Reputation: 919
I have a main fig and two insets (see e.g code below). Why the outcome of savefig is not the same as show() ? how can I get exactly the same with 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.
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()
Upvotes: 1
Views: 1071
Reputation: 23500
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. The results are very close to each other, but they are not exactly the same (i.e. rasterizing the PDF will produce different results).
So, as Adobe
suggests, you should do the smallest fine tuning with the backend you intend to use.
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. If you use vector graphics (as you do with PDF), then you might try using Cairo for both (GTKCairo
for interactive).
Upvotes: 1