How to Remove Matplotlib Figure Frame Without Losing Axes Tick Labels

πŸ’‘ Problem Formulation: When visualizing data using Matplotlib in Python, you might want to create a plot that has a clean, minimalistic look by removing the figure frame, while still retaining the axes tick labels for interpretation. The challenge is to eliminate the box-like frame or border around the plot without affecting the visibility of the tick labels that are vital for data comprehension.

Method 1: Spine Visibility

Altering the visibility of the spines in a Matplotlib figure can remove the frame while keeping the axes tick labels. Spines are the lines connecting the axis tick marks and noting the boundaries of the data area. By setting their visibility to False, we can achieve the desired effect.

Here’s an example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])

# Removing the frame while maintaining the tick labels
for spine in ax.spines.values():
    spine.set_visible(False)

plt.show()

Output: A plot without the frame, but with the axes tick labels remaining visible.

This code first imports Matplotlib’s pyplot and creates a basic figure with the subplots method. It then plots a simple line. By iterating through the spines of the axes object and setting their visibility to False, the bordering lines around the plot are removed while the axes and tick labels remain visible.

Method 2: Hide Spines Individually

For more granular control, you can remove specific spines while leaving others intact. This is useful if you only want to remove certain sides of the frame, for instance, the top and right spines, which is a common stylistic choice to make the plot look cleaner.

Here’s an example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])

# Remove the top and right spines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

plt.show()

Output: A plot with only the bottom and left spines (frame edges) visible, retaining all axes tick labels.

The code takes a targeted approach to frame removal by singularly setting the visibility of the top and right spines to False. This is especially useful when a full frame is unnecessary and may visually burden the plot. It maintains the bottom and left spines for aesthetic balance, along with the axes tick labels.

Method 3: Subplot Adjustments

This method leverages the subplot_adjust function of pyplot, which adjusts the parameters of the subplot, such as the spacing around it. Although less direct, it can be effective for producing figures without frames and is particularly useful when dealing with multiple subplots.

Here’s an example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])

# Adjusting subplot parameters
plt.subplots_adjust(left=0, right=1, top=1, bottom=0)

# Remove all spines
for spine in ax.spines.values():
    spine.set_visible(False)

plt.show()

Output: A tightly-fitted plot with no visible frame, displaying all axes tick labels.

After creating a plot, this snippet uses subplots_adjust to alter the subplot’s layout, making sure there’s no extra white space around the plot, which could suggest the existence of a frame. Spines are then hidden to eliminate the frame. This method is particularly useful when a tight layout is required or when dealing with complex subplot arrangements.

Method 4: Using Patch Visibility

This method hides the entire axes background, including the frame. It involves setting the patch (the background and frame of the axis) visibility to False. This is a blunt approach and may not always be suitable when only the frame needs to be removed, but it’s a quick method when the axes’s background can be hidden altogether.

Here’s an example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])

# Hide the background patch (which includes the frame)
ax.patch.set_visible(False)

plt.show()

Output: A plot displaying axes tick labels on what appears to be a transparent background, without any frame or extra background visible.

After initiating a Matplotlib figure, this snippet sets the axes patch visibility to False which, in addition to removing the frame, also makes the plot background transparent. While this removes the frame, it’s worth noting that it also affects the plot background, which may be undesirable in some contexts.

Bonus One-Liner Method 5: Axis Off

For absolute simplicity, using the axis('off') function is the brute-force method to remove all axis lines, tick labels, and frames. However, we can pair this with additional commands to bring back the tick labels while keeping the axis frame off. It’s not as modular as other methods but can be handy in simplified cases.

Here’s an example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])

# Turn off axis lines and labels
ax.axis('off')

# Manually adding tick labels
ax.set_xticks([0, 0.5, 1])
ax.set_yticks([0, 0.5, 1])

plt.show()

Output: A plot with custom set tick labels but no axes frame.

Here, the axis('off') command is used to turn off the axis lines and labels. The tick labels are then manually added back to the plot. While this method effectively removes the frame, it requires additional steps to manually manage the tick labels and is therefore less convenient if the default labels were satisfactory.

Summary/Discussion

  • Method 1: Spine Visibility. Provides precise control over all four spines individually. Can remove just the frame without influencing other plot elements. May require additional lines of code to adjust each spine separately.
  • Method 2: Hide Spines Individually. Offers a balance of control and simplicity, allowing the user to selectively display desired spines. Especially suitable for standard plots where just the top and right frames are to be removed.
  • Method 3: Subplot Adjustments. Most useful in multi-plot scenarios for eliminating the frame, as it can adjust the spacing around the subplots. It is less about frame removal and more about subplot management.
  • Method 4: Using Patch Visibility. A more expansive method that not only removes the frame but also the background. Ideal for a completely minimalist plot, yet may be too radical if only the frame removal is intended.
  • Method 5: Axis Off. The simplest, but also the most drastic method, requiring manual addition of tick labels after turning off the axis. Can be used for quick, ad hoc plots but lacks the nuance and flexibility of the other methods.