Home > Back-end >  How to plot a function in matplotlib when getting the following error:ValueError: The truth value of
How to plot a function in matplotlib when getting the following error:ValueError: The truth value of

Time:09-24

Here is the function I am trying to plot

def f(x):
    return 50 * (1 / (1   ((50/5)-1) * e ** -max(0, 0.1 * x)))

I am getting the following error when trying to plot it using matplotlib: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I believe it is because of the -max(0, 0.1 * x) part of my function.

Here is the full version of my matplotlib code:

import matplotlib.pyplot as plt
import numpy as np
from math import e

# 100 linearly spaced numbers between -10 and 100
x = np.linspace(-10,100,200)

def f(x):
    return 50 * (1 / (1   ((50/5)-1) * e ** -max(0, 0.1 * x)))

# setting the axes at the centre
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

# plot the function
plt.plot(x,f(x), 'r')

# show the plot
plt.show()

Researching result

  • Related