python:画由两条抛物线所围成的图形

《高等数学》同济大学版 P338

编写  test_diff_2_area.py  如下

# -*- coding: utf-8 -*-
""" 画由两条抛物线: y=sqrt(x) , y=x^2 所围成的图形的面积 """
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

def fun1(x):
    return np.sqrt(x)

def fun2(x):
    return np.power(x,2)


x = np.linspace(0, 1, num=100)
y1 = fun1(x)
y2 = fun2(x)

fig, ax = plt.subplots()
plt.plot(x, y1, 'r', linewidth=2)
plt.plot(x, y2, 'r', linewidth=2)

a = 0.2
b = 0.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)
iy1 = fun1(ix)
ixy1 = zip(ix, iy1)
iy2 = fun2(ix)
ixy2 = zip(ix, iy2)
verts1 = [(a, 0)] + list(ixy1) + [(b, 0)]
verts2 = [(a, 0)] + list(ixy2) + [(b, 0)]
# 多边形 Polygon
poly1 = Polygon(verts1, facecolor='0.9', edgecolor='0.3')
poly2 = Polygon(verts2, facecolor='1.0', edgecolor='0.3')
ax.add_patch(poly1)
ax.add_patch(poly2)

# 添加 LaTex数学公式
x_math = 0.5
y_math = 0.5
latex = r'$\int_a^b (\sqrt{x} - x^2)dx $'
plt.text(x_math, y_math, latex, fontsize=14, horizontalalignment='center')
plt.show()

运行 python test_diff_2_area.py 

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值