【发布时间】:2020-06-26 22:40:05
【问题描述】:
如何使用 numpy 和 matplotlib 绘制此图?
f(t) = t+2,如果t是整数f(t) = 2**t,否则
假设我的函数是为t > 0 定义的。
这可以绘图吗?
我尝试的方法不起作用...不知道如何解决。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from math import floor
# csfont = {'fontname':'Comic Sans MS'}
# hfont = {'fontname':'Helvetica'}
# plt.rcParams["font.family"] = "Comic Sans MS" # "Comic Sans MS"
font = {'family' : 'Comic Sans MS',
'weight' : 'bold',
'style' : 'italic',
'size' : 12}
matplotlib.rc('font', **font)
# a,b - the two ends of our interval
# [-16, 57]
a = 0.000001
b = 0.001
def f(t):
return ( t + 2 if (t==int(t)) else 2**t )
def g(x):
return np.floor(1/x)
t = np.linspace(a, b, 400)
f1 = f(t)
g1 = g(t)
plt.plot(t, f1, 'red', label="f (x)", linewidth=2) # plotting t, f1 separately
plt.plot(t, g1, 'green', label="g (x)", linewidth=2) # plotting t, g1 separately
plt.axhline(0, color='k')
plt.axvline(0, color='k')
# plt.xlabel('X axis', fontsize=14)
# plt.ylabel('Y axis', fontsize=14)
# plt.title("Illustration of the mean value theorem proof")
# plt.savefig('FFF.png')
plt.legend()
plt.show()
我也试过t==np.floor(t),但上面写着:The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我知道为什么,但这不是我想要的。我只想在那个 ndarray 上计算这个函数。
【问题讨论】:
-
我也试过
t==np.floor(t),但上面写着:The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() -
这能解决您的问题吗? stackoverflow.com/questions/31638508/…
标签: python python-3.x numpy matplotlib