Matplotlib可视化(二十五)-- 函数积分图

#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author: Caramel
@file: 2.8.py
@time: 2020/03/04
@desc:函数积分图
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
def fun(x):
    return (-1 * (x - 2) * (x - 8) + 40)
x = np.linspace(0, 10)
y = fun(x)
fig, ax = plt.subplots()
plt.plot(x, y, 'r', linewidth=2)


a = 2
b = 9
ax.set_xticks([a, b])
ax.set_yticks([])

ax.set_xticklabels(['$a$', '$b$'])#换成公式字体
plt.figtext(0.98, 0.05, '$x$')
plt.figtext(0.01, 0.98, '$y$')#0~1代表在图的比例处

ix = np.linspace(a, b)
iy = fun(ix)
ixy = zip(ix, iy)
verts = [(a, 0)] + list(ixy) + [(b, 0)]
poly = Polygon(verts, facecolor='0.9', edgecolor='0.3')

ax.add_patch(poly)

x_math = (a + b)*0.5
y_math = 35
plt.text(x_math, y_math, r'$ \int_a^b (-1 * (x - 2) * (x - 8) + 40)dx $', fontsize=15, horizontalalignment='center')
plt.show()

猜你喜欢

转载自blog.csdn.net/qq_42007339/article/details/104667860
今日推荐