so, I have been trying to plot two signals wrt a time axis that i created. when i plot the values against the signal_1 and signal_2, by specifically mentioning the time axis t, i.e, plt.plot(t,signal_1) i'm getting a fine plot with correct time axis. But when I try to do the same with both signals, with giving command sharex = True, then I'm getting a wrong time axis. I understand the problem goes away once i disable sharex =True, but why isn't sharex working. that is my question.
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
#create time array
t0 = datetime(1970,1,1)
hours = (1/1) * np.arange(7*24)
t = Tide._times(t0,hours)
'''
create fake amplitude, phase and period
'''
amp = [4,6]
phase = [250,140]
period = [4,12]
'''
generate a time array for two signal
'''
signal = [[] for i in range(2)]
for i in range(2):
for j in range(len(t)):
e = amp[i] * np.sin((2*np.pi/period[i])*j + (np.pi/180)*(phase[i]))
signal[i].append(e)
fig, (ax0,ax1) = plt.subplots(nrows=2,ncols=1,sharex=True)
ax0.plot(signal[0])
ax1.plot(t,signal[1])
plt.show()
To share an axis, both plots need to have comparable axes, within the same range (-ish).
In your example you only pass your X values to your 2nd plot. The first will get a standard integer X value (0...N) for each item in signal[0]
. Unless your t
values are also in this range, the two plots will not be drawn over one another.
If they are equivalent, you will want to do the following --
ax0.plot(t,signal[0])
ax1.plot(t,signal[1])
If not, then you will need to modify one of the axes so they are comparable (the correct way to do this depends on your data).