๐ก Problem Formulation: When plotting an image using Matplotlibโs imshow()
within a subplot, users often notice an unwanted white border around the image. This article discusses ways to eliminate this white space, ensuring that the image fully utilizes the allotted frame size, providing a cleaner and more professional appearance in visualization tasks.
Method 1: Adjusting Figure and Axes objects
This method involves modifying the properties of the Figure and Axes objects directly. By setting the tight_layout
parameters and manually adjusting subplots_adjust
, you can minimize or eliminate white borders with granular control.
Here’s an example:
import matplotlib.pyplot as plt import numpy as np data = np.random.rand(10, 10) fig, ax = plt.subplots() ax.imshow(data, aspect='auto') fig.subplots_adjust(left=0, right=1, bottom=0, top=1) plt.show()
Output: A figure with the image using the full space of the plot area without white borders.
The code above creates a random matrix, which is then visualized as an image. By adjusting the subplots_adjust
parameters to occupy the full range from 0 to 1, we ensure that there are no margins around the image, thus removing the white border.
Method 2: Using the axis โoffโ method
This method is straightforward; it simply turns the axis off, which in some cases may remove the white border. Note that this also removes axis labels and ticks, which might not be suitable for all applications.
Here’s an example:
import matplotlib.pyplot as plt import numpy as np data = np.random.rand(10, 10) fig, ax = plt.subplots() ax.imshow(data) ax.axis('off') plt.show()
Output: An image without axes and white borders.
The example demonstrates how easily one can turn off the axis, which hides all axis-related components and often the white border around the image. It is a very simple yet effective solution for images where axis information is not critical.
Method 3: Setting Aspect to ‘Equal’ and Expanding Axes
By setting the aspect ratio to ‘equal’ and then expanding the axes to fill the entire figure, you can remove the white border while maintaining the aspect ratio of the image.
Here’s an example:
import matplotlib.pyplot as plt import numpy as np data = np.random.rand(10, 10) fig, ax = plt.subplots() ax.imshow(data, aspect='equal') ax.set_position([0, 0, 1, 1]) plt.show()
Output: A figure with the image with maintained aspect ratio but without white borders.
This code snippet begins by plotting random data as an image. The aspect ratio is then set to ‘equal’, and the position of the plot is explicitly defined to cover the full figure area, which removes the white border.
Method 4: Use of Constrained Layout
Matplotlibโs constrained layout system automatically adjusts subplots and decorators (like legends and colorbars) to fit inside the figure window in an aesthetically pleasing way, which can help remove unwanted white space.
Here’s an example:
import matplotlib.pyplot as plt import numpy as np data = np.random.rand(10, 10) fig, ax = plt.subplots(constrained_layout=True) ax.imshow(data) plt.show()
Output: A neatly arranged figure where the image fills the plot area without extra white borders.
This method leverages the constrained layout feature of Matplotlib to handle subplot spacing automatically. When plotting the random data as an image, the constrained layout naturally reduces extra white space without additional manual adjustments.
Bonus One-Liner Method 5: Setting Remove Method
A one-liner occasional fix is to set the remove
method on the Subplot
object, which can remove the white space effectively.
Here’s an example:
import matplotlib.pyplot as plt import numpy as np data = np.random.rand(10, 10) fig, ax = plt.subplots() ax.imshow(data) fig.tight_layout(pad=0) ax.margins(0) ax.remove() ax = fig.add_axes([0, 0, 1, 1]) ax.imshow(data) plt.show()
Output: An image that fills the entire figure area with no white borders.
By initially creating a subplot and then removing it after applying tight_layout
, we can add a new axes object that spans the entire figure, ensuring no white borders around the image when displayed.
Summary/Discussion
- Method 1: Adjusting Figure and Axes objects. Strength: Precise control over layout. Weakness: Requires manual adjustments, may lead to complex code for multiple subplots.
- Method 2: Using the axis โoffโ method. Strength: Quick and easy solution. Weakness: Axis information is lost.
- Method 3: Setting Aspect to ‘Equal’. Strength: Maintains aspect ratio while removing white space. Weakness: Can be less intuitive to get the right position parameters.
- Method 4: Use of Constrained Layout. Strength: Automated layout adjustment. Weakness: Might not work for all plot configurations.
- Method 5: Bonus One-Liner Remove Method. Strength: A novel, quick fix. Weakness: Somewhat of a hack, less conventional.