python:画由抛物线: y^2=2x 与直线 y=x-4 所围成的图形

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

编写 test_diff_3_area.py 如下

python 复制代码
# -*- coding: utf-8 -*-
""" 画由抛物线: y^2=2x 与直线 y=x-4 所围成的图形 """
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

def fun(x):
    return x-4

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

def fun2(x):
    return -1*np.sqrt(2*x)


x = np.linspace(0, 10, num=100)
y = fun(x)
y1 = fun1(x)
y2 = fun2(x)

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

a = 0.0
b = 8.0
# 坐标轴设置
ax.set_xticks([0, 2, 4, 6, 8])
ax.set_yticks([])
#ax.set_xticklabels(['$a$', '$b$']) # 换成公式字体
plt.figtext(0.98, 0.05, '$x$')
plt.figtext(0.01, 0.98, '$y$') #0~1代表在图的比例处

# 绘制灰色多边形 y轴>0
ix = np.linspace(4, 8.0)
iy = fun(ix)
ixy = zip(ix, iy)
verts  = [(0, 0)] + list(ixy) + [(0, 4)]
# 多边形 Polygon
poly = Polygon(verts, facecolor='0.9', edgecolor='0.3')
ax.add_patch(poly)

ix = np.linspace(0, 8.0)
iy1 = fun1(ix)
ixy1 = zip(ix, iy1)
verts1 = [(0, 0)] + list(ixy1) + [(0, 4)]
# 多边形 Polygon
poly1 = Polygon(verts1, facecolor='1.0', edgecolor='0.3')
ax.add_patch(poly1)

# 绘制灰色多边形 y轴<0
ix = np.linspace(0.0, 2.0)
iy2 = fun2(ix)
ixy2 = zip(ix, iy2)
verts2 = [(0, 0)] + list(ixy2) + [(2, 0)]
# 多边形 Polygon
poly2 = Polygon(verts2, facecolor='0.9', edgecolor='0.3')
ax.add_patch(poly2)

ix = np.linspace(2, 4.0)
iy = fun(ix)
ixy = zip(ix, iy)
verts  = [(2, 0)] + list(ixy) + [(4, 0)]
# 多边形 Polygon
poly = Polygon(verts, facecolor='0.9', edgecolor='0.3')
ax.add_patch(poly)

# 添加 LaTex数学公式
x_math = 3
y_math = 3
latex = r'$\int_{-2}^4 (y+4 - \frac{1}{2} y^2)dy $'
plt.text(x_math, y_math, latex, fontsize=14, horizontalalignment='center')
plt.grid()
plt.show()

运行 python test_diff_3_area.py

相关推荐
gergul4 分钟前
python venv虚拟环境复制
linux·开发语言·python
MClink8 分钟前
小米开源大模型 MiMo 登顶全球第一,还白送百万亿 Token?手把手教你薅羊毛
人工智能·python·算法·openai·架构设计
m0_7414817810 分钟前
SQL删除包含敏感信息的数据_使用数据脱敏后执行删除
jvm·数据库·python
2401_8822737211 分钟前
如何用立即执行函数(IIFE)创建独立的作用域隔离变量
jvm·数据库·python
skilllite作者15 分钟前
Agent-Skills 核心能力与实战效能深度评测
大数据·开发语言·前端·数据库·人工智能·python
2301_7662834423 分钟前
mysql如何实现读写分离的权限分配_不同用户分别赋予权限
jvm·数据库·python
2401_8242226927 分钟前
HTML函数开发需不需要雷电接口_高速接口实际用途说明【说明】
jvm·数据库·python
2401_8242226934 分钟前
如何用 objectStore.get 根据主键 ID 获取数据库单条数据
jvm·数据库·python
郝学胜-神的一滴38 分钟前
高并发秒杀系统设计全解:从需求拆解到Redis库存实战
java·数据库·redis·python·程序人生·缓存·php
Mr.朱鹏42 分钟前
3.LangChain零基础速通-Prompt提示词模版和模型调用方法
人工智能·python·深度学习·langchain·llm·prompt·virtualenv