0

I have computed a thing and the result has some "NaN" values which if I want to replace them with zero by np.nan_to_num the graph becomes wrong. How can I plot not the whole of list? I mean how can I plot the value within the list and not plot "NaN" like the image that I have attached in this post. The code that I have tried but not successfully is below:

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)

enter image description here

Mir.Emad
  • 83
  • 8
  • @DrBwts it is highly similar to mine but there is a problem in `fp = A[-np.isnan(A)]` I got this error `only integer scalar arrays can be converted to a scalar index` . BTW thanks for your nice recommendation – Mir.Emad Jun 16 '20 at 21:29

1 Answers1

2

Your x and y are lists. You have to convert them to numpy arrays before you can do efficient filtering:

x = np.array(x)
y = np.array(y)
good_mask = np.isfinite(y)

plt.plot(x[good_mask], y[good_mask])
Han-Kwang Nienhuys
  • 3,084
  • 2
  • 12
  • 31