我正在运行一个简单的脚本,在一个绘图上绘制两种不同的数据类型,并且收到此错误(下面的完整错误文本)。我怀疑它与索引此行上的列表有关,但还没弄清楚为什么
`modelresp1 *= -f1.attrs['dx, dy, dz'][1]`
此外,我已经运行了好几天,直到今天下午才有这个问题。 HDF5文件是相同的,上面显示的行是相同的。具体是什么导致了这个错误,可以做些什么改变来避免它?我使用的是Python 3.5.2,Scipy 0.18.1,Numpy 1.11.2
感谢!!!
错误:
C:\MHD_Models\MHDsimple_plot.py:82: VisibleDeprecationWarning: using a non-integer number instead of
an integer will result in an error in the future
modelresp1 *= -f1.attrs['dx, dy, dz'][1]
Traceback (most recent call last):
File "C:\Anaconda3\lib\runpy.py", line 174, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "C:\Anaconda3\lib\runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "C:\MHD_Models\MHDsimple_plot.py", line 120, in <module>
ax1.plot(modeltime1, modelresp1, 'g-', label='Modeled Data')
File "C:\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1818, in inner
return func(ax, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1382, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 381, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 359, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 219, in _xy_from_xy
raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
这是脚本
import os
import sys
import h5py
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
#datafile 1 is the MODEL data file
datafile1 = "C:\MHD_Models\GSSI_400MHz_Opt_final6.out"
#define what output field to use from the modeled data
outputs = "Ey"
#datafile 2 is the measured REAL data file
datafile2 = "C:\MHD_Models\GSSI_400MHz_realprn(Amplitude).txt"
#REAL DATA
# open and read/load measured REAL data file row by row.
with open(datafile2, 'r') as f2:
refdata2 = np.loadtxt(f2)
# set reftime and refresp as the arrays of time and amplitude values in the reference file, respectively
reftime2 = refdata2[:,0] * 1e-9
refresp2 = refdata2[:,1]
#MODEL DATA
# open and read/load model data.
f1 = h5py.File(datafile1, 'r')
nrx = f1.attrs['nrx']
dt = f1.attrs['dt']
iterations = f1.attrs['Iterations']
# Check that there are any receivers
if nrx == 0:
raise CmdInputError('No receivers found in {}'.format(datafile1))
# set modeltime as the array of time values in the model file
modeltime1 = np.linspace(0, iterations * dt, iterations)
# set modelresp as the array of amplitude values in the model file
for rx in range(1, nrx + 1):
outputpath = f1['/rxs/rx' + str(rx) + '/']
outputname = list(outputpath.keys())[0]
modelresp1 = outputpath[outputname]
# Convert electric field value (V/m) to voltage (V)
if outputname == 'Ex':
modelresp1 *= -f1.attrs['dx, dy, dz'][0]
elif outputname == 'Ey':
modelresp1 *= -f1.attrs['dx, dy, dz'][1]
elif outputname == 'Ez':
modelresp1 *= -f1.attrs['dx, dy, dz'][2]
# DATA fitting
#Manage Data sets to plot on same instance
#Normalise reference respose and response from output file
refresp2 /= np.amax(np.abs(refresp2))
modelresp1 /= np.amax(np.abs(modelresp1))
#Make both responses the same length in time
if reftime2[-1] > modeltime1[-1]:
reftime2 = np.arange(0, dt * iterations, reftime2[-1] / len(reftime2))
refresp2 = refresp2[0:len(reftime2)]
elif modeltime1[-1] > reftime2[-1]:
modeltime1 = np.arange(0, reftime2[-1], f1.attrs['dt'])
modelresp1 = modelresp1[0:len(modeltime1)]
#Downsample the response with the higher sampling rate
if len(modeltime1) < len(reftime2):
refresp2 = signal.resample(refresp2, len(modelresp1))
elif len(reftime2) < len(modeltime1):
modelresp1 = signal.resample(modelresp1, len(refresp2))
# PLOTTING
#create figure and plot
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_title("A-scan Comparison")
ax1.set_xlabel("Time (ns)")
ax1.set_ylabel("Normalized Amplitude")
ax1.plot(reftime2, refresp2, 'r-', label='Real Data')
ax1.plot(modeltime1, modelresp1, 'g-', label='Modeled Data')
leg = ax1.legend()
plt.show()