Search code examples
pythonsubplotx-axis

Subplots in Python with x axis having too large of a jump between values


import matplotlib.pyplot as plt
import numpy as np

delta = 0.0001

t = np.arange(0,5+delta,delta)

xt = np.sin(np.pi*t)

fig = plt.figure(1)

ax1= plt.subplot(3,2,1)
ax1.plot(t,xt, "tab:red")
ax1.set(ylabel = "Amplitude")
ax1.set(xlabel = 'Time(s)')
ax1.set(title = 'for n = 1')
ax1.grid()

ax2 = plt.subplot(3,2,2)
ax2.plot(t,xt, "tab:green")
ax2.set(ylabel = "Amplitude")
ax2.set(xlabel = 'Time(s)')
ax2.set(title = 'for n = 2')
ax2.grid()


plt.tight_layout()


plt.show()

Hi this is just a snip of my code but my problem basically is with the x axis of the subplots. On the axis the values jump from 0-2-4 and I need it to be from 0-1-2-3-4-5. Is there a way I can get those values to display on the x axis rather than just 0-2-4.


Solution

  • There are several possible ways of doing this. One of the simplest is to manually set the x ticks.

    ax1.set_xticks(np.arange(6))
    ax2.set_xticks(np.arange(6))