I want to save a matplotlib figure as a png file with a width/height ratio of 1.25. I specified this ratio via the figsize argument. But when I save the figure using the option bbox_inches = "tight"
then the output png has a size of 553 to 396 pixels which is a ratio of 1.39. I would like to keep the bbox_inches = "tight"
option to prevent unnecessary white space in the figure borders. I tried different approaches suggested in similar posts on stackoverflow but couldn't figure out a solution.
Here is example code:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize = (3, 2.4), dpi = 150)
ax = plt.subplot(111)
for i in range(3):
ax.plot(np.random.random(10), np.random.random(10), "o", label = i)
ax.legend(bbox_to_anchor=(1, 0.6), title = "Title")
plt.ylabel("Label")
plt.xlabel("Label")
plt.title("Title", loc = "left")
plt.savefig("test.png", format = "png", dpi = 150, bbox_inches = "tight")