简体   繁体   English

使用 Matplotlib 绘制分段函数

[英]Plotting piecewise functions using Matplotlib

ValueError: The truth value of an array with more than one element is ambiguous. ValueError:具有多个元素的数组的真值不明确。 Use a.any() or a.all()使用 a.any() 或 a.all()

The above error is what I got for the below code intended to plot the piecewise function created.上面的错误是我为 plot 创建的分段 function 下面的代码得到的。 Can't figure it out.想不通。

import numpy as np
import matplotlib.pyplot as plt
def f(x):
    if x>=0 and x<=1:
        return x
    elif x>1 and x<=2:
        return 2-x
    else:
        return 0

xlist = np.linspace(0,1,num = 1000)
ylist = f(xlist)

plt.plot(ylist,xlist)
plt.show()

As per comments, you need to vectorize your method f (and also fix some mistakes):根据评论,您需要对方法f进行矢量化(并修复一些错误):

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    y = np.empty_like(x)
    mask1 = (0 <= x) & (x <= 1)
    mask2 = (1 < x) & (x <= 2)
    mask3 = np.logical_not((0 <= x) & (x <= 2)) #or ~((0 <= x) & (x <= 2))
    y[mask1] = x[mask1]
    y[mask2] = 2 - x[mask2]
    y[mask3] = 0
    return y

xlist = np.linspace(-1, 3, num = 1000)
ylist = f(xlist)

plt.plot(xlist, ylist)
plt.show()

You might also find that mask3 is not necessarry and vectorize a method f like this:您可能还会发现mask3不是必需的,并像这样对方法f进行矢量化:

def f(x):
    y = np.full_like(x, fill_value=0) #or np.zeros_like(x)
    mask1 = (0 <= x) & (x <= 1)
    mask2 = (1 < x) & (x <= 2)
    y[mask1] = x[mask1]
    y[mask2] = 2 - x[mask2]
    return y

在此处输入图像描述

What I understood is, you are basically comparing an array with a number which should not be the case here, the code provided might help you with solution,我的理解是,你基本上是在比较一个数组和一个数字,这里不应该是这种情况,提供的代码可能会帮助你解决问题,

import numpy as np
import matplotlib.pyplot as plt
def f(x):
    if x>=0 and x<=1:
        return x
    elif x>1 and x<=2:
        return 2-x
    else:
        return 0

xlist = np.linspace(0,1,num = 1000,endpoint=False)
ylist = np.array([f(num) for num in xlist])

plt.plot(xlist,ylist)
plt.show()

You can use the function you have already defined and vectorize is using np.vectorize .您可以使用您已经定义的 function 并且矢量化正在使用np.vectorize

import numpy as np
import matplotlib.pyplot as plt
    def f(x):
    if x>=0 and x<=1:
        return x
    elif x>1 and x<=2:
        return 2-x
    else:
        return 0.    # make this change to ensure float values

xlist = np.linspace(0,1,num = 1000)
ylist = np.vectorize(f)(xlist)

plt.plot(ylist,xlist)
plt.show()

The issue you are running into in your code is that you are passing an array to f rather than applying f element-wise, so any comparison gives an array of True or False values;您在代码中遇到的问题是您将数组传递给f而不是按元素应用f ,因此任何比较都会给出TrueFalse值的数组; this is what is ambiguous in the error message.这就是错误消息中模棱两可的地方。 Using np.vectorize will change the function defined to apply to each element of the array and give you the desired output.使用np.vectorize将更改定义的 function 以应用于数组的每个元素,并为您提供所需的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM