maybe it will be a very basic question for the experts but I am just a beginner. I have multiple time series signals from different distances. I plotted all time series data with the help of the plt.plot command as given in the below script.
bp_range=[0.5,20]
dir='/data'
# set plots
fig = plt.figure(figsize=(12,14))
grid = plt.GridSpec(2, 6)
ax1 = plt.subplot2grid((2,6), (0,0),colspan=3,rowspan=2)
ax2 = plt.subplot2grid((2,6), (0,3),rowspan=2,colspan=3,sharey=ax1)
#......get relevant information.........
for event_file in dep_files:
ref_file=" ".join(str(x) for x in event_file)
st = obspy.read(ref_file)
P=st[0].stats.sac.a
S=st[0].stats.sac.t0
evdp=st[0].stats.sac.evdp
t = st[0].stats.starttime
npts = st[0].stats.npts
dt = st[0].stats.delta
fNy = 1. / (2. * dt)
time = np.arange(0, npts) * dt
freq = np.linspace(0, fNy, npts // 2 1)
corners = 4
st_fft = np.fft.rfft(st[0].data)
## normalize the data...........
if wf_normalize:
st[0]=st[0].slice(t S-1,t S 3)
data = st[0].data.copy()
if max(data) != min(data):
st[0].data=data/(max(data) - min(data)) # Normalize data
sampling_rate = st[0].stats.sampling_rate
if max(data) != min(data):
st_fft=abs(st_fft)/(max(st_fft) - min(st_fft)) # Normalize data
#plot the data
ax1.plot(np.arange(0,len(st[0].data))*1/sampling_rate,
st[0].data evdp,
color='k',
linewidth=0.7)
ax2.plot(freq,abs(st_fft)*0.1 evdp,
color='k',
linewidth=0.7)
plt.tight_layout()
I got this plot Similar like this. Now I want to plot a time series signal in pcolormesh with respect to its normalized amplitude as Figure 4 plotted in this paper Paper . But I always get some errors such as not enough values to unpack (expected 2,got 1).
Here is my tried, even if it is not so good as a beginner of Python. Or is there any other suggested way to do this?
ylim = [0, 10]
xarray, yarray, zarray = np.array([]), np.array([]), np.array([])
if xarray.size == 0:
mask = np.where((evdp>=ylim[0]) & (evdp<=ylim[-1]))[0]
zarray = data.copy()[mask]
yarray = np.append(yarray,evdp)
xarray = np.append(np.arange(0,len(st[0].data))*1/sampling_rate)
im = ax.pcolormesh(xarray, yarray, zarray.T, cmap='plasma', shading='gouraud')
CodePudding user response:
Do not worry about being a beginner, good that you ask questions. It will help others in similar situation as you.
Regarding your question, it would be really helpful with a minimum working example so that we can investigate our selves. Which version of matplotlib are you running (matplotlib.__version__
)? What shapes do xarray
, yarray
, and zarray
have?
I can reproduce your error with a modified example from the matplotlib docs gallery (here https://matplotlib.org/stable/gallery/images_contours_and_fields/pcolormesh_levels.html). If I run
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(19680801)
Z = np.random.rand(6, 10)
x = np.arange(-0.5, 10, 1) # len = 11
y = np.arange(4.5, 11, 1) # len = 7
fig, ax = plt.subplots()
ax.pcolormesh(x, y, Z.flatten() )
I get ValueError: not enough values to unpack (expected 2, got 1)
For it to work I need to change the last line to ax.pcolormesh(x, y, Z)
So the problem is most likely that your zarray is not the right shape. I believe it needs to be a (Ny, Nx) sized matrix, where Ny is the length of the yarray, and Nx the length of the xarray. Perhaps run the example, and check the shape and size of the x,y, Z array and compare to yours.
(If you did not mess with rcParams, your shading parameter of pcolormesh should be set to 'auto' and should not matter if you give the quadrilateral, square in your case, edges or the center coordinates for x and y)