Python Forum
Value Error when Trying to Plot Filtered Waveforms
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Value Error when Trying to Plot Filtered Waveforms
#1
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,)
Reply
#2
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to capture live running waveforms in an oscilloscope using python? Girishbabu_27 4 1,062 Jul-29-2023, 06:05 AM
Last Post: Girishbabu_27
  Likert survey data plot error Andrzej_Andrzej 6 1,545 Jul-16-2023, 10:11 PM
Last Post: deanhystad
  Error when animating 3D plot Tee 4 1,066 Jul-03-2023, 08:49 PM
Last Post: Tee
  How to plot intraday data of several days in one plot mistermister 3 2,990 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  Filtered Row Count RookToday 1 10,043 Jul-18-2020, 09:13 PM
Last Post: RookToday
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,968 Jun-12-2019, 03:31 PM
Last Post: SriMekala
  scatter plot error - possibly data type - TypeError: nan is not a string chudson 1 6,594 Mar-24-2019, 11:48 AM
Last Post: chudson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020