Python Forum
Value Error when Trying to Plot Filtered Waveforms - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Value Error when Trying to Plot Filtered Waveforms (/thread-42093.html)



Value Error when Trying to Plot Filtered Waveforms - mkohler - May-09-2024

Howdy yall. I am trying to apply a highpass filter to my waveform from a specific earthquake but I cant seem to understand the error that I get when it says that "x and y must have same first dimension." Could anyone explain to me what I'm doing wrong and how I can try to fix it? Here's my code.


import numpy as np
import matplotlib.pyplot as plt

import obspy


# Read the seismogram
#st = obspy.read(r"C:\Users\maddi\Spring Catch-Up\Waveform mseed\Spring Catch-UpGroundMotionM7.0Raw.mseed")
st = client.get_waveforms("II","ERM","00","BHZ", UTCDateTime('2008-07-19 02:39:47'), UTCDateTime('2008-07-19 02:46:00'))

# There is only one trace in the Stream object, let's work on that trace...
tr = st[0]

# Filtering with a lowpass on a copy of the original Trace
tr_filt = tr.copy()
tr_filt.filter('highpass', freq=1.0, corners=2, zerophase=True)

# Now let's plot the raw and filtered data...
t = np.arange(0, tr.stats.npts / tr.stats.sampling_rate, tr.stats.delta)
plt.subplot(211)
plt.plot(t, tr.data)
plt.ylabel('Raw Data')
plt.subplot(212)
plt.plot(t, tr_filt.data, 'k')
plt.ylabel('highpassed Data')
plt.xlabel('Time [s]')
plt.suptitle(tr.stats.starttime)
plt.show()
Error:
ValueError Traceback (most recent call last) Cell In[81], line 21 19 t = np.arange(0, tr.stats.npts / tr.stats.sampling_rate, tr.stats.delta) 20 plt.subplot(211) ---> 21 plt.plot(t, tr.data) 22 plt.ylabel('Raw Data') 23 plt.subplot(212) File ~\anaconda3\envs\obspy\lib\site-packages\matplotlib\pyplot.py:3590, in plot(scalex, scaley, data, *args, **kwargs) 3582 @_copy_docstring_and_deprecators(Axes.plot) 3583 def plot( 3584 *args: float | ArrayLike | str, (...) 3588 **kwargs, 3589 ) -> list[Line2D]: -> 3590 return gca().plot( 3591 *args, 3592 scalex=scalex, 3593 scaley=scaley, 3594 **({"data": data} if data is not None else {}), 3595 **kwargs, 3596 ) File ~\anaconda3\envs\obspy\lib\site-packages\matplotlib\axes\_axes.py:1724, in Axes.plot(self, scalex, scaley, data, *args, **kwargs) 1481 """ 1482 Plot y versus x as lines and/or markers. 1483 (...) 1721 (``'green'``) or hex strings (``'#008000'``). 1722 """ 1723 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D) -> 1724 lines = [*self._get_lines(self, *args, data=data, **kwargs)] 1725 for line in lines: 1726 self.add_line(line) File ~\anaconda3\envs\obspy\lib\site-packages\matplotlib\axes\_base.py:303, in _process_plot_var_args.__call__(self, axes, data, *args, **kwargs) 301 this += args[0], 302 args = args[1:] --> 303 yield from self._plot_args( 304 axes, this, kwargs, ambiguous_fmt_datakey=ambiguous_fmt_datakey) File ~\anaconda3\envs\obspy\lib\site-packages\matplotlib\axes\_base.py:499, in _process_plot_var_args._plot_args(self, axes, tup, kwargs, return_kwargs, ambiguous_fmt_datakey) 496 axes.yaxis.update_units(y) 498 if x.shape[0] != y.shape[0]: --> 499 raise ValueError(f"x and y must have same first dimension, but " 500 f"have shapes {x.shape} and {y.shape}") 501 if x.ndim > 2 or y.ndim > 2: 502 raise ValueError(f"x and y can be no greater than 2D, but have " 503 f"shapes {x.shape} and {y.shape}") ValueError: x and y must have same first dimension, but have shapes (7461,) and (7460,)



RE: Value Error when Trying to Plot Filtered Waveforms - deanhystad - May-09-2024

The error message is pretty clear.
Error:
ValueError: x and y must have same first dimension, but have shapes (7461,) and (7460,)
The line that raises this error is.
plt.plot(t, tr.data)
So it looks like t and tr.data are not the same size. That points to this line being wrong.
t = np.arange(0, tr.stats.npts / tr.stats.sampling_rate, tr.stats.delta)
I would expect this to be:
t = np.arange(0, tr.stats.npts * tr.stats.delta, tr.stats.delta)
Print out the values for tr.stats.delta and tr.stats.sampling_rate. I don't think tr.stats.delta * tr.stats.sampling_rate == 1. The error is in your data, and a bad assumption in your program. Another, less likely possibility is that tr.stats.npts != len(tr.data).