python函数图的绘制
发布网友
发布时间:2023-01-05 21:48
我来回答
共1个回答
热心网友
时间:2023-10-16 16:02
<pre>
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
def func(x):
return -(x-2)*(x-8)+40
x=np.linspace(0,10)
y=func(x)
fig,ax = plt.subplots()
plt.plot(x,y,'r',linewidth=2)
plt.ylim(ymin=20)
a=2
b=9
ax.set_xticks([a,b])
ax.set_xticklabels(['$a$','$b$'])
ax.set_yticks([])
plt.figtext(0.9,0.05,'$x$')
plt.figtext(0.1,0.9,'$y$')
ix=np.linspace(a,b)
iy=func(ix)
ixy=zip(ix,iy)
verts=[(a,0)]+list(ixy)+[(b,0)]
poly = Polygon(verts,facecolor='0.9',edgecolor='0.5')
ax.add_patch(poly)
x_math=(a+b)*0.5
y_math=35
plt.text(x_math,y_math,r"$\int_a^b(-(x-2)*(x-8)+40)dx$",horizontalalignment='center',size=12)
plt.show()
</pre>