How to shift a graph along the X-axis in matplotlib?

To shift a graph along the X-axis in matplotlib, you need to modify the x-coordinates of your data points. This technique is useful for comparing different versions of the same data or creating animations.

Basic X-axis Shifting

The simplest way to shift a graph is to add a constant value to all x-coordinates ?

import numpy as np
import matplotlib.pyplot as plt

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

# Create x and y data points
x = np.linspace(-5, 5, 100)
y = np.sin(x)

# Plot original and shifted graphs
plt.plot(x, y, label='Original Graph', color='blue')
plt.plot(x + 3, y, label='Shifted Right by 3', color='red')
plt.plot(x - 2, y, label='Shifted Left by 2', color='green')

# Add labels and legend
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend(loc='upper right')
plt.grid(True, alpha=0.3)

plt.show()
[A graph showing three sine curves - the original in blue at x=0, one shifted right by 3 units in red, and one shifted left by 2 units in green]

Using Range for Index-based Shifting

You can also shift by changing the x-axis to use different index ranges ?

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Generate data
x = np.linspace(0, 10, 50)
y = np.cos(x)

# Original graph using x values
plt.plot(x, y, label='Original Graph', linewidth=2)

# Shifted graph using range starting from different point
plt.plot(range(15, 15 + len(y)), y, label='Shifted Graph', linewidth=2)

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend(loc='upper right')
plt.grid(True, alpha=0.3)

plt.show()
[A graph showing two cosine curves - the original starting at x=0 and a shifted version starting at x=15]

Multiple Shift Amounts

Here's an example showing multiple shifts of the same function ?

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [8.00, 4.00]
plt.rcParams["figure.autolayout"] = True

# Create data
x = np.linspace(0, 2*np.pi, 100)
y = np.exp(-x/3) * np.sin(x)

# Plot with different shifts
shifts = [0, 2, 4, 6]
colors = ['blue', 'red', 'green', 'orange']

for shift, color in zip(shifts, colors):
    plt.plot(x + shift, y, label=f'Shift = {shift}', color=color, linewidth=2)

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Graph Shifting Along X-axis')
plt.legend()
plt.grid(True, alpha=0.3)

plt.show()
[A graph showing four curves of a damped sine function, each shifted by 0, 2, 4, and 6 units respectively along the x-axis]

Key Points

  • Direct Addition: Add a constant to x-coordinates: plt.plot(x + shift, y)
  • Range Method: Use different starting indices: range(start, start + len(y))
  • Negative Shifts: Use negative values to shift left: x - 2
  • Multiple Plots: Combine original and shifted graphs for comparison

Conclusion

Shifting graphs along the X-axis in matplotlib is achieved by adding constants to x-coordinates or using different index ranges. This technique is essential for data comparison and visualization of time-series data with different starting points.

Updated on: 2026-03-26T02:35:19+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements