【发布时间】:2020-06-17 06:10:15
【问题描述】:
我计算了一个东西,结果有一些“NaN”值,如果我想用np.nan_to_num 将它们替换为零,那么图形就会出错。我怎样才能不绘制整个列表?我的意思是如何在列表中绘制值,而不是像我在这篇文章中附加的图像那样绘制“NaN”。我尝试过但未成功的代码如下:
import os
import numpy as np
import matplotlib.pyplot as plt
import pylab
import matplotlib as mpl
import pandas as pd
from matplotlib import cm
from matplotlib import rcParams
from scipy.signal import find_peaks
from scipy.signal import argrelextrema
import seaborn as sns
from statsmodels.tsa.stattools import adfuller
sns.set(style="darkgrid")
%matplotlib qt
CASES = [f for f in sorted(os.listdir('.')) if f.startswith('config')]
maxnum = np.max([int(os.path.splitext(f)[0].split('_')[1]) for f in CASES])
CASES = ['configuration_%d.out' % i for i in range(maxnum)]
def find_threshold(arr, value):
for i, a in enumerate(arr):
if a == value:
break
return i
# The function to find the index of peak points(local maxima and minima)
def find_peak(arr):
indices = []
res = []
for i in range(1,len(arr)-1):
if arr[i] > arr[i-1] and arr[i] > arr[i+1]:
res.append(arr[i])
indices.append(i)
elif arr[i] < arr[i-1] and arr[i] < arr[i+1]:
res.append(arr[i])
indices.append(i)
return indices, res
# The function to find spatial differenc (Using the "s" coordinate)
def find_diff(arr):
res = []
for i in range(1,len(arr)):
res.append(arr[i] - arr[i-1])
return res
# The collection of function into one function
def compute_Lh(theta, spatial):
indices, peaks = find_peak(theta)
selected_spatial = spatial[indices]
diffs = find_diff(selected_spatial)
mean = np.mean(diffs)
return mean
x=[]
y=[]
for i, d in enumerate(CASES):
a = np.loadtxt(d).T
spatial = a[2]
theta = a[3]
curve = a[4]
Bend_appex = max(curve)
threshold = find_threshold(curve, Bend_appex)
[![enter image description here][1]][1]
theta_u = theta[:threshold]
spatial_u = spatial[:threshold]
theta_d = theta[threshold:]
spatial_d = spatial[threshold:]
mean_u = compute_Lh(theta_u, spatial_u)
mean_d = compute_Lh(theta_d, spatial_d)
mean_a = compute_Lh(theta, spatial)
Ah_mean = (mean_u - mean_d) / mean_a
x.append(i)
y.append(Ah_mean)
plt.plot(x,y)
【问题讨论】:
-
@DrBwts 它与我的高度相似,但
fp = A[-np.isnan(A)]存在问题我收到此错误only integer scalar arrays can be converted to a scalar index。顺便说一句,感谢您的推荐
标签: python numpy matplotlib