How do I remove the Y-axis from a Pylab-generated picture?

To remove the Y-axis from a Pylab-generated picture, we can get the current axis of the plot and use the set_visible(False) method on the Y-axis.

Steps

  • Set the figure size and adjust the padding between and around the subplots.

  • Create x and y data points using numpy.

  • Plot the x and y data points using plot() method.

  • Get the current axis of the current figure.

  • Set the visibility to False for the Y-axis.

  • To display the figure, use show() method.

Example

Here's how to remove the Y-axis from a plot ?

import numpy as np
import pylab

# Set the figure size
pylab.rcParams["figure.figsize"] = [7.50, 3.50]
pylab.rcParams["figure.autolayout"] = True

# Random data points
x = np.random.rand(10)
y = np.random.rand(10)

# Plot the data points
pylab.plot(x, y)

# Get the current axis
ax = pylab.gca()

# Set Y-axis visibility to False
ax.yaxis.set_visible(False)

# Display the plot
pylab.show()

Alternative Method Using Matplotlib

You can also achieve the same result using matplotlib directly ?

import numpy as np
import matplotlib.pyplot as plt

# Create data points
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create the plot
plt.figure(figsize=(8, 4))
plt.plot(x, y, 'b-', linewidth=2)

# Remove Y-axis
plt.gca().yaxis.set_visible(False)

# Add title and labels
plt.title('Plot with Y-axis Removed')
plt.xlabel('X Values')

plt.show()

Additional Y-axis Removal Options

Here are different ways to hide or modify the Y-axis ?

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 5, 50)
y = x**2

fig, axes = plt.subplots(2, 2, figsize=(10, 8))
fig.suptitle('Different Y-axis Removal Methods')

# Method 1: Hide entire Y-axis
axes[0,0].plot(x, y)
axes[0,0].yaxis.set_visible(False)
axes[0,0].set_title('Entire Y-axis Hidden')

# Method 2: Hide Y-axis labels only
axes[0,1].plot(x, y)
axes[0,1].set_yticklabels([])
axes[0,1].set_title('Y-axis Labels Hidden')

# Method 3: Hide Y-axis ticks only
axes[1,0].plot(x, y)
axes[1,0].tick_params(left=False, labelleft=False)
axes[1,0].set_title('Y-axis Ticks Hidden')

# Method 4: Normal plot for comparison
axes[1,1].plot(x, y)
axes[1,1].set_title('Normal Plot')

plt.tight_layout()
plt.show()

Comparison

Method Code Effect
yaxis.set_visible(False) ax.yaxis.set_visible(False) Hides entire Y-axis including labels and ticks
set_yticklabels([]) ax.set_yticklabels([]) Hides only Y-axis labels, keeps tick marks
tick_params() ax.tick_params(left=False, labelleft=False) Hides Y-axis ticks and labels separately

Conclusion

Use ax.yaxis.set_visible(False) to completely remove the Y-axis from your Pylab or Matplotlib plots. This method hides both the axis line, tick marks, and labels, creating a cleaner visualization when Y-axis information is not needed.

Updated on: 2026-03-26T19:06:01+05:30

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements