如何不绘制图形并制作切割图

时间:2020-06-16 20:07:52

标签: python numpy matplotlib

我已经计算了一个东西,结果有一些“ 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)

enter image description here

1 个答案:

答案 0 :(得分:2)

您的xy是列表。您必须先将它们转换为numpy数组,然后才能进行有效的过滤:

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

plt.plot(x[good_mask], y[good_mask])
相关问题