Home > Article > Backend Development > How to Remove Excess White Space Around Saved Images in Matplotlib?
Problem:
When saving an image after processing with Matplotlib, encountering excessive white space around the saved image, despite attempting the tight option.
Investigation:
Initially, the issue was observed when displaying a basic figure containing an imported image. However, upon adding a NetworkX graph to the figure, white space appeared around the saved image.
Solution:
Specifying bbox_inches="tight" while saving the figure eliminates the white space padding:
plt.savefig("test.png", bbox_inches='tight')
Explanation:
The bbox_inches argument controls the bounding box of the saved image in inches. Setting it to 'tight' instructs Matplotlib to automatically adjust the bounding box to tightly fit the contents of the figure, eliminating unnecessary white space.
Related Topics:
The above is the detailed content of How to Remove Excess White Space Around Saved Images in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!