pandasmatplotlibfigsize

Saved matplotlib size on pdf is larger than original figure


When checking matplotlib figure size is :

fig_width, fig_height = plt.gcf().get_size_inches()
print(fig_width, fig_height)

6.0 4.0 ´<Figure size 3600x2400 with 0 Axes>´

One of the params to create the figure was:

plt.rcParams["figure.dpi"] = 600

I save the figure with the following code:

plt.savefig("figure.pdf", format= 'pdf', bbox_inches="tight",dpi=600)

When checking resulting pdf size I can see it is larger than the original one:

enter image description here

I also tried saving the image without specifying dpi, but result was the same:

plt.savefig("figure.pdf", format= 'pdf', bbox_inches="tight")

I read this page, but could not find the answer.

How should I save the matploblig figure so that resulting pdf is desired size 6*4 inches and 600 dpi resolution?


Solution

  • Adding the following line before plt.savefig() solved the issue:

    plt.gcf().set_size_inches(6, 4)