I tried \setlength{\fboxsep}{1pt} for example but it affects only the horizontal spacing, i.e., the spacing between the text and the box border on the left and right sides, but not the vertical spacing, i.e. top and bottom.
My figure is written as follows: